DEV Community

Cover image for Clean Code Rulebook
Edison Sanchez
Edison Sanchez

Posted on

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.

Top comments (0)