views:

309

answers:

3

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];
        }
    }
  }
+2  A: 

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:

value = func(i++ * 3);

I would do this:

value = func(i + 3);
i++;

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.

Samir Talwar
+3  A: 

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 -).

var i = 0, j = 0;
alert(i++ +j);

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?

var i = 0, j = 0;
alert(i+ ++j);

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

var i = 0, j = 0;
alert((i++) +j); 

Now this cannot be mistaken.

Sean Kinsey
Thanks for explaining. I tried putting the in (e++) instead of e++, but JSLint persists.
WebDevHobo
Ok, then you just need to set `plusplus: false` in your jslint options. That should disable that check.
Sean Kinsey
+3  A: 

or just do i += 1

unomi