Are empty true blocks an idiom in JavaScript?

I just typed in a code that looks like this:

if (foo == "bar"){}else{ } 

Is there a good reason someone would write it like this rather than

 if (foo != "bar") { } 

or I'm just dealing with a crazy crazy (this is my guess based on other things in the code).

+8
javascript
source share
7 answers

If one of my developers wrote the code this way, I would drop it back with a reprimand and a copy of "Javascript: The Good Parts". I can not come up with any good reason for this.

In addition, they should have written their comparison as if (foo === "bar") . Much better.

Edit a year and a half later:

Out of boredom, I knocked out jsperf just to see if there is a noticeable performance difference between the two methods shown in the OP, and [SPOILER WARNING] is not. Not.

http://jsperf.com/empty-blocks-in-if-else-statement

+3
source share

[change]

There is no such agreement in the JavaScript community. This is just bad code.

[Original "Answer" below]

I prefer to use the following syntax when I leave the project:

 if (foo == "bar") { /* 100 or so spaces */ } else { } 

Segment } else { , I hope, is hidden from the screen by text editors so that the code does the exact opposite of what it seems. In this way, I can guarantee that the remaining development team curses my name and will never consider me for future development or support. = D

+11
source share

I don’t understand why JavaScript code should be different in this respect from any C-heritage language. I have not seen this idiom before, and I really don't like it. I need to mentally disassemble the thing twice to make sure that I understand.

The only analogue I can think of is the empty default value in the switch statement, with a plentiful comment to say, β€œI thought about it, and that is just fine.”

+4
source share

I have done this before.

That's when I want to add some debuggers later.

Yes of course he could do

 if (foo != "bar"){ //something }else{} 

But isn't that the same thing?


Return to the code you saw.

So the programmer may have done this:

 if (foo == "bar"){} else{/*something*/} 

Then later, when he wanted to add some debugging information to the 1st part, he would.
Logic still works, and it is not mistaken to a lesser extent. (in my opinion)

+3
source share

This is confusing for two reasons. The syntax is confusing because it evaluates foo == bar just to do ... nothing? It seems redundant not to use the syntax you proposed. Visually, this is also confusing, because the empty block and the if evaluation are on the same line, so if I read this code later, I could be silent about things and read the statement read as

 if (foo == "bar"){ } 

Which is the exact opposite of code intent. One possible explanation for this is that the programmer decided to go back and implement some code for foo == bar, but either did not do it or forgot to do it.

My vote is crazy.

+2
source share

There is no good reason for this.

However, note that loops sometimes happen when an empty block may be acceptable:
 var i; for (i = 0; document.getElementById("box" + i).value != ""; i++) { } // do something with i 
+1
source share

I tend to write the if chain as follows:

 if (a) { } else if (b) { // do something } else if (c) { // do something } else if (d) { // do something } 

Instead

 if (!a) { if (b) { // do something } else if (c) { // do something } else if (d) { // do something } } 

This makes the code more accurate IMO.

Does it make sense?

0
source share

All Articles