JAVASCRIPT CODING GUIDELINES _by Oleksii Prohonnyi
BEST PRACTICES
Use === Instead of == "If two operands are of the same type and value, then === produces true and !== produces false.“ - JavaScript: The Good Parts
Eval = Bad Not only will this decrease your script's performance substantially, but it also poses a huge security risk.
Don't Use Short-Hand This is a terrible practice that should be avoided at all costs.
Declarations on Top It is a good coding practice to put all declarations at the top of each script or function.
Initialize Variables It is a good coding practice to initialize variables when you declare them.
Use shortcut declarations - Use {} instead of new Object() - Use "" instead of new String() - Use 0 instead of new Number() - Use false instead of new Boolean() - Use [] instead of new Array() - Use /()/ instead of new RegExp() - Use function (){} instead of new function()
Avoid Global Variables Minimize the use of global variables. This includes all data types, objects, and functions. Global variables and functions can be overwritten by other scripts. Use local variables instead, and learn how to use closures.
Always Declare Local Variables All variables used in a function should be declared as local variables. Local variables must be declared with the “var” keyword, otherwise they will become global variables.
Long List of Variables? Omit the "Var" Keyword and Use Commas Instead
Say “No!” to loops declarations Do not declare variables inside loops, don't make the engine work any harder than it must.
End Your Switches with Defaults Always end your switch statements with a default. Even if you think there is no need for it.
Avoid Cluttering The Global Namespace Using globals may cause naming conflicts between javascript source files and cause code to break. For this reason, it is a good practice to encapsulate functionality within a single global namespace.
Avoid sync "Ajax" calls If you think that your situation requires sync mode, it is most likely time to re-think your design. Very few (if any) situations actually require Ajax requests in sync mode.
Use JSON When storing data structures as plain text or sending/retrieving data structures via Ajax, use JSON instead of XML when possible.
Comment Your Code Always comment important sections of your code.
JAVASCRIPT OOD
Object Objects can be created using a constructor or an object literal. The constructor should be a custom function only.
Constructor Constructor functions simply assign values to slots of a newly created object.
Inheritance JavaScript supports inheritance hierarchies through prototyping in the manner of Self.
Encapsulation Encapsulation includes the idea that the data of an object should not be directly exposed.
CODE CONVENTION
Always follow team’s code convention!
REFERENCES
1. “Clean Code: A Handbook of Agile Software Craftsmanship” (Robert Martin) 2. “Even Faster Web Sites” (Steve Souders) 3. “Maintainable JavaScript” (Nicholas Zakas) 4. w3schools.com 5. javascripttoolbox.com 6. tutsplus.com
SIGMA UKRAINE38
Oleksii Prohonnyi facebook.com/oprohonnyi linkedin.com/in/oprohonnyi

JavaScript Coding Guidelines