JavaScript: Code Quality
Page 16
Code Quality
Introduction
Code quality includes
a number of different techniques.
This page lists various
best practices
, recommended
by others, with JavaScript code examples.
Code Quality
Quality coding practices help improve legibility and reduce bugs. See examples of the following topics, spaces, statements, line breaks, variable placement, naming conventions and minimize use of String, Number and Boolean variables.
Spaces Between Operators
Recommended:
a + b = c;
Not recommended:
a+b=c;
Statements
Statements and expressions should end in semicolors - ;
,
however they're not necessary. It's just good practice.
Line Breaks
For legible code avoid lines longer than eighty characters. Insert line breaks after operators, when possible.
Variable Placement
Place all variables at the beginning of the script. This practice assists understanding of a script.
Variable Naming
Programmers commonly name private variables with a leading underscore. Generally JavaScript doesn't include private variables. All variables are public. However some workarounds exist.
_privateName = "Sonjia";
Naming Conventions
JavaScript programmers generally
apply camelCase
when naming a variable, function or method.
Camelcase
assigns a lower
case to the first letter of
a variable and upper case to
separate words within a variable.
For example:
showAlert(), catName, webDeveloper
Minimize Types
String, Number, Boolean Objects
The following declarations might seem clear, as far as data types, yet they decrease code execution speed. Minimize creation of new String, Number and Boolean types.
a = new String(); b = new Number(); c = new Boolean();
Summary
Code quality includes
a number of different techniques.
This page lists various
best practices
, recommended
by others, with JavaScript code examples.
Code Quality
Quality coding practices help improve legibility and reduce bugs. You saw examples of the following topics, spaces, statements, line breaks, variable placement, naming conventions and encouragment to minimize use of String, Number and Boolean variables.
Learn JavaScript
JavaScript's the foundation of Web developer and Website design skills. This free and unique JavaScript tutorial includes some new or seldom used, but useful features.