Skip to content

Javascript: Guidelines

Oscar Fabiano edited this page Jul 6, 2021 · 1 revision

This is a guide to recommendations for javascript programming. It's based on code review and real mistakes made by the team over time, so it is absolutely necessary to read and follow these recommendations.

Table of Contents

Mandatory Rules

  1. Will you add something to the page? Create a component
  2. Split your work into different files and folders
  3. If you notice that your code has many lines, methods and variables. It's time to start refactoring, creating smaller components and working with emit
  4. Before creating anything, check that there is no component. We already have components for a date, list, simple tables and upload fields
  5. Before merging into the master branch, check if your component if is showing errors on the console (F12)

Best Pratices (Mandatory)

Bolleans

if

Wrong way:

if(variable === 1)
if(variable === 0)
if(variable === true)
if(variable === false)

Right way:

if(variable)
if(!variable)

Wrong way:
var flag = true
if(flag)

var x = false
if(x)

var i = 0
if(i > 0)

Recommendations

Right way:

var isEnabled = true
if(isEnabled)

var countSales = 0
if(countSales > 0)

Assignment

Wrong way:

variable = 1
variable = 0

Right way:

variable = true
variable = false

Code Blocks

Inline if

Wrong way:

if(condition) doSomething()

Right way:

if(condition){
    doSomething()
}

Wrong way:
if(condition) variable = 'something'
else variable = 'something else'

Right way:

variable = condition ? 'something' : 'something else'

Loops

foreach instead of using for

Wrong way:

for(var i = 0; i < data.lenght; i++){
     // data[i]
}

Right way:

data.forEach(item => {
    // item
});
Clone this wiki locally