Level Up Coding

Coding tutorials and news. The developer homepage gitconnected.com && skilled.dev && levelup.dev

Follow publication

7 Stupidly Simple Tricks to Make Your Code Cleaner in 2025

Nitin Sharma
Level Up Coding
Published in
9 min readFeb 4, 2025

Image generated by the freeCodeCamp team

1. Write Code Like You’re Explaining it to a 5-Year Old

let x = y + z;
let totalPrice = productPrice + shippingCost;
function calc(itm) {
let t = 0;
for (let i = 0; i < itm.length; i++) {
t += itm[i].p;
}
return t;
}
function calculateTotalPrice(cartItems) {
let totalPrice = 0;
for (let i = 0; i < cartItems.length; i++) {
totalPrice += cartItems[i].price;
}
return totalPrice;
}

2. Use AI Tools (or an AI Code Reviewer)

3. Get Rid of Unnecessary Comments

// Adding 10 to the result 
total = total + 10;
// Adding 10 because the client requires a 10% buffer for calculations 
total = total + 10;

4. Follow the DRY Principle

if (user.age > 18 && user.age < 65) { 
// Do something
}

if (user.age > 18 && user.age < 65) {
// Do something else
}
function isWorkingAge(age) { 
return age > 18 && age < 65;
}

if (isWorkingAge(user.age)) {
// Do something
}

5. Fix Your Code Formatting & Follow a Consistent Style

let total_price;  
let UserData;
function getuser() {}
let userData; 
let totalPrice;
function getUser() {}

6. Don’t Let Your Functions Do Too Much

function calculateCart(items) {
let total = 0;
for (let i = 0; i < items.length; i++) {
total += items[i].price * items[i].quantity;
}
return total > 100 ? total * 0.9 : total;
}
function calculateCart(items) {
const total = getCartTotal(items);
return applyDiscount(total);
}

function getCartTotal(items) {
return items.reduce((sum, item) => sum + item.price * item.quantity, 0);
}

function applyDiscount(total) {
return total > 100 ? total * 0.9 : total;
}

7. Organize Your Files and Folders Properly

project-folder/  

├── index.html
├── app.js
├── helpers.js
├── data.js
├── user.js
├── product.js
project-folder/  

├── pages/
│ ├── home/
│ │ ├── HomePage.js
│ │ ├── HomePage.css
│ │ └── HomePage.test.js
│ ├── user/
│ │ ├── UserPage.js
│ │ ├── UserPage.css
│ │ └── UserPage.test.js
│ └── product/
│ ├── ProductPage.js
│ ├── ProductPage.css
│ └── ProductPage.test.js
├── components/
│ ├── Header.js
│ ├── Footer.js
│ └── Button.js
├── utils/
│ └── api.js
└── index.js

Wrapping Up

No responses yet

Write a response