Javascript Code Styling

D

Dr John Stockton

JRS: In article <[email protected]>, dated Sat, 30
Sep 2006 09:38:16 remote, seen in John G
Harris said:
Putting the opening bracket on the same line saves paper. If you're a
publisher then saving paper reduces costs and increases profits.

Programmers aren't publishers unless they are writing a textbook. Each
open curly bracket is matched by a close curly bracket, and they can be
a long way apart. It helps if it is easy to see which open bracket is
matched by which close bracket.

One actually needs to see how long the block is. The indentation tells
one that.

During editing, of course, discrepancies may occur. In that case,
indentation indicates likely intent; or, if an auto-indenting tool is
used, discrepancies between indentation and intent show where
corresponding { } errors occur.

When editing, especially when that is combined with re-composing, it is
useful to have the beginning and end of a block not too far apart. Two
things help with that : not putting too much code in a block, and not
wasting vertical space.
 
I

Ian Collins

John said:
Programmers aren't publishers unless they are writing a textbook. Each
open curly bracket is matched by a close curly bracket, and they can be
a long way apart. It helps if it is easy to see which open bracket is
matched by which close bracket.
If they are that far apart, your function is too long and should be
broken down.
 
L

Laurent Bugnion

Hi,

Hi Laurent,

Thanks for the reply. Is this the only situation you found that is a
problem or are there other exceptions to making Hungarian notation work
in JavaScript?

Yes, if the other guideline is respected, which says that a variable
must only contain one data type during its whole life. Note however that
as usual with guideline, there are exceptions, which should be duly
documented in the code.

Is there a URL for your guidelines?

Unfortunately, this is proprietary work, so I am not supposed to publish
it. But I will ask and maybe post it on my website if I get the approval.
Thanks,
Peter

Greetings,
Laurent
 
J

Julian Turner

I played around with Hungarian notation in JavaScript and found it is
not a perfect fit because JavaScript is not strongly typed. For
example, when a function can take a variety of argument types and 'n'
is the prefix for nodes

function highlightElement(nElement) {
if (typeof nElement === "string) {nElement =
document.getElementById(nElement);}
nElement.style.background = "yellow";
}

So three out of five of these uses of "nElement" are for the case where
it is really strElement. The 'n' really specifes the type that the
function will convert to and use when it does its real work. So
although there is still meaning to the 'n', Hungarian notation doesn't
seem like a perfect fit.

Peter


Personally, I tend to use a "v" prefix, i.e. vElement, to identify that
the type of the parameter may "vary".

Regards

Julian
 
J

John G Harris

Ian Collins said:
If they are that far apart, your function is too long and should be
broken down.

Become famous! Tell us how to shorten a switch statement :)

The fact is that sometimes the code's job is to do one darn thing after
another. Sometimes the individual steps in the job are so simple that
turning them into functions would make things less clear. Note the word
'sometimes' : judgement is needed here.

John
 
I

Ian Collins

John said:
Become famous! Tell us how to shorten a switch statement :)
Avoid it? There's an exception that proves every rule!
The fact is that sometimes the code's job is to do one darn thing after
another. Sometimes the individual steps in the job are so simple that
turning them into functions would make things less clear. Note the word
'sometimes' : judgement is needed here.
Very true.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Mon, 2
Oct 2006 21:23:40 remote, seen in John G
Harris said:
Become famous! Tell us how to shorten a switch statement :)

X = 1

switch (X) {
case 0 : alert(00) ; break
case 1 : alert(11) ; break
case 2 : alert(22) ; break
}

function f0() { alert(000) }
function f1() { alert(111) }
function f2() { alert(222) }
[f0, f1, f2][X]()


The two pieces of code are of similar character-count. But the
switching-part is shorter in the second; especially so when alert(x)
is replaced by longer code. The maximum length of the statements/blocks
in the second is much shorter than in the first.
 
J

John G Harris

Dr John Stockton said:
JRS: In article <[email protected]>, dated Mon, 2
Oct 2006 21:23:40 remote, seen in John G
Harris said:
Become famous! Tell us how to shorten a switch statement :)

X = 1

switch (X) {
case 0 : alert(00) ; break
case 1 : alert(11) ; break
case 2 : alert(22) ; break
}

function f0() { alert(000) }
function f1() { alert(111) }
function f2() { alert(222) }
[f0, f1, f2][X]()


The two pieces of code are of similar character-count. But the
switching-part is shorter in the second; especially so when alert(x)
is replaced by longer code. The maximum length of the statements/blocks
in the second is much shorter than in the first.

But it's also a proof-reading and maintenance nightmare, so it's not a
solution.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,170
Latest member
Andrew1609
Top