04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 20: Best Practices<br />

Another part of readability is comments. In most programming languages, it ’ s an accepted practice to<br />

comment each method. Due to JavaScript ’ s ability to create functions at any point in the code, this is<br />

often overlooked. It is perhaps even more important to document each function in JavaScript because of<br />

this. Generally speaking, the places that should be commented in your code are as follows:<br />

❑<br />

❑<br />

❑<br />

❑<br />

Functions and methods — Each function or method should include a comment that describes<br />

its purpose and possibly the algorithm being used to accomplish the task. It ’ s also important to<br />

state assumptions that are being made, what the arguments represent, and whether or not the<br />

function returns a value (since this is not discernible from a function definition).<br />

Large sections of code — Multiple lines of code that are all used to accomplish a single task<br />

should be preceded with a comment describing the task.<br />

Complex algorithms — If you ’ re using a unique approach to solve a problem, explain how you<br />

are doing it as a comment. This will not only help others who are looking at your code, but will<br />

also help you the next time you look at it.<br />

Hacks — Because of browser differences, JavaScript code typically contains some hacks. Don ’ t<br />

assume that someone else who is looking at the code will understand the browser issue that<br />

such a hack is working around. If you need to do something differently because one of the<br />

browsers can ’ t use the normal way, put that in a comment. It reduces the likelihood that<br />

someone will come along, see your hack, and “ fix ” it, inadvertently introducing the bug that<br />

you had already worked around.<br />

Indentation and comments create more readable code that is easier to maintain in the future.<br />

Variable and Function Naming<br />

The proper naming of variables and functions in code is vital to making it understandable and<br />

maintainable. Since many JavaScript developers began as hobbyists, there ’ s a tendency to use<br />

nonsensical names such as “ foo ” and “ bar ” for variables and names such as “ doSomething ” for<br />

functions. A professional JavaScript developer must overcome these old habits to create maintainable<br />

code. General rules for naming are as follows:<br />

❑<br />

❑<br />

❑<br />

Variable names should be nouns such as car or person .<br />

Function names should begin with a verb such as getName() . Functions that return Boolean<br />

values typically begin with is , as in isEnabled() .<br />

Use logical names for both variables and functions, without worrying about the length. Length<br />

can be mitigated through post - processing and compression (discussed later).<br />

It ’ s imperative to avoid useless variable names that don ’ t indicate the type of data they contain. With<br />

proper naming, code reads like a narrative of what is happening, making it easier to understand.<br />

Variable Type Transparency<br />

Since variables are loosely typed in JavaScript, it is easy to lose track of the type of data that a variable<br />

should contain. Proper naming mitigates this to some point, but it may not be enough in all cases. There<br />

are three ways to indicate the data type of a variable.<br />

The first way is through initialization. When a variable is defined, it should be initialized to a value that<br />

indicates how it will be used in the future. For example, a variable that will hold a Boolean should be<br />

637

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!