Skip to content
This repository has been archived by the owner on Oct 20, 2020. It is now read-only.

Coding Style

Cedric Pinson edited this page Jul 10, 2017 · 5 revisions

JavaScript CodeStyle

Coding style is enforced with prettier-js so if you are not sure do a grunt prettier on your code and check the code with grunt check. unittest will not pass if you dont respect coding style.

Naming

Naming should be as descriptive as possible. The only exception is the indexing variable in an loop. That can be shortened to a single letter starting from i.

  • variableNamesLikeThis
  • functionNamesLikeThis
  • ClassNamesLikeThis
  • methodNamesLikeThis
  • ConstantsLikeThis

Private properties and methods of objects begin with an underscore _.

Literals

Objects

  • Object should always be created by {} and not new Object().
  • When creating an Object with keys, Keys should be written without quotes. The exception is when it is necessary.

good:

var obj = { A: 1, b: 2, C: 3 };

var obj = {
    A: 1, 
    b: 2, 
    C: 3 
};

poor:

var obj = { "A" : 1, "b" : 2, "C" : 3 };

Arrays

  • Arrays should always be created by [] and not new Array().

Conditional instructions

switch

  • Every case should have a break statement, except for the default case or when returning a value.
  • Assignment should not be used in a test.

Cycles

for

  • Don't use foreach loops in critical parts.
  • Don't use for in loops on arrays.

Operators

with

Operator with not used.

The equality operator

Always use strict equality === (inequality !== ).

eval

Avoid using eval. To parse json, use JSON.parse.

undefined

Check the value through a strict comparison.

Good:

x === undefined;

Poor:

/ / In modern browsers already defined immutable undefined. 
var undefined; 
x === undefined;

typeof x === 'undefined'

x === void 0

GLSL CodeStyle

Check code sample:

vec4 encodeRGBM(const in vec3 col, const in float range) {
    if(range <= 0.0) {
        return vec4(col, 1.0);
    }
    vec4 rgbm;
    vec3 color = col / range;
    rgbm.a = clamp(max(max(color.r, color.g), max(color.b, 1e-6)), 0.0, 1.0);
    rgbm.a = ceil(rgbm.a * 255.0) / 255.0;
    rgbm.rgb = color / rgbm.a;
    return rgbm;
}