10 Clean Code Principles by Conner Ardman

Wednesday, December 31, 2025

Conner Ardman says, he has 10 clean code principles like below.

This post is a summary of the attached video.

1. Avoiding unnecessary nesting

Avoid unnecessary nesting by ordering conditional statements.

Before

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");
  }
}

After

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();
}

2. Avoiding ambiguity

Use clear and meaningful names for variables and functions.

Before

const MIN_PASSWORD = 6;
function checkPasswordLength(password) {
  return password.length >= MIN_PASSWORD;
}

After

const MIN_PASSWORD_LENGTH = 6;
function isPasswordLongEnough(password) {
  return password.length >= MIN_PASSWORD_LENGTH;
}

3. Avoiding unnecessary commentings

Add comments only when they are really needed.

Before

// 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;
}

After

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;
}

4. Keeping identical formatting

Keeping identical formattings like choosing text concatenation and template literal.

Before

const name = "Dug";
const age = 26;
function getUserInfo() {
  console.log("User Info:");
  console.log("Name:" + name);
  console.log(`Age: ${age}`);
}

After

const name = "Dug";
const age = 26;
function getUserInfo() {
  console.log("User Info:");
  console.log(`Name: ${name}`);
  console.log(`Age: ${age}`);
}

5. DRY(Don’t Repeat Yourself)

Do not repeat codes unnecessarily.

Before

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()}`);
}

After

function logAction(action) {
  console.log(`User ${action} at ${new Date()}`);
}

6. Failing fast early

Fail fast on conditional statement to avoid executing unnecessary codes.

Before

function getUppercaseInput(input) {
  const result = input?.toUpperCase?.();
  if (typeof input !== "string" || input.trim() === "") {
    throw new Error("Invalid input");
  }
  return result;
}

After

function getUppercaseInput(input) {
  if (typeof input !== "string" || input.trim() === "") {
    throw new Error("Invalid input");
  }
  return input.toUpperCase();
}

7. Avoiding magic number

Name variables properly to make their meaning clear.

Before

let price = 10;
if (transactionType === 1) {
  price *= 1.1;
}

After

const TAXABLE_TRANSACTION_TYPE = 1;
const TAX_MULTIPLE = 1.1;
let price = 10;
if (transactionType === TAXABLE_TRANSACTION_TYPE) {
  price *= TAX_MULTIPLE;
}

8. Avoding manipulating a global variable

Manipulate local variables rather than global variables to avoid bugs.

Before

let area = 0;
function calculateAndUpdateArea(radius) {
  const newArea = Math.PI * radius * radius;
  area = newArea;
  return newArea;
}

After

let area = 0;
function calculateArea(radius) {
  return (newArea = Math.PI * radius * radius);
}
area = calculateArea(5);

9. Keeping readibility

Make codes easy to read.

Before

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);

After

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);

10. Avoiding over optimization

Consider multiple factors to write good code, not just optimization.

Before

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));

After

const arr = [4, 2, 2, 8, 3, 3, 1];
const sorted = arr.slice().sort((a, b) => a - b);
console.log(sorted);