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.

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay