Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
vgno-coding-standards
Advanced tools
##Overview
###Scope
This document provides the coding standards and guidelines for developers and teams working for or with VG. The subjects covered are:
###Goals
Good coding standards are important in any development project, particularly when multiple developers are working on the same project. Having coding standards helps to ensure that the code is of high quality, has fewer bugs, and is easily maintained.
##General
\n
.##Naming
variableNamesLikeThis
functionNamesLikeThis
ClassNamesLikeThis
methodNamesLikeThis
CONSTANTS_LIKE_THIS
namespacesLikeThis
events-like-this
private
or protected
properties and methods should be prefixed with a single _
characterJSON
and XML
are written in CamelCase
. For example: Json
, Xml
.-
character instead.Bad:
setTimeout(function() {
doThatThing();
}, 1000);
Good:
setTimeout(function someDescriptiveTitle() {
doThatThing();
}, 1000);
##Strictness
Always start the top level block with 'use strict;'
. If writing CommonJS modules, assume a scope is present and place the pragma at the top of the file. If writing AMD or standalone Javascript, ensure your file has a separate scope and apply the pragma as early as possible:
CommonJS:
'use strict';
var fs = require('fs');
/**
* ... more code
*
* module will (implicitly) be wrapped with:
* (function (exports, require, module, __filename, __dirname) {
* // your code here
* })(module.exports, require, module, 'your-file.js', '/directory/to/file');
*/
AMD/standalone:
(function() {
'use strict';
var foo = 'bar';
// ... more code
});
##Variable declaration
var
statement;Good:
var keys = ['foo', 'bar'];
var values = [23, 42];
var object = {}, key;
while (items.length) {
key = keys.pop();
object[key] = values.pop();
}
Bad:
var keys = ['foo', 'bar'], values = [23, 42];
var object = {};
while (items.length) {
var key = keys.pop();
object[key] = values.pop();
}
##Literals
###Objects
var obj = {
prop: 0
};
Good:
var obj = {
a: 0,
b: 1,
lengthyName: 2
};
Bad:
var obj = {
a : 0,
b : 1,
lengthyName: 2
};
Good:
var obj = {
key: 0,
'key-key': 1
};
Bad:
var obj = {
'key': 0,
'key-key': 1
};
Bad:
var myObject = {
key: 'value',
key2: 'value', // <-- BAD
};
Good:
var obj = {
'default': 'someValue',
'super': 'someSuper'
};
Bad:
var obj = {
default: 'someValue',
super: 'someSuper'
};
###Arrays
var fellowship = ['foo', 'bar', 'baz'];
###Strings
var lyrics = 'Never gonna give you up. Never gonna let you down. Never gonna turn around and desert you.';
var test = 'It shouldn\'t fail';
##Semicolons Statements should always end with a semicolon.
##Keywords
if (test) {
// ...
}
function foo() {
// ...
}
var bar = function() {
// ...
};
return;
##Block Statements
if (test) {
// ...
}
function foo() {
// ...
}
Good:
if (test) {
return;
}
Bad:
if (test)
return;
if (test) return;
if (test) { return; }
##Conditional Statements ###if
else
keyword should be on the same line as the closing brace of the if-part of the statement:if (test) {
// ...
} else {
// ...
}
Good:
var foo = bar();
if (foo > 0) {
// ...
}
Bad:
var foo;
if ((foo = bar()) > 0) {
// ...
}
Good:
if (condition) {
actionIfTrue();
} else {
actionIfFalse();
}
Bad:
condition && actionIfTrue() || actionIfFalse();
if (longCondition ||
anotherLongCondition &&
yetAnotherLongCondition
) {
// ...
}
Good:
if (getType() === 'driving') {
}
Bad:
if ('driving' === getType()) {
}
###switch The switch statement should be written as in the example:
switch (value) {
case 1:
// ...
break;
case 2:
// ...
break;
default:
// ...
// no break keyword on the last case
}
##Loops
###for
Functional constructs (map, reduce etc) are prefered where applicable. Array.prototype.forEach is prefered over the for
loop.
[1, 2, 3].forEach(function (value) {
console.log(value);
});
Performance-critical parts of the code can use a for
statement, as well as cases where the loop can be broken out of before iteration has completed.
###for (var i in obj)
When using the for-in
loop, authors should be aware of the pitfalls of enumerable properties on prototypes and know when to use hasOwnProperty().
##Operators and keywords ###'const' keyword
Never use the const keyword.
###'with' operator
The with
operator should not be used.
###Comparison Operators
If there is no need for type casting, the strict equality operator ===
(or strict inequality !==
) should be used.
###Ternary Operator The ternary operator should be written as in the examples:
var x = a ? b : c;
var y = a ?
longButSimpleOperandB : longButSimpleOperandC;
var z = a ?
moreComplicatedB :
moreComplicatedC;
###Unary Operators Unary operators should be typed without whitespace between them and their operands:
var foo = !bar;
Exceptions from this rule are the unary special JS operators).
##eval
The eval
function should be avoided.
json
serialized data should be parsed with JSON.parse.
##undefined
Checking for undefined
values should be done using the strict equality operator.
Good:
x === undefined;
Bad:
typeof x === 'undefined'
x === void 0
##Parentheses
delete
, typeof
and void
, or with the keywords return
, throw
and new
.##Exceptions
throw
should be used with new Error
or an object of a class derived from Error
:
Good:
throw new Error('msg');
Bad:
throw 'msg';
##Type Casting Type casting should be done explicitly (note the difference between parsing and type casting):
Good:
Boolean(foo)
Number(bar)
String(baz)
[].indexOf(qux) === -1 or [].indexOf(qux) < 0
Bad:
!!foo
+bar
baz + ''
~[].indexOf(qux)
##Parsing
Good:
var width = parseInt('20px', 10);
var someNum = parseInt('0x10', 16);
Bad:
var width = parseInt('20px');
var someNum = parseInt('0x10');
##Multi-Line Statements
var debt = this.calculateBaseDebt() + this.calculateSharedDebt() + this.calculateDebtPayments() +
this.calculateDebtFine();
Good:
DoSomethingThatRequiresALongFunctionName(
very_long_argument1,
argument2,
argument3,
argument4
);
anotherStatement;
Bad:
DoSomethingThatRequiresALongFunctionName(
very_long_argument1,
argument2,
argument3,
argument4);
anotherStatement;
##Method Chaining When a method is called on a new line, it should:
.
.Good:
someObject
.operation()
.operationWithCallback(function (obj) {
obj.processed = true;
})
.end();
Bad:
someObject.
start().
end();
someObject
.start()
.end();
##String concatenation
+
operator.[].join('')
should be avoided.Good:
var foo = 'A rather long string of English text, an error message ' +
'actually that just keeps going and going -- an error ' +
'message to make the Energizer bunny blush (right through ' +
'those Schwarzenegger shades)! Where was I? Oh yes, ' +
'you\'ve got an error and all the extraneous whitespace is ' +
'just gravy. Have a nice day.';
Bad:
var foo = 'A rather long string of English text, an error message \
actually that just keeps going and going -- an error \
message to make the Energizer bunny blush (right through \
those Schwarzenegger shades)! Where was I? Oh yes, \
you\'ve got an error and all the extraneous whitespace is \
just gravy. Have a nice day.';
##Empty Lines A single empty line can be used as a separator for grouping the code into logical blocks:
doSomethingTo(x);
doSomethingElseTo(x);
andThen(x);
nowDoSomethingWith(y);
andNowWith(z);
##Function Context
doAsync(function () {
this.fn();
}.bind(this));
Good:
[1, 2, 3].forEach(function (n) {
this.fn(n);
}, this);
Bad:
[1, 2, 3].forEach(function (n) {
this.fn(n);
}.bind(this));
_this
:var _this = this;
doAsync(function () {
_this.fn();
});
##Comments
//
. Between the //
and the text of the comment should be one space character.##Classes
var FooClass = inherit({
__constructor: function () {},
// destructors are placed right after the constructor
destruct: function () {},
someMethod: function () {}
});
##node.js
###Importing Modules
Good:
var http = require('http');
var fs = require('fs');
// code here
Bad:
var http = require('http');
// code here
var fs = require('fs');
// code here
This rule does not apply to modules that are imported "on demand".
###JSHint
Please use JSHint with the .jshintrc
config found in this
repository.
###JSCS
Please use JSCS with the .jscsrc
config found in this repository.
###ESLint
Please use ESLint with the .eslintrc
config found in this repository.
FAQs
VG.no coding standards
The npm package vgno-coding-standards receives a total of 0 weekly downloads. As such, vgno-coding-standards popularity was classified as not popular.
We found that vgno-coding-standards demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.