DEV Community

Cover image for Clean Code Rulebook
Edison Sanchez
Edison Sanchez

Posted on

3 3

Clean Code Rulebook

Variable Names

Variable name with meaning:

Wrong

let x = priceValidation(0);

Right

let isPriceValid = priceValidation(0);

Singular and Plural

Wrong

let person = ['John', 'Mary', 'Luke'];
let persons = 'Adam';

Right

let persons = ['John', 'Mary', 'Luke'];
let person = 'Adam';

Functions are Verbs

Wrong

const total = (a, b) => a + b;

Right

const calcTotal = (a, b) => a + b;

To Be or Not Be...

Wrong

const greenColor = ( color === 'green');

Right

const isGreenColor = ( color === 'green');

WTH is 273.15

Wrong

const kelvinDegrees = 24 + 273.15;

Right

const celsiusDegrees = 24;
const kelvinScale = 273.5;
const kelvinDegrees = celsiusDegrees + kelvinScale;

Please, call me by full name.

Wrong

const names = [ 'John Smith', 'Mary Smith', 'Luke Smith' ];
const namesWithLastNameSmith = names.filter ( (x) => { 
//code ...
});

Right

const names = [ 'John Smith', 'Mary Smith', 'Luke Smith' ];
const namesWithLastNameSmith = names.filter ( (name) => { 
//code ...
});

Errors, Warnings, and Info.

Something Wrong?

Wrong

const isOver18YearsOld = age > 17;
if( isOver18YearsOld ) {
  showMessage('Is Something wrong :(';
}

Right

const isOver18YearsOld = age > 17;
if( isOver18YearsOld ) {
  showMessage('Sorry, You must be over 18 years old.';
}

Debuggin in Production :O

Wrong

throw new Error(Unexpected Error);

Right

throw new Error(someFile.js:someFunction: undefined value in variable);

I keep updating this RuleBook.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay