7 ms·
One I've never thought of using before is the: isAwesome && alert("yay"); I have used the name || "no name" operator a lot and thought to myself that it looked
by fedlarm 12y ago
One I've never thought of using before is the:
isAwesome && alert("yay");
I have used the name || "no name" operator a lot and thought to myself that it looked cool on first impression. But having something like:
isAwesome() && alert("yay");
Already makes it harder to read I think. I'm curious to what others think of this one? Maybe its just because it is new to me..
- xyclos 12y agoit is also useful for cross browser debugging. having a console.log() in your code running in IE will cause problems if the console isn't actually open. If you use window.console&&console.log() you can be sure that it wont hurt your code running in IE.
- JonnieCache 12y agoI agree that it's ugly, but also in some languages you can get into trouble because of operator precedence. This is why ruby and perl have the operators `or` and `and` as well as || and &&. http://devblog.avdi.org/2010/08/02/using-and-and-or-in-ruby/ http://devblog.avdi.org/2010/08/02/using-and-and-or-in-ruby/
- byerley 12y agoI don't think I've ever misread that style (usually the function being called makes it obvious), but it relies on short-circuit evaluation which doesn't carry over to a lot of languages - http://en.wikipedia.org/wiki/Short-circuit_evaluation http://en.wikipedia.org/wiki/Short-circuit_evaluation C is particularly dangerous because it will normally short-circuit, but the standard doesn't require it and in rare cases the compiler will optimize it out.
- digibo 12y agoI find constructs like isAwesome() && alert("yay"); confusing as well, but in our codebase it's pretty much standard to use a simple falsy check like 'callback && callback()' for optional callbacks, but we don't use it anywhere else. To be honest, I like and use some of those, especially when writing experimental/disposable code, but otherwise I try to be gentle to less experienced developers and my future self. As of #5 - I used to do and love this when writing unit tests. At the end of the work day, if I'm in the middle of implementing a feature, I'd commit and then write the next unit test (inevitably) sitting in my head and leave it failing. Really helps when it's Monday.