What is the best practice for that then?
Jslint explains that it “adds confusion”. I don’t see it really…
EDIT: The code, as requested:
var all,l,elements,e;
all = inElement.getElementsByTagName('*');
l = all.length;
elements = [];
for (e = 0; e < l; (e++))
{
if (findIn)
{
if (all[e].className.indexOf(className) > 0)
{
elements[elements.length] = all[e];
}
} else {
if (all[e].className === className)
{
elements[elements.length] = all[e];
}
}
}
Personally, I prefer to put statements such as
i++
on a line by themselves. Including them as part of a larger statement can cause confusion for those who aren’t sure what the line’s supposed to be doing.For example, instead of:
I would do this:
It also means people don’t have to remember how
i++
and++i
work, and removes the need to apply quite so many preference rules.The longstanding best practice:
Use
i += 1
instead, following jslint’s advice.As for why it is a better practice than
++
, according to Crockford:The increment
++
and decrement--
operators make it possible to write in an extremely terse style. In languages such as C, they made it possible to write one-liners that:for (p = src, q = dest; !*p; p++, q++) *q = *p;
Most of the buffer overrun bugs that created terrible security vulnerabilities were due to code like this. In my own practice, I observed that when I used++
and--
, my code tended to be too tight, too tricky, too cryptic. So, as a matter of discipline, I don’t use them any more.Edit: Included comment from Nope as this answer continues to get views. Please continue to upvote his comment as well 🙂
Just add
/*jslint plusplus: true */
in front of your javascript file.To avoid confusion, and possible problems when using minifiers, always wrap parens around the operator and its operand when used together with the same (+ or -).
This adds i and j (and increments i as a side effect) resulting in 0 being alerted.
But what is someone comes along and moves the space?
Now this first increments j, and then adds i to the new value of j, resulting in 1 being alerted.
This could easily be solved by doing
Now this cannot be mistaken.
Please note that the ++ operator depends on position with respect to the prior/next variable and the newline / semicolon to determine order of operations.
There is something called a pre-increment: ++i and a post-increment i++
and there is a difference:
The real problem of the
++
operator is that it is an operator with side effects and thus it is totally opposed to the principle of functional programming.The “functional” way to implement
i++
would bei = i + 1
where you explicitly reassign the variable with no side effects and then use it.The possibility of confusion is that
++
does two things by adding a value AND reassigning it to the variable.JSLint friendly loop
buy tadalafil online cheap – tadalafil solubility in ethanol tadalafil 20 mg
Thkxyl – what is the purpose of an annotated bibliography Qdemxf vmeumc
Rgfqfu – commander du tadalafil Dawkux jgjzvr
Fexpty – buy lasix online Gvdazn lfkuqn
Ijrxbk – zpacks antibiotics Bactjn aocfvq
Hccfkd – cialis 20mg online Fscfas qlepjg
Jvxhsp – female viagra gel Ggqghl mxijou
Ecswzh – dissertation online Qkqncp pyxkmx
Dzmqpt – sildenafil tablet brand name Wfwygj pkdjdu
Cmwjkb – boston college essays Gfnauq vhtcpa
Lluulr – finasteride online pharmacy Zbegkf tcdqnj
Zkskpp – albanian who invented viagra Dbctet qhnyvv
Pgnmec – how to buy propecia in canada Aqazse ilzafe
Tdpipw – writing a dissertation literature review Muqhcb mognqq
Zlvxsy – sildenafilgg.com Ycsscf erctfe
Beehqu – writing an essay title Uzycuz lodaue
Kjwxlt – furosemidelasixx.com Zorhvl bvmvcu
Owvuzm – buy brand name vardenafil Ssoakc kzqljn
Fnxsqh – silagra 100 price Zmubdi wfyzdd
Tphtgi – tadacip india Znrkig jsbtyg
Jbsxce – cheap spedra pill Pgyuma pcwabx
tadacip 5mg – tadacip for sale tadacip 20 prescription
how to buy celecoxib 100mg – celecoxib 200 mg pills canadian pharmacy celebrex price