9 Clean Code Principles To Keep In Mind

Writing clean code isn't just about aesthetics—it's about writing code that is easy to read, maintain, and extend. Here are 9 fundamental principles to help you write cleaner, more professional code.
1. Meaningful Names
Choose names that clearly reveal the purpose of variables, functions, and classes.
Bad:
let d; d = new Date();
Good:
let currentDate = new Date();
2. One Function, One Responsibility
Each function should do one thing and do it well.
Bad:
function getUserDataAndRender() { fetchUser(); renderUser(); }
Good:
function fetchUser() { // fetch logic } function renderUser() { // render logic }
3. Avoid Magic Numbers
Use named constants to make your code more understandable.
Bad:
if (user.age > 18) { // logic }
Good:
const LEGAL_AGE = 18; if (user.age > LEGAL_AGE) { // logic }
4. Use Descriptive Booleans
Boolean names should express a condition.
Bad:
let flag = true;
Good:
let isUserLoggedIn = true;
5. Keep Code DRY (Don't Repeat Yourself)
Repeated logic increases the chance of bugs. Reuse your code.
Bad:
calculateTotal(price, tax); calculateTotal(price, tax);
Good:
function calculateTotal(price, tax) { return price + price * tax; } let total = calculateTotal(price, tax);
6. Avoid Deep Nesting
Too many nested blocks can make code hard to read. Return early or use guard clauses.
Bad:
if (user) { if (user.isActive) { // logic } }
Good:
if (!user || !user.isActive) return; // logic
7. Comment Why, Not What
Explain the intent behind complex code.
Bad:
// loop through array for (let i = 0; i < items.length; i++) { // ... }
Good:
// Reorder items by priority before processing for (let i = 0; i < items.length; i++) { // ... }
8. Limit Function Arguments
Avoid too many parameters. Group related data.
Bad:
function createUser(name, age, email, address) { // logic }
Good:
function createUser({ name, age, email, address }) { // logic }
9. Code Should Be Self-Explanatory
Aim to write code that doesn’t need comments to be understood.
Bad:
let a = 5; let b = 10; let c = a + b;
Good:
let numberOfApples = 5; let numberOfOranges = 10; let totalFruits = numberOfApples + numberOfOranges;
Final Thoughts
Clean code is not a luxury; it's a necessity. Following these principles will make your code easier to debug, extend, and collaborate on. Remember, code is read more often than it is written—make it readable.