How to write “Clean Code”?

Sunita J. Marshal
3 min readDec 9, 2022

--

When working in a team or if you need to revisit your code in the future, clean code makes it simpler to read and understand. This can help you save time and effort and avoid mistakes.

In general, it is more efficient and effective. You may help ensure that your code functions effectively and efficiently by adhering to recommended practices and eliminating needless complexity.

It is also simpler to update and manage. It will be simpler to find and solve errors, as well as to add new features and functionality, if your code is organized and modular.

In this article, I’ll give you some important tips to write clean code.

  1. Maintain a unified format and style
    This helps you avoid errors and makes your code easier to read and understand. To help maintain uniformity, consider adopting a style guide, such as the one offered by Google.

Bad Practice:

const DAYS_IN_WEEK = 7;
const daysInMonth = 30;

const songs = ["Back In Black", "Stairway to Heaven", "Hey Jude"];
const Artists = ["ACDC", "Led Zeppelin", "The Beatles"];

function eraseDatabase() {}
function restore_database() {}

class animal {}
class Alpaca {}

Good Practice:

const DAYS_IN_WEEK = 7;
const DAYS_IN_MONTH = 30;

const SONGS = ["Back In Black", "Stairway to Heaven", "Hey Jude"];
const ARTISTS = ["ACDC", "Led Zeppelin", "The Beatles"];

function eraseDatabase() {}
function restoreDatabase() {}

class Animal {}
class Alpaca {}

2. Use simple, descriptive names for your classes, functions, and variables.
Use only well-known and commonly understood acronyms and single-letter names.

Bad Practice:

const yyyymmdstr = moment().format("YYYY/MM/DD")

Good Practice:

const currentDate = moment().format("YYYY/MM/DD")

3. To organize your code and make it easier to read, use white space and indentation.
Using blank lines to divide up code into distinct sections and indentation to illustrate the order of conditional statements and loops are two examples of how to do this.

Bad Practice:

function linearSearch(array: number[],value: number): boolean{
for(let i=0;i<array.length;i++){
if (array[i] === value) return true;
}
return false;
}

Good Practice:

function linearSearch(array: number[], value: number): boolean {
for (let i = 0; i < array.length; i++) {
if (array[i] === value) return true;
}

return false;
}

4. To give more context and explanation, comment your code.
When working in a group or if you need to go back and review your code later, this can be extremely useful.

Bad Practice:

function hashIt(data) {
// The hash
let hash = 0;

// Length of string
const length = data.length;

// Loop through every character in data
for (let i = 0; i < length; i++) {
// Get character code.
const char = data.charCodeAt(i);
// Make the hash
hash = (hash << 5) - hash + char;
// Convert to 32-bit integer
hash &= hash;
}
}

Good Practice:

function hashIt(data) {
let hash = 0;
const length = data.length;

for (let i = 0; i < length; i++) {
const char = data.charCodeAt(i);
hash = (hash << 5) - hash + char;

// Convert to 32-bit integer
hash &= hash;
}
}

5. Keep your code organized and modular.
By dividing it into compact, reusable classes and functions. As a result, your code is simpler to test, maintain, and reuse in other projects.

Bad Practice:

function emailClients(clients) {
clients.forEach(client => {
const clientRecord = database.lookup(client);
if (clientRecord.isActive()) {
email(client);
}
});
}

Good Practice:

function emailActiveClients(clients) {
clients.filter(isActiveClient).forEach(email);
}

function isActiveClient(client) {
const clientRecord = database.lookup(client);
return clientRecord.isActive();
}

6. Keep your code simple and easy to understand.
Instead, aim for clarity and simplicity. Your code will be simpler to read, comprehend, and maintain as a result.

Be simple and lazy.

7. Test your code to make sure it is accurate and bug-free.
By doing so, you can identify concerns as they arise and stop them from growing into bigger problems.

Conclusion:

Overall, considering readability and maintainability when writing code is essential. You may ensure that your code is simple to comprehend and work with in the now and the future by adhering to these recommendations and best practises.

Thanks for reading!

Leave some claps, comment, and share with colleagues and friends.

--

--

Sunita J. Marshal

Freelance Political writer, global affairs and geo-political matters!