Blog-Archiv

Sonntag, 21. September 2014

JavaScript Best Practices

As I am tired of looking for more JavaScript gotchas (there are too much of them) I summarize my Best Practice collection here and now. Read my other JS posts for discussion of them (did I discuss them all?).

Target of these practices is to minimize the risk of using the JavaScript programming language, and to reach a maximum of readability.

  1. Write "use strict"; at start of any function.
    That way you quickly will find undefined variables in your code.

  2. Always use var when defining variables.
    This is required in strict mode.

  3. Terminate every statement by a ; (semicolon).
    Even when it does not help against the JS interpreter adding an unintended newline, it makes your code clearer.

  4. Use === and !== instead of == and !=
    And thus prove that none of your variables changed its type on the fly.

  5. Don't use null, use undefined instead.
    One of them is dispensable, and undefined is inevitable.

  6. Avoid new and this. Avoid instanceof.
    They can lead to very hard to find bugs. Code that speculates with "classes" is error-prone.

  7. Test for undefined using a simple if ( ! x ) ...;
    Avoid (typeof x === "undefined") expressions. Free undefined variables will be detected by strict mode. Use typeof only to test the existence of external JS library objects.

  8. Use functional inheritance.
    All other inheritance variants are error-prone expert tasks.

  9. Put any non-function code into an immediately-invoked-function-expression.
    Never use the global scope for variables and private functions.

  10. For better readability, do not use $ or _ in identifiers.
    Leave these characters to shared libraries like jQuery or underscore.

  11. Pass dependencies as function parameters.
    Try to not use global objects and functions, pass any dependency as parameter.

  12. Don't use navigator to identify browser vendors, better test for browser features.
    For example if (window.stop) window.stop(); - but don't use document.all to identify IE !

  13. Don't use arrays as maps, their length would be zero then.

  14. When using a for-in loop, wrap the loop body into a hasOwnProperty() condition.
    This makes safe that no inherited properties appear in the loop.

  15. Don't use eval().
    Code that generates other code is hard to maintain, and mostly unnecessary.

  16. Document every function, its parameters, its return.
    In an untyped language like JS this is the quickest way to express how to use code.

And, if you are not sure about whether readability and documentation (comments) are important: imagine coming back after three years and having to fix and extend your code :-)

If the intent of your code is clear, it can always be fixed to do what was intended.
If the intent is not clear, your code could be misinterpreted and the author might "lose face".

Finding out what some code was expected to do at the time of its writing is hard enough. Leave us some comments about why you are doing this or that (not about what you are doing).

Can you find more ways to say how important documentation is? Leave a comment!




Keine Kommentare: