Wednesday, August 01, 2007

summary or simplification of javascript coding style

summary or simplification of javascript coding style:

1. keys of keys:

a. declare variables in the beginning of the function (like old C style)
(i) this can be important to avoid confusion. Javascript variable has no block scope
---------however, I must add that this is a uphill battle with Java (and now some C# people who have really reinvented themselves from VB6 -- this is a very good interview question for C# people -- if they worked in C# for a while, then, they know; otherwise, they will still use VB6 style) guys. I myself cannot do it. So, I will not do it. I will keep it in mind, but I will do it java style (and modern C++ style also)-- i.e., when I use it, I declare it. I do not declare it before I use it. ---- this actually can help prevent from declaring a variable -- now, it is easy, I always var it; otherwise, I have to check whether I var-ed it or not in the beginning of the function.

b. First line does not start from a new line
(i) this is to avoid semicolon insertion
(ii) which can create the "return null problem" (return null when the value is not the same line with "return")

update: I found another good one: always put "," at the beginning of the line for array elements. This way, you will not forget to add it or remove it when cut/paste. Forget removing ',' is especially bad, becuase firefox is fine, but IE will freak out -- long after you have done the coding!

2. Keys:

a. === and !==: use them most of the time, instead of == and != (also: use the old c trick, 3 === myVarv ------ update: forget about it; does not work! Also, it seems that it is not intuitive, against rule of literate programming. Further, the "===" has a strong psychological effect, you will not use "=" within if anymore! ---- also, within if etc, there should never be valid "=". All those can eliminate the problem -- hepefully).

(i) It is almost always better to use the === and !== operators. The == and != operators do type coercion. In particular, do not use == to compare against falsy values.
(ii) note: if you use ==, if one of them is a number, then, if the other is a string, the string will be changed to a number.

b. + sign as parseInt (+ is necessary because other wise it will be a string if one of them is string!)
total = subtotal + +myInput.value;
is better written as
total = subtotal + (+myInput.value);

3. You must do, but you will remember when you need them:

a. use object.hasOwnProperty(variable) to get the "true members"

for (variable in object) { if (object.hasOwnProperty(variable)) { statements; } }

b. "do" block: it needs ";" (unlike while or for)
(this is important only because we must not forget the ;
because inserting ; by compliler is bad thing -- see above return null issue)

do { statements; } while (condition);

c. "with Statement" -- never use it!

The with statement should not be used.
use a variable, and assign the namespace/class name to the variable:

var o = ooo.eee.oo.ah_ah.ting.tang.walla.walla; = true;
o.bang = true;

4. Nice to do:

a. No m_ or _ etc. for private members
---- because javascript has no such concept.
---- sse the right provate mechanism, so that all are provate except the "returned".

b. Braces should be used around all statements as long as it is a compound statement, do not use the one-line version (i.e. no brace version), even for single statements, when they are part of a control structure, such as an if or for statement. This makes it easier to add statements without accidentally introducing bugs.

c. indent 4 spaces, do not use tab.

d. do not use "continue Statement" because it tends to obscure the control flow of the function.


Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home