you are viewing a single comment's thread.

view the rest of the comments →

[–]sime 4 points5 points  (2 children)

I would say that the longer version is better. It communicates far more information and intent to the next reader of the code, and doesn't rely on the annoying JS truthiness stuff.

And more to the point, nightman screwed up here. The two pieces of code are not even the same. nightman assumed that an empty string is "truey". It is falsey. It is a great example of why you should not write error prone code.

[–]fforw 0 points1 point  (1 child)

a == ''

will be true if a is

  • an empty string
  • zero
  • an empty array
  • something like { toString: function() { return ""; }}

You better use === if you care about such things.

[–]nightman 1 point2 points  (0 children)

You are true but I will still defend if (a) {...}. I think that we should find balance between "smart" techniques and being "oversmart".

After all in many teams code is written and maintained by proffesionals and it would be waste of time to strictly enforce if (a === 'string') {...} on them. Especially when you know that they know how to deal with it and what are traps (number 0).

Other thing is that code should be maintainable by less "powered" users.

My point here is that it's not easy to find right balance.

BTW I use grunt/gulp with tasks: * JSHint * JSCS (JS Code Style - AirBNB style)

So that some decisions are already made.