Skip to content

Write more readable code with the following tips

When the program is written only by us, it is relatively easy to understand it because we "understand" ourselves.

But it turns out that most of the programs you create may be done with other people. And even if you do it alone, wouldn't it be good if others could understand it?

That's why I bring you this guide of guidelines based on the standard of programming in JavaScript.

Guidelines

The purpose of creating this guide is to standardize the way the code that is developed is written. This is to unify criteria, reduce analysis time and facilitate understanding and updating of previously written code. This guide has examples in JavaScript and PHP.

Comments

General rules for adding comments.

JavaScript

javascript
// do not start comments with a capital letter nor end them with a period '.'

/**
 * Start multi-line comments with a capital letter
 * Mainly because they are generally used to document large
 * blocks of code, such as functions, for example
 */

/* use spaces between comment slashes */

let x = 'x'; // leave a space between the end of the statement and the slashes '//'

Variables

Variable declaration

When declaring variables, avoid leaving the initial value as null. Put a space between variable = and its value.

JavaScript

javascript
// avoid using 'var' to declare variables
let string = ''; // variable initialized as 'string'
let number = 0; // variable initialized as 'number'
let object = {}; // variable initialized as 'object'
let array = []; // variable initialized as 'object'

PHP

php
$string = '';       // variable initialized as 'string'
$number = 0;        // variable initialized as 'number'
$array = [];      // variable initialized as 'array'

Operations on variables

It is recommended to leave spaces between operators when performing operations on variables.

JavaScript

javascript
let one = 1;
let two = 2;
let three = one + two;
let four = one * 2 + two;

PHP

php
$one = 1;
$two = 2;
$three = $one + $two;
$four = $one * 2 + $two;

Data comparison

It is recommended to use triple comparison operators === instead of ==

This is because the triple comparison === checks that both the value and the data type are equal.

javascript
if ('25' == 25) {
  // successful comparison
}
if ('25' === 25) {
  // failed comparison since it compares 'string' data type with 'number'
}

Strings

It is recommended to use single quotes '' for handling strings.

javascript
let x = 'string';
if (x === 'string') {
  // successful condition
} else {
  // unsuccessful condition
}

Validations

If you want to validate whether a variable you are dealing with is of a specific data type, you can use the following validation methods.

JavaScript

javascript
// validate if it is of type integer
let num = 0;
if (typeof num === 'number') {
  // successful validation
}

// validate if it is of type string
let str = 'hello';
if (typeof str === 'string') {
  // successful validation
}

// validate if it is an object
let obj = {};
if (typeof obj === 'object') {
  // successful validation
}

// validate if it is an array
let arr = [];
if (Array.isArray(arr)) {
  // successful validation
}

// validate if it is boolean
let bool = true;
if (typeof bool === 'boolean') {
  // successful validation
}

Using typeof x === 'array' is wrong because arrays in JavaScript are objects.

PHP

php
// validate if it is of type integer
$num = 0
if (is_int($num)) {
  // successful validation
}

// validate if it is of type string
$str = 'hello'
if (is_string($str)) {
  // successful validation
}

// validate if it is an object
$obj = {}
if (is_object($obj)) {
  // successful validation
}

// validate if it is an array
$arr = []
if (is_array($arr)) {
  // successful validation
}

// validate if it is boolean
$bool = []
if (is_bool($bool)) {
  // successful validation
}

Functions

Declaration of functions and calling of functions

When declaring functions, a space should be placed between the keyword function, the parentheses () and the starting brace {.

This is to differentiate the declaration from a function call.

JavaScript

javascript
// declaration of a function
function iAmAFunction() {
  console.log('This is a text printout');
}

// declaration of a function in an object
let object = {
  iAmAFunction: function () {
    console.log('This is a text printout');
  },
};

// calling a function
iAmAFunction();

PHP

php
// declaration of a function
function iAmAFunction () {
  echo 'This is a text printout';
}

// calling a function
iAmAFunction();

Use of native language functions

JavaScript considers a value false if it is:

  • false
  • 0
  • '' or ""
  • null
  • undefined
  • NaN (example, divisions between 0)

Conditionals if and else

javascript
if (condition) {
  // code for first condition
} else if (anotherCondition) {
  // code for second condition
} else {
  // code for third condition
}

// ternary conditions
let condition = true(condition) ? 'Condition met' : 'Condition failed'; // condition met

Cycles while and for

javascript
for (let a = 0; a < 10; a++) {
  // repetitive code
}

while (true) {
  // repetitive code
}

do {
  // repetitive code
} while (true);