global = 1) should be avoided as much as possible.Good example:
Bad example:
$ only when they contain jQuery objects.Good example:
Bad example:
Good example:
Bad example:
Each variable must be declared on a separate line.
Multiple variables must be separated by a comma.
Good example:
Bad example:
Bad example:
var itemids = hostids = [];
var left = null, top = null, right = null, bottom = null;
var itemIds = [],
itemApplications = [];Good example:
Bad example:
Good example:
Bad example:
Good example:
Bad example:
The opening brace is written on the same line as the conditional statement.
The closing brace is always written on its own line.
Good example:
Bad example:
Good example:
if (list.length > 100) {
// some logic here
}
if (list.length > 100) {
// some logic here
} else {
// some logic here
}
if (list.length > 100) {
// some logic here
} else if (list.length == 1) {
// some logic here
} else {
// some logic here
}Bad example:
if (list.length > 100) // some logic here
if (list.length > 100) {
// some logic here
} else {
// some logic here
}Good example:
Bad example:
Example:
switch (condition) {
case 1:
// some logic here
// falls through
case 2:
// some logic here
break;
case 3:
// some logic here
break;
default:
// some logic here
break;
}Example:
try {
// some logic here
} catch (exception if condition) {
// some logic here
} finally {
// some logic here
}Example:
Example:
Example:
Example:
Use single quotes for strings when possible.
Add spaces around + in string concatenations.
Good example:
Bad example:
Good example:
Bad example:
Use spaces around binary operators, but do not add spaces between unary operators (like -, ++, --) and their operands.
Use a space between a keyword and the following parenthesis.
Good example:
Bad example:
Good example:
Bad example:
return.Good example:
var footerButton = GetData(e.target),
form = footerButton.closest('form'),
confirmText = footerButton.attr('confirm');
if (confirmText && !confirm(confirmText)) {
Event.stop(e);
return false;
}Bad example:
var footerButton = GetData(e.target),
form = footerButton.closest('form'),
confirmText = footerButton.attr('confirm');
if (confirmText && !confirm(confirmText)) {
Event.stop(e);
return false;
}// double slashes for comments.Example:
Example:
Use full words in comments (e.g., parameter instead of param).
Write multiline comments using block syntax and keep within the maximum line length.
Omit comments if the code is self-explanatory.
== equal or != not equal operators when comparing numeric values to zero.Good example:
Bad example:
typeof to check if a variable is undefined.Good example:
Bad example:
Decouple logic from the main function by placing it in a separate object.
Keep the main function simple and avoid unnecessary complexity.
Exception: simple functions may remain self-contained.
Good example:
var SinteticData = {
params: {
min: 0,
// some params
},
setParams: function (params) {
// some magic here
},
getSin: function (min, max, shift, period) {
// some magic here
},
getValue: function () {
// some magic with call getSin()
}
}
try {
SinteticData.setParams(JSON.parse(value));
return SinteticData.getValue();
} catch (error) {
error += (String(error).endsWith('.')) ? '' : '.';
Zabbix.log(3, '[ DEMO ] ERROR: ' + error);
return JSON.stringify({ 'error': error });
}Bad example:
var SinteticData = {
params: {
min: 0,
// some params
},
setParams: function (params) {
// some magic here
},
getSin: function (min, max, shift, period) {
// some magic here
}
}
try {
SinteticData.setParams(JSON.parse(value));
var tmp_data = SinteticData.getSin(SinteticData.params),
tmp_data2 = [],
tmp_out = '';
for (var i = 0; i < tmp_data.length; i++) {
// some magic with update tmp_data2
}
for (var j = 0; j < tmp_data.length; j++) {
// some magic with update tmp_out
}
return tmp_out;
} catch (error) {
error += (String(error).endsWith('.')) ? '' : '.';
Zabbix.log(3, '[ DEMO ] ERROR: ' + error);
return JSON.stringify({ 'error': error });
}For example, check the input parameters:
...
setParams: function (params) {
['min', 'max', 'shift', 'mutation_t', 'mutation_v', 'mutation_r', 'period'].forEach(function (field) {
if (typeof params === 'object' && typeof params[field] !== 'undefined' && params[field] !== '') {
SinteticData.params[field] = parseFloat(params[field]);
}
});
}
...Good example:
Bad example:
Good example:
try {
credentials = JSON.parse(credentials);
} catch (error) {
throw 'Failed to parse response received from instance metadata. Check debug log for more information.';
}Bad example:
Example:
params:
include:
- ./scripts/oci_validate_zbx_params.js
- ./scripts/oci.js
- ./scripts/compute/oci_compute_health_metrics_POST.jsExample:
if (typeof AWS.params.proxy !== 'undefined' && AWS.params.proxy !== '') {
request.setProxy(AWS.params.proxy);
}Good example:
Bad example: