![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
if (someStuff != null && someStuff != "") [...] Will JS evaluate the whole konjunction or will it be intelligent enough to stop as the first partial condition fails? Is it depending on the platform used? |
#3
| |||
| |||
|
|
In the code at our company i see the following. if (someStuff != null) { if (someStuff != "") { doThatThing (); } } While it's fully correct and valid, i'd like to rewrite it as follows. if (someStuff != null && someStuff != "") doThatThing (); |
|
I know that some languages will evaluate the first condition of the conjunction and, provided that it fails, conclude that the statement is not to be performed. I'd like to assume so in this case as well, but wishing not to show arrogance and know-better-ism, i'd like to check with more experienced programmers first. Will JS evaluate the whole konjunction or will it be intelligent enough to stop as the first partial condition fails? Is it depending on the platform used? |
#4
| |||
| |||
|
|
In the code at our company i see the following. if (someStuff != null) { if (someStuff != "") { doThatThing (); } } While it's fully correct and valid, i'd like to rewrite it as follows. if (someStuff != null && someStuff != "") doThatThing (); It is very widely considered good style when writing javascript to never omit the braces of a block statement from around the code executed in - if/else - branches. It is extremely common to have a single statement conditionally executed and then to realise that a second statement will need to be added to that branch. With the braces already in place all you do is add the statement, but without the braces you often get fooled by the indentation into adding the statement at the existing level of indentation and not seeing that only one statement is going to conditionally executed and any that follow it will be unconditionally executed. The usual result is a confusing and hard to track down bug. |

|
Will JS evaluate the whole konjunction or will it be intelligent enough to stop as the first partial condition fails? Is it depending on the platform used? The language specification requires short circuiting, andno implementations have been observed to fail to correctlyimplement the specification in this regard. However, your simplification looks like it probably stillfalls short of an optimum outcome. snippage An alternative test could be: if (someStuff) { doThatThing (); } |

#5
| |||
| |||
|
|
if (someStuff != null && someStuff != "") doThatThing (); It is very widely considered good style when writing javascript to never omit the braces of a block statement from around the code executed in - if/else - branches. |
#6
| |||
| |||
|
|
Henry wrote in news:18 ... @d77g2000hsb.googlegroups.com: ^^^^^^^^^^^^ ^^^ if (someStuff != null && someStuff != "") doThatThing (); It is very widely considered good style when writing javascript to never omit the braces of a block statement from around the code executed in - if/else - branches. Bullshit. snip Killing all google groups posts ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
#7
| |||||||
| |||||||
|
|
In the code at our company i see the following. if (someStuff != null) { if (someStuff != "") { doThatThing (); } } While it's fully correct and valid, i'd like to rewrite it as follows. if (someStuff != null && someStuff != "") doThatThing (); It is very widely considered good style when writing javascript to never omit the braces of a block statement from around the code executed in - if/else - branches. It is extremely common to have a single statement conditionally executed and then to realise that a second statement will need to be added to that branch. With the braces already in place all you do is add the statement, but without the braces you often get fooled by the indentation into adding the statement at the existing level of indentation and not seeing that only one statement is going to conditionally executed and any that follow it will be unconditionally executed. The usual result is a confusing and hard to track down bug. I forgot to add the braces, otherwise i'm always putting them in by copmany requirement. The extra pointers and suggestions are ALWAYS appreciated, of course. Thanks! With risk of getting into something very nasty - the argument about braces "already there when one needs them" doesn't hold, in my opinion. 1. At some point of time one needs to put them in so if it happens frequently that one'll need to put more things there, it's not MORE work to do so. At best, not much less (or much less, when lucky). |
|
2. Unless one isn't a hard-core-never-seen-anything-else Python programmer, one shouldn't confuse indentation and scoping. |
|
At least i've never had that problem but perhaps i'm a genius, i can admit. ![]() |
|
Please enlighten me if i'm missing anything and please keep in mind that the above are only my personal views, hence subject to change, if needed. |
|
Will JS evaluate the whole konjunction or will it be intelligent enough to stop as the first partial condition fails? Is it depending on the platform used? The language specification requires short circuiting, andno implementations have been observed to fail to correctlyimplement the specification in this regard. However, your simplification looks like it probably stillfalls short of an optimum outcome. snippage An alternative test could be: if (someStuff) { doThatThing (); } To be perfectly sure i got it right - i can skip the test in the original post and simply test "is someStuff the case"?! |
|
That would be awsomely simplifying! |
|
In fact, that could just make me appreciate JavaScript, hehe. ![]() |
#8
| |||
| |||
|
|
On May 22, 1:47 pm, rf wrote: Henry wrote in news:18 ... @d77g2000hsb.googlegroups.com: ^^^^^^^^^^^^ ^^^ if (someStuff != null && someStuff != "") doThatThing (); It is very widely considered good style when writing javascript to never omit the braces of a block statement from around the code executed in - if/else - branches. Bullshit. snip Killing all google groups posts ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Hypocrisy. |
#9
| |||
| |||
|
|
Henry wrote: rf wrote: Henry wrote: snip It is very widely considered good style when writing javascript to never omit the braces of a block statement from around the code executed in - if/else - branches. Bullshit. snip This one I could not resist replying to as it has no foundation at all. |
#10
| |||||
| |||||
|
|
I forgot to add the braces, otherwise i'm always putting them in by copmany requirement. The extra pointers and suggestions are ALWAYS appreciated, of course. Thanks! With risk of getting into something very nasty - the argument about braces "already there when one needs them" doesn't hold, in my opinion. 1. At some point of time one needs to put them in so if it happens frequently that one'll need to put more things there, it's not MORE work to do so. At best, not much less (or much less, when lucky). The amount of work isn't really the point. It is about avoiding introducing bugs. There is a very high correlation between the use of braces in javascript source code and (sensible) indentation, such that it becomes very easy to see indentation and mentally imply the braces. Most of the time that would not be a problem because there braces would actually be there, and so that re-enforces the subconscious assumption that they will be there. And so on the relatively rare occasions were there is indentation without braces it becomes very easy to make the addition to the indented code and never observe that there were no braces around it. |
|
2. Unless one isn't a hard-core-never-seen-anything-else Python programmer, one shouldn't confuse indentation and scoping. Javascript is not block scoped so indentation and scopingare not that related most of time. |
|
But didn't you say that you would normally include thebraces by "company requirement"? That means you will nothave much opportunity to make the mistakes that occur whenthey are missing. |
|
Please enlighten me if i'm missing anything and please keep in mind that the above are only my personal views, hence subject to change, if needed. If all else fails time will enlighten you. |

|
Where does this go in the next decade? If simple things are already being ladled as awe inspiring what superlatives are going to be left for the things that are really worth pointing out? |
![]() |
| Thread Tools | |
| Display Modes | |
| |