Web Development with jQuery®

(Elliott) #1

Programming Conventions (^) ❘ 21
Compare the preceding to the next convention, which is known as Allman Style, which is the default
in Microsoft Visual Studio:
if (condition)
{
something = 1 ;
}
else if (another)
{
something = 2 ;
}
else
{
something = 3 ;
}
In Allman Style, all the curly braces line up in the source code, which makes it easier to detect when
one is missing, in addition to preventing typos like missing curly braces from occurring in the fi rst
place because you have a visual aid for their placement. It also lends itself nicely to having more
space between lines of code, making things easier to read.
When function calls, like window.open in the example, are long, sometimes the function call is bro-
ken up over multiple lines to make it easier to read. To the browser,
window.open(
this.href,
"picture",
"scrollbars=no,width=300,height=280,resizable=yes"
);
and
window.open(this.href, "picture", "scrollbars=no,width=300,height=280,resizable=
yes");
are exactly the same. The former example just makes it easier for humans to parse the arguments
present in the function call.
Sometimes these two conventions are mixed to form a third convention, which is known as the One
Tr ue Brace convention. This convention is defi ned in the Coding Standards Guidelines for PHP’s
PEAR repository.
window.onload = function()
{
var nodes = document.getElementsByTagName('a');
for (var counter = 0 , length = nodes.length; counter < length; counter++) {
nodes[counter].onclick = function(event) {
window.open(
this.href,
"picture",
"scrollbars=no,width=300,height=280,resizable=yes"
);
http://www.it-ebooks.info

Free download pdf