Wednesday, December 31, 2025
Conner Ardman says, he has 10 clean code principles like below.
This post is a summary of the attached video.
Avoid unnecessary nesting by ordering conditional statements.
function processUser(user) {
if (user !== null) {
if (user.hasSubscription) {
if (user.page >= 18) {
showFullVersion();
} else {
showChildrenVersion();
}
} else {
throw new Error("User needs a subscription");
}
} else {
throw new Error("No user found");
}
}
function processUser(user) {
if (user === null) {
throw new Error("No user found");
}
if (!user.hasSubscription) {
throw new Error("User needs a subscription");
}
if (user.age < 18) {
return showChildrenVersion();
}
showFullVersion();
}
Use clear and meaningful names for variables and functions.
const MIN_PASSWORD = 6;
function checkPasswordLength(password) {
return password.length >= MIN_PASSWORD;
}
const MIN_PASSWORD_LENGTH = 6;
function isPasswordLongEnough(password) {
return password.length >= MIN_PASSWORD_LENGTH;
}
Add comments only when they are really needed.
// Function to check if a number is prime
function isPrime(number) {
//Check if number is less than 2
if (number < 2) {
// If less than 2, not a prime number
return false;
}
// At lest 1 divisor must but less than square root, so we can stop there
for (let i = 2; i <= Math.sqrt(number); i++) {
// Check if number is divisible by i
if (number % i === 0) {
// If divisible, number is not prime
return false;
}
}
// After all checks, if not divisible by any i, number is prime
return true;
}
function isPrime(number) {
if (number < 2) {
return false;
}
// At lest 1 divisor must but less than square root, so we can stop there
for (let i = 2; i <= Math.sqrt(number); i++) {
if (number % i === 0) {
return false;
}
}
return true;
}
Keeping identical formattings like choosing text concatenation and template literal.
const name = "Dug";
const age = 26;
function getUserInfo() {
console.log("User Info:");
console.log("Name:" + name);
console.log(`Age: ${age}`);
}
const name = "Dug";
const age = 26;
function getUserInfo() {
console.log("User Info:");
console.log(`Name: ${name}`);
console.log(`Age: ${age}`);
}
Do not repeat codes unnecessarily.
function logLogin() {
console.log(`User logged in at ${new Date()}`);
}
function logLogout() {
console.log(`User logged in at ${new Date()}`);
}
function logSignup() {
console.log(`User signed up at ${new Date()}`);
}
function logAction(action) {
console.log(`User ${action} at ${new Date()}`);
}
Fail fast on conditional statement to avoid executing unnecessary codes.
function getUppercaseInput(input) {
const result = input?.toUpperCase?.();
if (typeof input !== "string" || input.trim() === "") {
throw new Error("Invalid input");
}
return result;
}
function getUppercaseInput(input) {
if (typeof input !== "string" || input.trim() === "") {
throw new Error("Invalid input");
}
return input.toUpperCase();
}
Name variables properly to make their meaning clear.
let price = 10;
if (transactionType === 1) {
price *= 1.1;
}
const TAXABLE_TRANSACTION_TYPE = 1;
const TAX_MULTIPLE = 1.1;
let price = 10;
if (transactionType === TAXABLE_TRANSACTION_TYPE) {
price *= TAX_MULTIPLE;
}
Manipulate local variables rather than global variables to avoid bugs.
let area = 0;
function calculateAndUpdateArea(radius) {
const newArea = Math.PI * radius * radius;
area = newArea;
return newArea;
}
let area = 0;
function calculateArea(radius) {
return (newArea = Math.PI * radius * radius);
}
area = calculateArea(5);
Make codes easy to read.
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const result = numbers.reduce((acc, n) => (n & 1 ? [...acc, n * n] : acc), []);
console.log(result);
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const filteredAndSquared = numbers.filter((n) => n % 2 !== 0).map((n) => n * n);
console.log(filteredAndSquared);
Consider multiple factors to write good code, not just optimization.
const arr = [4, 2, 2, 8, 3, 3, 1];
function countingSort(arr, min, max) {
let count = new Array(max - min + 1).fill(0);
arr.forEach((element) => {
count[element - min]++;
});
let index = 0;
for (let i = min; i <= max; i++) {
while (count[i - mix] > 0) {
while (count[i - mix] > 0) {
arr[index++] = i;
count[i - min]--;
}
}
}
return arr;
}
console.log(countingSort(arr, 1, 8));
const arr = [4, 2, 2, 8, 3, 3, 1];
const sorted = arr.slice().sort((a, b) => a - b);
console.log(sorted);