This repository has been archived by the owner on Oct 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 202
Coding Style
Cedric Pinson edited this page Jul 10, 2017
·
5 revisions
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 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 _.
- Object should always be created by
{}
and notnew 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 should always be created by
[]
and notnew Array()
.
- 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.
- Don't use foreach loops in critical parts.
- Don't use for in loops on arrays.
Operator with
not used.
Always use strict equality ===
(inequality !==
).
Avoid using eval. To parse json, use JSON.parse.
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
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;
}