Coding Without Chaos: 9 Best Practices for Sustainable Code

Imagine revisiting your old project after months or even years. You open the code, feeling a wave of nostalgia, but then… what on earth does this variable x1y2z mean? Who wrote this 300-line function? And why are there no comments anywhere?! Then it hits you—it was you.

Why Write Clean Code?

Because developers spend 90% of their time reading code, not writing it. Clean code isn’t just about aesthetics—it’s an investment in your future and your team’s sanity. If you want your project to survive beyond a sprint demo and not be cursed by every developer who touches it after you, keep reading. 

1. Use Meaningful Names

Which version makes more sense to you?

Not Recommended:

function cx(d) { 
   return d[0]; 
}

Recommended:

function getFirstElement(data) { 
   return data[0]; 
}

Saving a few keystrokes today could cost you (and everyone else) countless hours deciphering cryptic names later. Plus, in JavaScript, the minifier already optimizes names for you—so there’s no need to shorten them manually.

2. Write Functions That Do One Thing Well

If your function has more responsibilities than the average full-stack developer, something’s off.

Not Recommended:

function processUserData(user) { 
   validateUser(user); 
   cleanUserData(user); 
   saveUserToDb(user); 
   sendWelcomeEmail(user); 
}

Recommended:

function validateUser(user) { /* ... */ } 
function cleanUserData(user) { /* ... */ } 
function saveUserToDb(user) { /* ... */ } 
function sendWelcomeEmail(user) { /* ... */ }

Smaller, single-responsibility functions make your code more flexible, reusable, and easier to test.

3. DRY – Avoid Repeating Yourself

Copy-pasting the same code across multiple places is a recipe for maintenance nightmares. Stick to the DRY (Don’t Repeat Yourself) principle.

Not Recommended:

function getAdminEmail(admin) { 
   return admin.email; 
} 
function getUserEmail(user) { 
   return user.email; 
}

Recommended:

function getEmail(entity) { 
   return entity.email; 
}

One update, and it works everywhere. Smart and efficient!

4. Comment Only When Necessary

The best comment is the one you don’t need to write because your code is self-explanatory.

Not Recommended:

// This variable counts loop iterations 
let i = 0;

Recommended:

// Tracks connection attempts to prevent infinite loop 
// in case of repeated failures. 
let iterationCount = 0;

When commenting, explain the why, not the what.

5. Proper Formatting Boosts Readability

Well-formatted code is easier to read and work with.

Not Recommended:

function foo ( x,y ) { 
   return x+y; 
}

Recommended:

function foo(x, y) { 
   return x + y; 
}

Use linting and auto-formatting. A five-minute setup saves you hours of frustration.

6. Handle Errors Gracefully

Never assume everything will work flawlessly. That’s why we have try-catch—to prevent your application from crashing at the worst possible moment.

Not Recommended:

function readFile(filePath) { 
   let file = open(filePath, 'r'); 
   let content = file.read(); 
   file.close(); 
   return content; 
}

Recommended:

function readFile(filePath) { 
   try { 
      return fs.readFileSync(filePath, 'utf-8'); 
} catch (error) { 
   console.error('Error reading file:', error); 
   return null; 
   } 
}

You don’t want your entire application to break just because someone accidentally deleted a file, do you?

7. Tests Save You from Disaster

Without tests, your code is like a parachute you forgot to check before jumping.

Recommended:

function add(a, b) { 
   return a + b; 
} 
unction testAdd() { 
   console.assert(add(2, 3) === 5, 'Test failed'); 
   console.assert(add(-1, 1) === 0, 'Test failed'); 
} 
testAdd();

If something goes wrong, you want to catch it before your boss or customer does.

8. Fewer Dependencies = Fewer Problems

Over-relying on external libraries can backfire. A perfect example is the infamous left-pad npm incident. In 2016, a developer removed the small left-pad package from npm, causing thousands of projects across the internet to break instantly. This event highlighted the risks of depending on external libraries for simple operations. You can read more about it here.

Not Recommended:

import _ from 'lodash'; 
function calculateSum(data) { 
   return _.sum(data); 
}

Recommended:

function calculateSum(data) { 
   return data.reduce((acc, num) => acc + num, 0); 
}

Use third-party libraries wisely—only when they are truly necessary.

9. Refactoring Is Essential

Refactor continuously—otherwise, your old code will soon feel like a tangled mess.

Recommended:

// Original Code 
function calculateDiscount(price, discount) { 
   let discountedPrice = price - (price * discount / 100); 
   return discountedPrice; 
} 

// Refactored Code 
function calculateDiscount(price, discount) { 
   return price * (1 - discount / 100); 
}

Simpler, cleaner, better.

Final Thoughts: Clean Code, Easier Life

Clean code isn’t just about looking neat—it’s about being understandable, maintainable, and efficient. At Bart, we know that well-written code saves time, reduces frustration, and cuts costs. And if you love writing clean, structured code as much as we do, you might just feel right at home with us. 😉