What is the purpose of an 'if (0)' block in if-else block?











up vote
43
down vote

favorite
2












My question is about the line I have mentioned in the subject and which I can see in many places inside the production code.



The overall code looks like this:



if (0) {
// Empty braces
} else if (some_fn_call()) {
// actual code
} else if (some_other_fn_call()) {
// another actual code
...
} else {
// default case
}


The other branches are irrelevant to my question. I'm wondering what the meaning of putting if (0) here is. The braces are empty, so I don't think that it is supposed to comment some block of code. Does it force the compiler to make some optimization or are its intentions different?



I have tried to search for this explicit case here on SO and on the internet, but with no success. There're similar questions about JavaScript, but not C. There's another question, What happens when a zero is assigned in an `if` condition?, but it discusses zero assignment to a variable, not the 'if (0)' usage itself.










share|improve this question









New contributor




Zzaponka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    zero in if condition is making the condition to fail & move control to else part
    – jasinth premkumar
    yesterday






  • 9




    @jasinthpremkumar, yes, it is clear to me that the branch with false inside will be never executed, but there's nothing to run anyway. I mean, why we just can't delete this and start with first 'else if'? Thanks.
    – Zzaponka
    yesterday






  • 19




    @buræquete: No, that's not a duplicate at all.
    – M Oehm
    yesterday






  • 7




    Just looks like a misguided attempt at "consistent style" to me. A bit like pointlessly aligning = operators for initializers.
    – Toby Speight
    yesterday






  • 3




    @TobySpeight Aligning = is not pointless. It is not uncommon for code to be difficult to read when there are many lines in a row with non-aligned = which are made easily readable with. Obviously it can be abused, and there are cases where it can reduce readability, but done correctly it is good practice and increases readability.
    – Aaron
    yesterday















up vote
43
down vote

favorite
2












My question is about the line I have mentioned in the subject and which I can see in many places inside the production code.



The overall code looks like this:



if (0) {
// Empty braces
} else if (some_fn_call()) {
// actual code
} else if (some_other_fn_call()) {
// another actual code
...
} else {
// default case
}


The other branches are irrelevant to my question. I'm wondering what the meaning of putting if (0) here is. The braces are empty, so I don't think that it is supposed to comment some block of code. Does it force the compiler to make some optimization or are its intentions different?



I have tried to search for this explicit case here on SO and on the internet, but with no success. There're similar questions about JavaScript, but not C. There's another question, What happens when a zero is assigned in an `if` condition?, but it discusses zero assignment to a variable, not the 'if (0)' usage itself.










share|improve this question









New contributor




Zzaponka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    zero in if condition is making the condition to fail & move control to else part
    – jasinth premkumar
    yesterday






  • 9




    @jasinthpremkumar, yes, it is clear to me that the branch with false inside will be never executed, but there's nothing to run anyway. I mean, why we just can't delete this and start with first 'else if'? Thanks.
    – Zzaponka
    yesterday






  • 19




    @buræquete: No, that's not a duplicate at all.
    – M Oehm
    yesterday






  • 7




    Just looks like a misguided attempt at "consistent style" to me. A bit like pointlessly aligning = operators for initializers.
    – Toby Speight
    yesterday






  • 3




    @TobySpeight Aligning = is not pointless. It is not uncommon for code to be difficult to read when there are many lines in a row with non-aligned = which are made easily readable with. Obviously it can be abused, and there are cases where it can reduce readability, but done correctly it is good practice and increases readability.
    – Aaron
    yesterday













up vote
43
down vote

favorite
2









up vote
43
down vote

favorite
2






2





My question is about the line I have mentioned in the subject and which I can see in many places inside the production code.



The overall code looks like this:



if (0) {
// Empty braces
} else if (some_fn_call()) {
// actual code
} else if (some_other_fn_call()) {
// another actual code
...
} else {
// default case
}


The other branches are irrelevant to my question. I'm wondering what the meaning of putting if (0) here is. The braces are empty, so I don't think that it is supposed to comment some block of code. Does it force the compiler to make some optimization or are its intentions different?



I have tried to search for this explicit case here on SO and on the internet, but with no success. There're similar questions about JavaScript, but not C. There's another question, What happens when a zero is assigned in an `if` condition?, but it discusses zero assignment to a variable, not the 'if (0)' usage itself.










share|improve this question









New contributor




Zzaponka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











My question is about the line I have mentioned in the subject and which I can see in many places inside the production code.



The overall code looks like this:



if (0) {
// Empty braces
} else if (some_fn_call()) {
// actual code
} else if (some_other_fn_call()) {
// another actual code
...
} else {
// default case
}


The other branches are irrelevant to my question. I'm wondering what the meaning of putting if (0) here is. The braces are empty, so I don't think that it is supposed to comment some block of code. Does it force the compiler to make some optimization or are its intentions different?



I have tried to search for this explicit case here on SO and on the internet, but with no success. There're similar questions about JavaScript, but not C. There's another question, What happens when a zero is assigned in an `if` condition?, but it discusses zero assignment to a variable, not the 'if (0)' usage itself.







c if-statement






share|improve this question









New contributor




Zzaponka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Zzaponka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday









Boann

36.5k1286119




36.5k1286119






New contributor




Zzaponka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









Zzaponka

32737




32737




New contributor




Zzaponka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Zzaponka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Zzaponka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1




    zero in if condition is making the condition to fail & move control to else part
    – jasinth premkumar
    yesterday






  • 9




    @jasinthpremkumar, yes, it is clear to me that the branch with false inside will be never executed, but there's nothing to run anyway. I mean, why we just can't delete this and start with first 'else if'? Thanks.
    – Zzaponka
    yesterday






  • 19




    @buræquete: No, that's not a duplicate at all.
    – M Oehm
    yesterday






  • 7




    Just looks like a misguided attempt at "consistent style" to me. A bit like pointlessly aligning = operators for initializers.
    – Toby Speight
    yesterday






  • 3




    @TobySpeight Aligning = is not pointless. It is not uncommon for code to be difficult to read when there are many lines in a row with non-aligned = which are made easily readable with. Obviously it can be abused, and there are cases where it can reduce readability, but done correctly it is good practice and increases readability.
    – Aaron
    yesterday














  • 1




    zero in if condition is making the condition to fail & move control to else part
    – jasinth premkumar
    yesterday






  • 9




    @jasinthpremkumar, yes, it is clear to me that the branch with false inside will be never executed, but there's nothing to run anyway. I mean, why we just can't delete this and start with first 'else if'? Thanks.
    – Zzaponka
    yesterday






  • 19




    @buræquete: No, that's not a duplicate at all.
    – M Oehm
    yesterday






  • 7




    Just looks like a misguided attempt at "consistent style" to me. A bit like pointlessly aligning = operators for initializers.
    – Toby Speight
    yesterday






  • 3




    @TobySpeight Aligning = is not pointless. It is not uncommon for code to be difficult to read when there are many lines in a row with non-aligned = which are made easily readable with. Obviously it can be abused, and there are cases where it can reduce readability, but done correctly it is good practice and increases readability.
    – Aaron
    yesterday








1




1




zero in if condition is making the condition to fail & move control to else part
– jasinth premkumar
yesterday




zero in if condition is making the condition to fail & move control to else part
– jasinth premkumar
yesterday




9




9




@jasinthpremkumar, yes, it is clear to me that the branch with false inside will be never executed, but there's nothing to run anyway. I mean, why we just can't delete this and start with first 'else if'? Thanks.
– Zzaponka
yesterday




@jasinthpremkumar, yes, it is clear to me that the branch with false inside will be never executed, but there's nothing to run anyway. I mean, why we just can't delete this and start with first 'else if'? Thanks.
– Zzaponka
yesterday




19




19




@buræquete: No, that's not a duplicate at all.
– M Oehm
yesterday




@buræquete: No, that's not a duplicate at all.
– M Oehm
yesterday




7




7




Just looks like a misguided attempt at "consistent style" to me. A bit like pointlessly aligning = operators for initializers.
– Toby Speight
yesterday




Just looks like a misguided attempt at "consistent style" to me. A bit like pointlessly aligning = operators for initializers.
– Toby Speight
yesterday




3




3




@TobySpeight Aligning = is not pointless. It is not uncommon for code to be difficult to read when there are many lines in a row with non-aligned = which are made easily readable with. Obviously it can be abused, and there are cases where it can reduce readability, but done correctly it is good practice and increases readability.
– Aaron
yesterday




@TobySpeight Aligning = is not pointless. It is not uncommon for code to be difficult to read when there are many lines in a row with non-aligned = which are made easily readable with. Obviously it can be abused, and there are cases where it can reduce readability, but done correctly it is good practice and increases readability.
– Aaron
yesterday












12 Answers
12






active

oldest

votes

















up vote
28
down vote



accepted










I sometimes use this for symmetry so I can move the other else if{ freely around with my editor without having to mind the first if.



Semantically the



if (0) {
// Empty braces
} else


part doesn't do anything and you can count on optimizers to delete it.






share|improve this answer



















  • 138




    Personal opinion: While this may be the reason code why it is written as it is, I think it's a bad justification. Code is read more often than it's written, and this unnecessary code just increases parsing overhead for the reader.
    – user694733
    yesterday








  • 9




    @user694733: You could argue that the common if else prefix to all significant code paths lines the conditions up nicely and makes scaning them easier. (That's subjective, though, and would depend a lot of what's really inside the conditions and code blocks.)
    – M Oehm
    yesterday






  • 51




    I don't think if (0) {..} introduces any parsability/readability problem. It should be obvious to anyone who knows a bit of C. That's not an issue. The problem is the follow-up question after reading it: "What the hell is it for then?" Unless it's for debugging/temporary purposes (i.e., the intention is to "enable" that if block later), I'd advocate removing altogether. Basically "reading" such code would likely cause an unnecessary "pause" for the reader for no good reason. And that's a good enough a reason to remove it.
    – P.P.
    yesterday






  • 33




    Seems like it definitely detracts from readability. It was so bad it sent that programmer to SO to ask what it was for. Not a good sign.
    – Vectorjohn
    21 hours ago






  • 12




    Even using this pattern, I don't know if you can "move else if around the editor without worry" because the conditions may not be mutually exclusive, in which case order matters. Personally I would use only if, and perform early return, extracting the logic chain to a separate function if necessary.
    – John Wu
    16 hours ago




















up vote
30
down vote













This can be useful if there are #if statements, ala



   if (0)
{
// Empty block
}
#if TEST1_ENABLED
else if (test1())
{
action1();
}
#endif
#if TEST2_ENABLED
else if (test2())
{
action2();
}
#endif


etc.



In this case, any (and all) of the tests can be #if'ed out, and the code will compile correctly. Almost all compilers will remove the if (0) {} part.
A simple autogenerator could generate code like this, as it is slightly easier to code - it doesn't have to consider the first enabled block separately.






share|improve this answer

















  • 2




    In many cases, an if/else if chain isn't used so much as a decision tree, but rather as an "act upon first matching condition" construct, where the condition that happens to have the highest-priority isn't particularly "special". While I'd not seen if(0) used as a way to allow all real branches to have consistent syntax, I like the consistent syntax it facilitates.
    – supercat
    23 hours ago










  • It’s not even useful in this case because you can achieve the same effect without: just split the else if line into two and put the preprocessor guard in between.
    – Konrad Rudolph
    10 hours ago










  • @KonradRudolph I'm not following; how would you write it?
    – JiK
    2 hours ago










  • @JiK I’d remove the if (0) branch and reformat the rest such that else is on its own line, surrounded by a guard along the lines of #if TEST1_ENABLED && TEST2_ENABLED.
    – Konrad Rudolph
    2 hours ago




















up vote
18
down vote













I've seen a similar pattern used in generated code. For example, in SQL, I've seen libraries emit the following where clause.



where 1 = 1


This presumably makes it easier to just add on other criteria, because all additional criteria can be prepended with and instead of an additional check to see if it is the first criteria or not.






share|improve this answer

















  • 2




    The 1=1 is also "useful" because you can always add the where in front, unconditionally. Otherwise you'd have to check if it's empty, and if so avoid generating the where clause.
    – Bakuriu
    22 hours ago






  • 2




    In addition, most databases will automatically "remove" the 1=1 from the WHERE, so it doesn't have an impact on performance.
    – Nic Hartley
    22 hours ago






  • 2




    This is acceptable in a library that automatically generates SQL queries that are most likely never seen even by the DevOps team. It's not "acceptable" in high-level code that has to be written and read multiple times.
    – phagio
    7 hours ago




















up vote
11
down vote













As written, the if (0) {} clause compiles out to nothing.



I suspect the function of the clause at the top of this ladder is to provide an easy place to temporarily disable all the other functionality at once (for debugging or comparison purposes) by changing the 0 to a 1 or true.






share|improve this answer




























    up vote
    8
    down vote













    Not sure of any optimizations, but my two cents :



    This happened because of some code modification, where one primary condition was removed, (the function call in initial if block, let's say), but the devs/ maintainers




    • were lazy to restructure the if-else block

    • did not want to go down on the branch coverage count


    so instead of removing the associated if block, they simply changed the condition to if(0) and moved on.






    share|improve this answer

















    • 2




      Isn't if(0) decrease branch coverage too?
      – David Szalai
      yesterday






    • 1




      @DavidSzalai Not completely - at most it will decrease by 1 (from previous 2) - but one hit will still be required for coverage, to the best of my knowledge.
      – Sourav Ghosh
      yesterday




















    up vote
    4
    down vote













    I think it's just bad code. Writing a quick example in Compiler Explorer, we see that in both gcc and clang no code is generated for the if (0) block, even with optimizations completely disabled:



    https://godbolt.org/z/PETIks



    Playing around with removing the if (0) causes no changes to the generated code, so I conclude that this is not an optimization.



    It's possible that there used to be something in the top if block which was later removed. In short, it looks like removing it would cause the exact same code to be generated, so feel free to do that.






    share|improve this answer




























      up vote
      2
      down vote













      As it's been said, the zero is evaluated to false, and the branch will likely be optimized out by the compiler.



      I've also seen this before in code where a new feature was added and a kill-switch was needed (if something goes wrong with the feature you can just turn it off), and some time later when the kill-switch was removed the programmer didn't also remove the branch, e.g.



      if (feature_a_active()) {
      use_feature_a();
      } else if (some_fn()) {
      ...


      became



      if (0) {
      // empty
      } else if (some_fn()) {
      ...





      share|improve this answer










      New contributor




      sergiopm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.

























        up vote
        0
        down vote













        I have seen this a few times, I think the most likely reason is it was evaluating something in an older/different version/branch of the code, or possibly for debugging, and changing it to if(0) is a somewhat lazy way of removing whatever was there.






        share|improve this answer




























          up vote
          0
          down vote













          I've seen non reachable code blocks in pre-expanded JavaScript that have been generated using a templating language.



          For instance, the code you are reading could have been pasted from a server that pre-evaluated the first condition that at that time relied on a variable only available on server side.



          if ( ${requestIsNotHttps} ){ ... }else if( ...


          which once pre-compiled hences :



          if ( 0 ){ ... }else if ( ...


          hope this helps you relativise the potential low keyboard activity of the pro-recycling coders era for which i manifest enthusiasm !






          share|improve this answer





















          • I agree, in the age of ubiquitous automation we should rely on autogenerated code more, as it allows us to spend more time on actual things. But for now, my exact point of interest is how this everything is architectured under the hood.
            – Zzaponka
            9 hours ago


















          up vote
          0
          down vote













          It's code rot.



          At some point that "if" did something useful, the situation changed, maybe the variable being evaluated was removed.



          The person who was fixing/changing the system did as little as possible to affect the logic of the system so he just made sure the code would recompile. So he leaves an "if(0)" because that's quick and easy and he's not totally sure that's what he wants to do. He gets the system working and he doesn't go back to fix it completely.



          Then the next developer comes along and thinks that was done deliberately and only comments out that part of the code (since it's not being evaluated anyway), then the next time the code is touched those comments are removed.






          share|improve this answer




























            up vote
            0
            down vote













            One possibility not yet mentioned: the if (0) { line could be providing a convenient spot for a breakpoint.



            Debugging is often done on non-optimised code so the always-false test will be present and able to have breakpoint set on it. When compiled for production, the line of code would be optimised out. The seemingly useless line gives functionality for development and testing builds without impacting release builds.



            There are other good suggestions above as well; the only way to really know what the purpose is, is to track down the author and ask. Your source code control system might help with that. (Look for blame-type functionality.)






            share|improve this answer




























              up vote
              -4
              down vote













              if (0) will always evaluate to false. The code in the question is functionally equivalent to



              if (some_fn_call()) {
              // actual code
              } else if (some_other_fn_call()) {
              // another actual code
              ...
              } else {
              // default case
              }


              Possibly, this occurred because of code being deleted in the first if block and there was a possibility of it being reinserted, so the if part was kept.






              share|improve this answer



















              • 14




                The question does not seek definition of the code shown, question is why is it the way it is?
                – Sourav Ghosh
                yesterday










              • I do not see any reason to code it with if (0) as shown by OP. There may be some previous code that was there and later removed with if(0)
                – Rishikesh Raje
                yesterday










              • Usually, these constructs are used to keep other conditions in similar syntax. It's an aesthetic choice.
                – Rushabh Mehta
                yesterday






              • 2




                Actually that's what i do while debugging. Instead of deleting a possibly problematic if clause, I just skip it by adding ( false && 'original boolean check'), just to make sure that if is not causing mischief. This particular answer is suggesting just that. It is a valid answer, dear down-voters please reconsider your position.
                – brett
                23 hours ago











              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });






              Zzaponka is a new contributor. Be nice, and check out our Code of Conduct.










               

              draft saved


              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298646%2fwhat-is-the-purpose-of-an-if-0-block-in-if-else-block%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              12 Answers
              12






              active

              oldest

              votes








              12 Answers
              12






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              28
              down vote



              accepted










              I sometimes use this for symmetry so I can move the other else if{ freely around with my editor without having to mind the first if.



              Semantically the



              if (0) {
              // Empty braces
              } else


              part doesn't do anything and you can count on optimizers to delete it.






              share|improve this answer



















              • 138




                Personal opinion: While this may be the reason code why it is written as it is, I think it's a bad justification. Code is read more often than it's written, and this unnecessary code just increases parsing overhead for the reader.
                – user694733
                yesterday








              • 9




                @user694733: You could argue that the common if else prefix to all significant code paths lines the conditions up nicely and makes scaning them easier. (That's subjective, though, and would depend a lot of what's really inside the conditions and code blocks.)
                – M Oehm
                yesterday






              • 51




                I don't think if (0) {..} introduces any parsability/readability problem. It should be obvious to anyone who knows a bit of C. That's not an issue. The problem is the follow-up question after reading it: "What the hell is it for then?" Unless it's for debugging/temporary purposes (i.e., the intention is to "enable" that if block later), I'd advocate removing altogether. Basically "reading" such code would likely cause an unnecessary "pause" for the reader for no good reason. And that's a good enough a reason to remove it.
                – P.P.
                yesterday






              • 33




                Seems like it definitely detracts from readability. It was so bad it sent that programmer to SO to ask what it was for. Not a good sign.
                – Vectorjohn
                21 hours ago






              • 12




                Even using this pattern, I don't know if you can "move else if around the editor without worry" because the conditions may not be mutually exclusive, in which case order matters. Personally I would use only if, and perform early return, extracting the logic chain to a separate function if necessary.
                – John Wu
                16 hours ago

















              up vote
              28
              down vote



              accepted










              I sometimes use this for symmetry so I can move the other else if{ freely around with my editor without having to mind the first if.



              Semantically the



              if (0) {
              // Empty braces
              } else


              part doesn't do anything and you can count on optimizers to delete it.






              share|improve this answer



















              • 138




                Personal opinion: While this may be the reason code why it is written as it is, I think it's a bad justification. Code is read more often than it's written, and this unnecessary code just increases parsing overhead for the reader.
                – user694733
                yesterday








              • 9




                @user694733: You could argue that the common if else prefix to all significant code paths lines the conditions up nicely and makes scaning them easier. (That's subjective, though, and would depend a lot of what's really inside the conditions and code blocks.)
                – M Oehm
                yesterday






              • 51




                I don't think if (0) {..} introduces any parsability/readability problem. It should be obvious to anyone who knows a bit of C. That's not an issue. The problem is the follow-up question after reading it: "What the hell is it for then?" Unless it's for debugging/temporary purposes (i.e., the intention is to "enable" that if block later), I'd advocate removing altogether. Basically "reading" such code would likely cause an unnecessary "pause" for the reader for no good reason. And that's a good enough a reason to remove it.
                – P.P.
                yesterday






              • 33




                Seems like it definitely detracts from readability. It was so bad it sent that programmer to SO to ask what it was for. Not a good sign.
                – Vectorjohn
                21 hours ago






              • 12




                Even using this pattern, I don't know if you can "move else if around the editor without worry" because the conditions may not be mutually exclusive, in which case order matters. Personally I would use only if, and perform early return, extracting the logic chain to a separate function if necessary.
                – John Wu
                16 hours ago















              up vote
              28
              down vote



              accepted







              up vote
              28
              down vote



              accepted






              I sometimes use this for symmetry so I can move the other else if{ freely around with my editor without having to mind the first if.



              Semantically the



              if (0) {
              // Empty braces
              } else


              part doesn't do anything and you can count on optimizers to delete it.






              share|improve this answer














              I sometimes use this for symmetry so I can move the other else if{ freely around with my editor without having to mind the first if.



              Semantically the



              if (0) {
              // Empty braces
              } else


              part doesn't do anything and you can count on optimizers to delete it.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 12 hours ago

























              answered yesterday









              PSkocik

              30.5k54368




              30.5k54368








              • 138




                Personal opinion: While this may be the reason code why it is written as it is, I think it's a bad justification. Code is read more often than it's written, and this unnecessary code just increases parsing overhead for the reader.
                – user694733
                yesterday








              • 9




                @user694733: You could argue that the common if else prefix to all significant code paths lines the conditions up nicely and makes scaning them easier. (That's subjective, though, and would depend a lot of what's really inside the conditions and code blocks.)
                – M Oehm
                yesterday






              • 51




                I don't think if (0) {..} introduces any parsability/readability problem. It should be obvious to anyone who knows a bit of C. That's not an issue. The problem is the follow-up question after reading it: "What the hell is it for then?" Unless it's for debugging/temporary purposes (i.e., the intention is to "enable" that if block later), I'd advocate removing altogether. Basically "reading" such code would likely cause an unnecessary "pause" for the reader for no good reason. And that's a good enough a reason to remove it.
                – P.P.
                yesterday






              • 33




                Seems like it definitely detracts from readability. It was so bad it sent that programmer to SO to ask what it was for. Not a good sign.
                – Vectorjohn
                21 hours ago






              • 12




                Even using this pattern, I don't know if you can "move else if around the editor without worry" because the conditions may not be mutually exclusive, in which case order matters. Personally I would use only if, and perform early return, extracting the logic chain to a separate function if necessary.
                – John Wu
                16 hours ago
















              • 138




                Personal opinion: While this may be the reason code why it is written as it is, I think it's a bad justification. Code is read more often than it's written, and this unnecessary code just increases parsing overhead for the reader.
                – user694733
                yesterday








              • 9




                @user694733: You could argue that the common if else prefix to all significant code paths lines the conditions up nicely and makes scaning them easier. (That's subjective, though, and would depend a lot of what's really inside the conditions and code blocks.)
                – M Oehm
                yesterday






              • 51




                I don't think if (0) {..} introduces any parsability/readability problem. It should be obvious to anyone who knows a bit of C. That's not an issue. The problem is the follow-up question after reading it: "What the hell is it for then?" Unless it's for debugging/temporary purposes (i.e., the intention is to "enable" that if block later), I'd advocate removing altogether. Basically "reading" such code would likely cause an unnecessary "pause" for the reader for no good reason. And that's a good enough a reason to remove it.
                – P.P.
                yesterday






              • 33




                Seems like it definitely detracts from readability. It was so bad it sent that programmer to SO to ask what it was for. Not a good sign.
                – Vectorjohn
                21 hours ago






              • 12




                Even using this pattern, I don't know if you can "move else if around the editor without worry" because the conditions may not be mutually exclusive, in which case order matters. Personally I would use only if, and perform early return, extracting the logic chain to a separate function if necessary.
                – John Wu
                16 hours ago










              138




              138




              Personal opinion: While this may be the reason code why it is written as it is, I think it's a bad justification. Code is read more often than it's written, and this unnecessary code just increases parsing overhead for the reader.
              – user694733
              yesterday






              Personal opinion: While this may be the reason code why it is written as it is, I think it's a bad justification. Code is read more often than it's written, and this unnecessary code just increases parsing overhead for the reader.
              – user694733
              yesterday






              9




              9




              @user694733: You could argue that the common if else prefix to all significant code paths lines the conditions up nicely and makes scaning them easier. (That's subjective, though, and would depend a lot of what's really inside the conditions and code blocks.)
              – M Oehm
              yesterday




              @user694733: You could argue that the common if else prefix to all significant code paths lines the conditions up nicely and makes scaning them easier. (That's subjective, though, and would depend a lot of what's really inside the conditions and code blocks.)
              – M Oehm
              yesterday




              51




              51




              I don't think if (0) {..} introduces any parsability/readability problem. It should be obvious to anyone who knows a bit of C. That's not an issue. The problem is the follow-up question after reading it: "What the hell is it for then?" Unless it's for debugging/temporary purposes (i.e., the intention is to "enable" that if block later), I'd advocate removing altogether. Basically "reading" such code would likely cause an unnecessary "pause" for the reader for no good reason. And that's a good enough a reason to remove it.
              – P.P.
              yesterday




              I don't think if (0) {..} introduces any parsability/readability problem. It should be obvious to anyone who knows a bit of C. That's not an issue. The problem is the follow-up question after reading it: "What the hell is it for then?" Unless it's for debugging/temporary purposes (i.e., the intention is to "enable" that if block later), I'd advocate removing altogether. Basically "reading" such code would likely cause an unnecessary "pause" for the reader for no good reason. And that's a good enough a reason to remove it.
              – P.P.
              yesterday




              33




              33




              Seems like it definitely detracts from readability. It was so bad it sent that programmer to SO to ask what it was for. Not a good sign.
              – Vectorjohn
              21 hours ago




              Seems like it definitely detracts from readability. It was so bad it sent that programmer to SO to ask what it was for. Not a good sign.
              – Vectorjohn
              21 hours ago




              12




              12




              Even using this pattern, I don't know if you can "move else if around the editor without worry" because the conditions may not be mutually exclusive, in which case order matters. Personally I would use only if, and perform early return, extracting the logic chain to a separate function if necessary.
              – John Wu
              16 hours ago






              Even using this pattern, I don't know if you can "move else if around the editor without worry" because the conditions may not be mutually exclusive, in which case order matters. Personally I would use only if, and perform early return, extracting the logic chain to a separate function if necessary.
              – John Wu
              16 hours ago














              up vote
              30
              down vote













              This can be useful if there are #if statements, ala



                 if (0)
              {
              // Empty block
              }
              #if TEST1_ENABLED
              else if (test1())
              {
              action1();
              }
              #endif
              #if TEST2_ENABLED
              else if (test2())
              {
              action2();
              }
              #endif


              etc.



              In this case, any (and all) of the tests can be #if'ed out, and the code will compile correctly. Almost all compilers will remove the if (0) {} part.
              A simple autogenerator could generate code like this, as it is slightly easier to code - it doesn't have to consider the first enabled block separately.






              share|improve this answer

















              • 2




                In many cases, an if/else if chain isn't used so much as a decision tree, but rather as an "act upon first matching condition" construct, where the condition that happens to have the highest-priority isn't particularly "special". While I'd not seen if(0) used as a way to allow all real branches to have consistent syntax, I like the consistent syntax it facilitates.
                – supercat
                23 hours ago










              • It’s not even useful in this case because you can achieve the same effect without: just split the else if line into two and put the preprocessor guard in between.
                – Konrad Rudolph
                10 hours ago










              • @KonradRudolph I'm not following; how would you write it?
                – JiK
                2 hours ago










              • @JiK I’d remove the if (0) branch and reformat the rest such that else is on its own line, surrounded by a guard along the lines of #if TEST1_ENABLED && TEST2_ENABLED.
                – Konrad Rudolph
                2 hours ago

















              up vote
              30
              down vote













              This can be useful if there are #if statements, ala



                 if (0)
              {
              // Empty block
              }
              #if TEST1_ENABLED
              else if (test1())
              {
              action1();
              }
              #endif
              #if TEST2_ENABLED
              else if (test2())
              {
              action2();
              }
              #endif


              etc.



              In this case, any (and all) of the tests can be #if'ed out, and the code will compile correctly. Almost all compilers will remove the if (0) {} part.
              A simple autogenerator could generate code like this, as it is slightly easier to code - it doesn't have to consider the first enabled block separately.






              share|improve this answer

















              • 2




                In many cases, an if/else if chain isn't used so much as a decision tree, but rather as an "act upon first matching condition" construct, where the condition that happens to have the highest-priority isn't particularly "special". While I'd not seen if(0) used as a way to allow all real branches to have consistent syntax, I like the consistent syntax it facilitates.
                – supercat
                23 hours ago










              • It’s not even useful in this case because you can achieve the same effect without: just split the else if line into two and put the preprocessor guard in between.
                – Konrad Rudolph
                10 hours ago










              • @KonradRudolph I'm not following; how would you write it?
                – JiK
                2 hours ago










              • @JiK I’d remove the if (0) branch and reformat the rest such that else is on its own line, surrounded by a guard along the lines of #if TEST1_ENABLED && TEST2_ENABLED.
                – Konrad Rudolph
                2 hours ago















              up vote
              30
              down vote










              up vote
              30
              down vote









              This can be useful if there are #if statements, ala



                 if (0)
              {
              // Empty block
              }
              #if TEST1_ENABLED
              else if (test1())
              {
              action1();
              }
              #endif
              #if TEST2_ENABLED
              else if (test2())
              {
              action2();
              }
              #endif


              etc.



              In this case, any (and all) of the tests can be #if'ed out, and the code will compile correctly. Almost all compilers will remove the if (0) {} part.
              A simple autogenerator could generate code like this, as it is slightly easier to code - it doesn't have to consider the first enabled block separately.






              share|improve this answer












              This can be useful if there are #if statements, ala



                 if (0)
              {
              // Empty block
              }
              #if TEST1_ENABLED
              else if (test1())
              {
              action1();
              }
              #endif
              #if TEST2_ENABLED
              else if (test2())
              {
              action2();
              }
              #endif


              etc.



              In this case, any (and all) of the tests can be #if'ed out, and the code will compile correctly. Almost all compilers will remove the if (0) {} part.
              A simple autogenerator could generate code like this, as it is slightly easier to code - it doesn't have to consider the first enabled block separately.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 23 hours ago









              CSM

              39239




              39239








              • 2




                In many cases, an if/else if chain isn't used so much as a decision tree, but rather as an "act upon first matching condition" construct, where the condition that happens to have the highest-priority isn't particularly "special". While I'd not seen if(0) used as a way to allow all real branches to have consistent syntax, I like the consistent syntax it facilitates.
                – supercat
                23 hours ago










              • It’s not even useful in this case because you can achieve the same effect without: just split the else if line into two and put the preprocessor guard in between.
                – Konrad Rudolph
                10 hours ago










              • @KonradRudolph I'm not following; how would you write it?
                – JiK
                2 hours ago










              • @JiK I’d remove the if (0) branch and reformat the rest such that else is on its own line, surrounded by a guard along the lines of #if TEST1_ENABLED && TEST2_ENABLED.
                – Konrad Rudolph
                2 hours ago
















              • 2




                In many cases, an if/else if chain isn't used so much as a decision tree, but rather as an "act upon first matching condition" construct, where the condition that happens to have the highest-priority isn't particularly "special". While I'd not seen if(0) used as a way to allow all real branches to have consistent syntax, I like the consistent syntax it facilitates.
                – supercat
                23 hours ago










              • It’s not even useful in this case because you can achieve the same effect without: just split the else if line into two and put the preprocessor guard in between.
                – Konrad Rudolph
                10 hours ago










              • @KonradRudolph I'm not following; how would you write it?
                – JiK
                2 hours ago










              • @JiK I’d remove the if (0) branch and reformat the rest such that else is on its own line, surrounded by a guard along the lines of #if TEST1_ENABLED && TEST2_ENABLED.
                – Konrad Rudolph
                2 hours ago










              2




              2




              In many cases, an if/else if chain isn't used so much as a decision tree, but rather as an "act upon first matching condition" construct, where the condition that happens to have the highest-priority isn't particularly "special". While I'd not seen if(0) used as a way to allow all real branches to have consistent syntax, I like the consistent syntax it facilitates.
              – supercat
              23 hours ago




              In many cases, an if/else if chain isn't used so much as a decision tree, but rather as an "act upon first matching condition" construct, where the condition that happens to have the highest-priority isn't particularly "special". While I'd not seen if(0) used as a way to allow all real branches to have consistent syntax, I like the consistent syntax it facilitates.
              – supercat
              23 hours ago












              It’s not even useful in this case because you can achieve the same effect without: just split the else if line into two and put the preprocessor guard in between.
              – Konrad Rudolph
              10 hours ago




              It’s not even useful in this case because you can achieve the same effect without: just split the else if line into two and put the preprocessor guard in between.
              – Konrad Rudolph
              10 hours ago












              @KonradRudolph I'm not following; how would you write it?
              – JiK
              2 hours ago




              @KonradRudolph I'm not following; how would you write it?
              – JiK
              2 hours ago












              @JiK I’d remove the if (0) branch and reformat the rest such that else is on its own line, surrounded by a guard along the lines of #if TEST1_ENABLED && TEST2_ENABLED.
              – Konrad Rudolph
              2 hours ago






              @JiK I’d remove the if (0) branch and reformat the rest such that else is on its own line, surrounded by a guard along the lines of #if TEST1_ENABLED && TEST2_ENABLED.
              – Konrad Rudolph
              2 hours ago












              up vote
              18
              down vote













              I've seen a similar pattern used in generated code. For example, in SQL, I've seen libraries emit the following where clause.



              where 1 = 1


              This presumably makes it easier to just add on other criteria, because all additional criteria can be prepended with and instead of an additional check to see if it is the first criteria or not.






              share|improve this answer

















              • 2




                The 1=1 is also "useful" because you can always add the where in front, unconditionally. Otherwise you'd have to check if it's empty, and if so avoid generating the where clause.
                – Bakuriu
                22 hours ago






              • 2




                In addition, most databases will automatically "remove" the 1=1 from the WHERE, so it doesn't have an impact on performance.
                – Nic Hartley
                22 hours ago






              • 2




                This is acceptable in a library that automatically generates SQL queries that are most likely never seen even by the DevOps team. It's not "acceptable" in high-level code that has to be written and read multiple times.
                – phagio
                7 hours ago

















              up vote
              18
              down vote













              I've seen a similar pattern used in generated code. For example, in SQL, I've seen libraries emit the following where clause.



              where 1 = 1


              This presumably makes it easier to just add on other criteria, because all additional criteria can be prepended with and instead of an additional check to see if it is the first criteria or not.






              share|improve this answer

















              • 2




                The 1=1 is also "useful" because you can always add the where in front, unconditionally. Otherwise you'd have to check if it's empty, and if so avoid generating the where clause.
                – Bakuriu
                22 hours ago






              • 2




                In addition, most databases will automatically "remove" the 1=1 from the WHERE, so it doesn't have an impact on performance.
                – Nic Hartley
                22 hours ago






              • 2




                This is acceptable in a library that automatically generates SQL queries that are most likely never seen even by the DevOps team. It's not "acceptable" in high-level code that has to be written and read multiple times.
                – phagio
                7 hours ago















              up vote
              18
              down vote










              up vote
              18
              down vote









              I've seen a similar pattern used in generated code. For example, in SQL, I've seen libraries emit the following where clause.



              where 1 = 1


              This presumably makes it easier to just add on other criteria, because all additional criteria can be prepended with and instead of an additional check to see if it is the first criteria or not.






              share|improve this answer












              I've seen a similar pattern used in generated code. For example, in SQL, I've seen libraries emit the following where clause.



              where 1 = 1


              This presumably makes it easier to just add on other criteria, because all additional criteria can be prepended with and instead of an additional check to see if it is the first criteria or not.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered yesterday









              seth flowers

              7,27721735




              7,27721735








              • 2




                The 1=1 is also "useful" because you can always add the where in front, unconditionally. Otherwise you'd have to check if it's empty, and if so avoid generating the where clause.
                – Bakuriu
                22 hours ago






              • 2




                In addition, most databases will automatically "remove" the 1=1 from the WHERE, so it doesn't have an impact on performance.
                – Nic Hartley
                22 hours ago






              • 2




                This is acceptable in a library that automatically generates SQL queries that are most likely never seen even by the DevOps team. It's not "acceptable" in high-level code that has to be written and read multiple times.
                – phagio
                7 hours ago
















              • 2




                The 1=1 is also "useful" because you can always add the where in front, unconditionally. Otherwise you'd have to check if it's empty, and if so avoid generating the where clause.
                – Bakuriu
                22 hours ago






              • 2




                In addition, most databases will automatically "remove" the 1=1 from the WHERE, so it doesn't have an impact on performance.
                – Nic Hartley
                22 hours ago






              • 2




                This is acceptable in a library that automatically generates SQL queries that are most likely never seen even by the DevOps team. It's not "acceptable" in high-level code that has to be written and read multiple times.
                – phagio
                7 hours ago










              2




              2




              The 1=1 is also "useful" because you can always add the where in front, unconditionally. Otherwise you'd have to check if it's empty, and if so avoid generating the where clause.
              – Bakuriu
              22 hours ago




              The 1=1 is also "useful" because you can always add the where in front, unconditionally. Otherwise you'd have to check if it's empty, and if so avoid generating the where clause.
              – Bakuriu
              22 hours ago




              2




              2




              In addition, most databases will automatically "remove" the 1=1 from the WHERE, so it doesn't have an impact on performance.
              – Nic Hartley
              22 hours ago




              In addition, most databases will automatically "remove" the 1=1 from the WHERE, so it doesn't have an impact on performance.
              – Nic Hartley
              22 hours ago




              2




              2




              This is acceptable in a library that automatically generates SQL queries that are most likely never seen even by the DevOps team. It's not "acceptable" in high-level code that has to be written and read multiple times.
              – phagio
              7 hours ago






              This is acceptable in a library that automatically generates SQL queries that are most likely never seen even by the DevOps team. It's not "acceptable" in high-level code that has to be written and read multiple times.
              – phagio
              7 hours ago












              up vote
              11
              down vote













              As written, the if (0) {} clause compiles out to nothing.



              I suspect the function of the clause at the top of this ladder is to provide an easy place to temporarily disable all the other functionality at once (for debugging or comparison purposes) by changing the 0 to a 1 or true.






              share|improve this answer

























                up vote
                11
                down vote













                As written, the if (0) {} clause compiles out to nothing.



                I suspect the function of the clause at the top of this ladder is to provide an easy place to temporarily disable all the other functionality at once (for debugging or comparison purposes) by changing the 0 to a 1 or true.






                share|improve this answer























                  up vote
                  11
                  down vote










                  up vote
                  11
                  down vote









                  As written, the if (0) {} clause compiles out to nothing.



                  I suspect the function of the clause at the top of this ladder is to provide an easy place to temporarily disable all the other functionality at once (for debugging or comparison purposes) by changing the 0 to a 1 or true.






                  share|improve this answer












                  As written, the if (0) {} clause compiles out to nothing.



                  I suspect the function of the clause at the top of this ladder is to provide an easy place to temporarily disable all the other functionality at once (for debugging or comparison purposes) by changing the 0 to a 1 or true.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered yesterday









                  Russell Borogove

                  13k2938




                  13k2938






















                      up vote
                      8
                      down vote













                      Not sure of any optimizations, but my two cents :



                      This happened because of some code modification, where one primary condition was removed, (the function call in initial if block, let's say), but the devs/ maintainers




                      • were lazy to restructure the if-else block

                      • did not want to go down on the branch coverage count


                      so instead of removing the associated if block, they simply changed the condition to if(0) and moved on.






                      share|improve this answer

















                      • 2




                        Isn't if(0) decrease branch coverage too?
                        – David Szalai
                        yesterday






                      • 1




                        @DavidSzalai Not completely - at most it will decrease by 1 (from previous 2) - but one hit will still be required for coverage, to the best of my knowledge.
                        – Sourav Ghosh
                        yesterday

















                      up vote
                      8
                      down vote













                      Not sure of any optimizations, but my two cents :



                      This happened because of some code modification, where one primary condition was removed, (the function call in initial if block, let's say), but the devs/ maintainers




                      • were lazy to restructure the if-else block

                      • did not want to go down on the branch coverage count


                      so instead of removing the associated if block, they simply changed the condition to if(0) and moved on.






                      share|improve this answer

















                      • 2




                        Isn't if(0) decrease branch coverage too?
                        – David Szalai
                        yesterday






                      • 1




                        @DavidSzalai Not completely - at most it will decrease by 1 (from previous 2) - but one hit will still be required for coverage, to the best of my knowledge.
                        – Sourav Ghosh
                        yesterday















                      up vote
                      8
                      down vote










                      up vote
                      8
                      down vote









                      Not sure of any optimizations, but my two cents :



                      This happened because of some code modification, where one primary condition was removed, (the function call in initial if block, let's say), but the devs/ maintainers




                      • were lazy to restructure the if-else block

                      • did not want to go down on the branch coverage count


                      so instead of removing the associated if block, they simply changed the condition to if(0) and moved on.






                      share|improve this answer












                      Not sure of any optimizations, but my two cents :



                      This happened because of some code modification, where one primary condition was removed, (the function call in initial if block, let's say), but the devs/ maintainers




                      • were lazy to restructure the if-else block

                      • did not want to go down on the branch coverage count


                      so instead of removing the associated if block, they simply changed the condition to if(0) and moved on.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered yesterday









                      Sourav Ghosh

                      107k14129185




                      107k14129185








                      • 2




                        Isn't if(0) decrease branch coverage too?
                        – David Szalai
                        yesterday






                      • 1




                        @DavidSzalai Not completely - at most it will decrease by 1 (from previous 2) - but one hit will still be required for coverage, to the best of my knowledge.
                        – Sourav Ghosh
                        yesterday
















                      • 2




                        Isn't if(0) decrease branch coverage too?
                        – David Szalai
                        yesterday






                      • 1




                        @DavidSzalai Not completely - at most it will decrease by 1 (from previous 2) - but one hit will still be required for coverage, to the best of my knowledge.
                        – Sourav Ghosh
                        yesterday










                      2




                      2




                      Isn't if(0) decrease branch coverage too?
                      – David Szalai
                      yesterday




                      Isn't if(0) decrease branch coverage too?
                      – David Szalai
                      yesterday




                      1




                      1




                      @DavidSzalai Not completely - at most it will decrease by 1 (from previous 2) - but one hit will still be required for coverage, to the best of my knowledge.
                      – Sourav Ghosh
                      yesterday






                      @DavidSzalai Not completely - at most it will decrease by 1 (from previous 2) - but one hit will still be required for coverage, to the best of my knowledge.
                      – Sourav Ghosh
                      yesterday












                      up vote
                      4
                      down vote













                      I think it's just bad code. Writing a quick example in Compiler Explorer, we see that in both gcc and clang no code is generated for the if (0) block, even with optimizations completely disabled:



                      https://godbolt.org/z/PETIks



                      Playing around with removing the if (0) causes no changes to the generated code, so I conclude that this is not an optimization.



                      It's possible that there used to be something in the top if block which was later removed. In short, it looks like removing it would cause the exact same code to be generated, so feel free to do that.






                      share|improve this answer

























                        up vote
                        4
                        down vote













                        I think it's just bad code. Writing a quick example in Compiler Explorer, we see that in both gcc and clang no code is generated for the if (0) block, even with optimizations completely disabled:



                        https://godbolt.org/z/PETIks



                        Playing around with removing the if (0) causes no changes to the generated code, so I conclude that this is not an optimization.



                        It's possible that there used to be something in the top if block which was later removed. In short, it looks like removing it would cause the exact same code to be generated, so feel free to do that.






                        share|improve this answer























                          up vote
                          4
                          down vote










                          up vote
                          4
                          down vote









                          I think it's just bad code. Writing a quick example in Compiler Explorer, we see that in both gcc and clang no code is generated for the if (0) block, even with optimizations completely disabled:



                          https://godbolt.org/z/PETIks



                          Playing around with removing the if (0) causes no changes to the generated code, so I conclude that this is not an optimization.



                          It's possible that there used to be something in the top if block which was later removed. In short, it looks like removing it would cause the exact same code to be generated, so feel free to do that.






                          share|improve this answer












                          I think it's just bad code. Writing a quick example in Compiler Explorer, we see that in both gcc and clang no code is generated for the if (0) block, even with optimizations completely disabled:



                          https://godbolt.org/z/PETIks



                          Playing around with removing the if (0) causes no changes to the generated code, so I conclude that this is not an optimization.



                          It's possible that there used to be something in the top if block which was later removed. In short, it looks like removing it would cause the exact same code to be generated, so feel free to do that.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered yesterday









                          cha0site

                          7,79322644




                          7,79322644






















                              up vote
                              2
                              down vote













                              As it's been said, the zero is evaluated to false, and the branch will likely be optimized out by the compiler.



                              I've also seen this before in code where a new feature was added and a kill-switch was needed (if something goes wrong with the feature you can just turn it off), and some time later when the kill-switch was removed the programmer didn't also remove the branch, e.g.



                              if (feature_a_active()) {
                              use_feature_a();
                              } else if (some_fn()) {
                              ...


                              became



                              if (0) {
                              // empty
                              } else if (some_fn()) {
                              ...





                              share|improve this answer










                              New contributor




                              sergiopm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                              Check out our Code of Conduct.






















                                up vote
                                2
                                down vote













                                As it's been said, the zero is evaluated to false, and the branch will likely be optimized out by the compiler.



                                I've also seen this before in code where a new feature was added and a kill-switch was needed (if something goes wrong with the feature you can just turn it off), and some time later when the kill-switch was removed the programmer didn't also remove the branch, e.g.



                                if (feature_a_active()) {
                                use_feature_a();
                                } else if (some_fn()) {
                                ...


                                became



                                if (0) {
                                // empty
                                } else if (some_fn()) {
                                ...





                                share|improve this answer










                                New contributor




                                sergiopm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                Check out our Code of Conduct.




















                                  up vote
                                  2
                                  down vote










                                  up vote
                                  2
                                  down vote









                                  As it's been said, the zero is evaluated to false, and the branch will likely be optimized out by the compiler.



                                  I've also seen this before in code where a new feature was added and a kill-switch was needed (if something goes wrong with the feature you can just turn it off), and some time later when the kill-switch was removed the programmer didn't also remove the branch, e.g.



                                  if (feature_a_active()) {
                                  use_feature_a();
                                  } else if (some_fn()) {
                                  ...


                                  became



                                  if (0) {
                                  // empty
                                  } else if (some_fn()) {
                                  ...





                                  share|improve this answer










                                  New contributor




                                  sergiopm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                  Check out our Code of Conduct.









                                  As it's been said, the zero is evaluated to false, and the branch will likely be optimized out by the compiler.



                                  I've also seen this before in code where a new feature was added and a kill-switch was needed (if something goes wrong with the feature you can just turn it off), and some time later when the kill-switch was removed the programmer didn't also remove the branch, e.g.



                                  if (feature_a_active()) {
                                  use_feature_a();
                                  } else if (some_fn()) {
                                  ...


                                  became



                                  if (0) {
                                  // empty
                                  } else if (some_fn()) {
                                  ...






                                  share|improve this answer










                                  New contributor




                                  sergiopm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                  Check out our Code of Conduct.









                                  share|improve this answer



                                  share|improve this answer








                                  edited yesterday





















                                  New contributor




                                  sergiopm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                  Check out our Code of Conduct.









                                  answered yesterday









                                  sergiopm

                                  764




                                  764




                                  New contributor




                                  sergiopm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                  Check out our Code of Conduct.





                                  New contributor





                                  sergiopm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                  Check out our Code of Conduct.






                                  sergiopm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                  Check out our Code of Conduct.






















                                      up vote
                                      0
                                      down vote













                                      I have seen this a few times, I think the most likely reason is it was evaluating something in an older/different version/branch of the code, or possibly for debugging, and changing it to if(0) is a somewhat lazy way of removing whatever was there.






                                      share|improve this answer

























                                        up vote
                                        0
                                        down vote













                                        I have seen this a few times, I think the most likely reason is it was evaluating something in an older/different version/branch of the code, or possibly for debugging, and changing it to if(0) is a somewhat lazy way of removing whatever was there.






                                        share|improve this answer























                                          up vote
                                          0
                                          down vote










                                          up vote
                                          0
                                          down vote









                                          I have seen this a few times, I think the most likely reason is it was evaluating something in an older/different version/branch of the code, or possibly for debugging, and changing it to if(0) is a somewhat lazy way of removing whatever was there.






                                          share|improve this answer












                                          I have seen this a few times, I think the most likely reason is it was evaluating something in an older/different version/branch of the code, or possibly for debugging, and changing it to if(0) is a somewhat lazy way of removing whatever was there.







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered yesterday









                                          John U

                                          1,64511529




                                          1,64511529






















                                              up vote
                                              0
                                              down vote













                                              I've seen non reachable code blocks in pre-expanded JavaScript that have been generated using a templating language.



                                              For instance, the code you are reading could have been pasted from a server that pre-evaluated the first condition that at that time relied on a variable only available on server side.



                                              if ( ${requestIsNotHttps} ){ ... }else if( ...


                                              which once pre-compiled hences :



                                              if ( 0 ){ ... }else if ( ...


                                              hope this helps you relativise the potential low keyboard activity of the pro-recycling coders era for which i manifest enthusiasm !






                                              share|improve this answer





















                                              • I agree, in the age of ubiquitous automation we should rely on autogenerated code more, as it allows us to spend more time on actual things. But for now, my exact point of interest is how this everything is architectured under the hood.
                                                – Zzaponka
                                                9 hours ago















                                              up vote
                                              0
                                              down vote













                                              I've seen non reachable code blocks in pre-expanded JavaScript that have been generated using a templating language.



                                              For instance, the code you are reading could have been pasted from a server that pre-evaluated the first condition that at that time relied on a variable only available on server side.



                                              if ( ${requestIsNotHttps} ){ ... }else if( ...


                                              which once pre-compiled hences :



                                              if ( 0 ){ ... }else if ( ...


                                              hope this helps you relativise the potential low keyboard activity of the pro-recycling coders era for which i manifest enthusiasm !






                                              share|improve this answer





















                                              • I agree, in the age of ubiquitous automation we should rely on autogenerated code more, as it allows us to spend more time on actual things. But for now, my exact point of interest is how this everything is architectured under the hood.
                                                – Zzaponka
                                                9 hours ago













                                              up vote
                                              0
                                              down vote










                                              up vote
                                              0
                                              down vote









                                              I've seen non reachable code blocks in pre-expanded JavaScript that have been generated using a templating language.



                                              For instance, the code you are reading could have been pasted from a server that pre-evaluated the first condition that at that time relied on a variable only available on server side.



                                              if ( ${requestIsNotHttps} ){ ... }else if( ...


                                              which once pre-compiled hences :



                                              if ( 0 ){ ... }else if ( ...


                                              hope this helps you relativise the potential low keyboard activity of the pro-recycling coders era for which i manifest enthusiasm !






                                              share|improve this answer












                                              I've seen non reachable code blocks in pre-expanded JavaScript that have been generated using a templating language.



                                              For instance, the code you are reading could have been pasted from a server that pre-evaluated the first condition that at that time relied on a variable only available on server side.



                                              if ( ${requestIsNotHttps} ){ ... }else if( ...


                                              which once pre-compiled hences :



                                              if ( 0 ){ ... }else if ( ...


                                              hope this helps you relativise the potential low keyboard activity of the pro-recycling coders era for which i manifest enthusiasm !







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered 16 hours ago









                                              simonarame

                                              905




                                              905












                                              • I agree, in the age of ubiquitous automation we should rely on autogenerated code more, as it allows us to spend more time on actual things. But for now, my exact point of interest is how this everything is architectured under the hood.
                                                – Zzaponka
                                                9 hours ago


















                                              • I agree, in the age of ubiquitous automation we should rely on autogenerated code more, as it allows us to spend more time on actual things. But for now, my exact point of interest is how this everything is architectured under the hood.
                                                – Zzaponka
                                                9 hours ago
















                                              I agree, in the age of ubiquitous automation we should rely on autogenerated code more, as it allows us to spend more time on actual things. But for now, my exact point of interest is how this everything is architectured under the hood.
                                              – Zzaponka
                                              9 hours ago




                                              I agree, in the age of ubiquitous automation we should rely on autogenerated code more, as it allows us to spend more time on actual things. But for now, my exact point of interest is how this everything is architectured under the hood.
                                              – Zzaponka
                                              9 hours ago










                                              up vote
                                              0
                                              down vote













                                              It's code rot.



                                              At some point that "if" did something useful, the situation changed, maybe the variable being evaluated was removed.



                                              The person who was fixing/changing the system did as little as possible to affect the logic of the system so he just made sure the code would recompile. So he leaves an "if(0)" because that's quick and easy and he's not totally sure that's what he wants to do. He gets the system working and he doesn't go back to fix it completely.



                                              Then the next developer comes along and thinks that was done deliberately and only comments out that part of the code (since it's not being evaluated anyway), then the next time the code is touched those comments are removed.






                                              share|improve this answer

























                                                up vote
                                                0
                                                down vote













                                                It's code rot.



                                                At some point that "if" did something useful, the situation changed, maybe the variable being evaluated was removed.



                                                The person who was fixing/changing the system did as little as possible to affect the logic of the system so he just made sure the code would recompile. So he leaves an "if(0)" because that's quick and easy and he's not totally sure that's what he wants to do. He gets the system working and he doesn't go back to fix it completely.



                                                Then the next developer comes along and thinks that was done deliberately and only comments out that part of the code (since it's not being evaluated anyway), then the next time the code is touched those comments are removed.






                                                share|improve this answer























                                                  up vote
                                                  0
                                                  down vote










                                                  up vote
                                                  0
                                                  down vote









                                                  It's code rot.



                                                  At some point that "if" did something useful, the situation changed, maybe the variable being evaluated was removed.



                                                  The person who was fixing/changing the system did as little as possible to affect the logic of the system so he just made sure the code would recompile. So he leaves an "if(0)" because that's quick and easy and he's not totally sure that's what he wants to do. He gets the system working and he doesn't go back to fix it completely.



                                                  Then the next developer comes along and thinks that was done deliberately and only comments out that part of the code (since it's not being evaluated anyway), then the next time the code is touched those comments are removed.






                                                  share|improve this answer












                                                  It's code rot.



                                                  At some point that "if" did something useful, the situation changed, maybe the variable being evaluated was removed.



                                                  The person who was fixing/changing the system did as little as possible to affect the logic of the system so he just made sure the code would recompile. So he leaves an "if(0)" because that's quick and easy and he's not totally sure that's what he wants to do. He gets the system working and he doesn't go back to fix it completely.



                                                  Then the next developer comes along and thinks that was done deliberately and only comments out that part of the code (since it's not being evaluated anyway), then the next time the code is touched those comments are removed.







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered 4 hours ago









                                                  Dark Matter

                                                  1255




                                                  1255






















                                                      up vote
                                                      0
                                                      down vote













                                                      One possibility not yet mentioned: the if (0) { line could be providing a convenient spot for a breakpoint.



                                                      Debugging is often done on non-optimised code so the always-false test will be present and able to have breakpoint set on it. When compiled for production, the line of code would be optimised out. The seemingly useless line gives functionality for development and testing builds without impacting release builds.



                                                      There are other good suggestions above as well; the only way to really know what the purpose is, is to track down the author and ask. Your source code control system might help with that. (Look for blame-type functionality.)






                                                      share|improve this answer

























                                                        up vote
                                                        0
                                                        down vote













                                                        One possibility not yet mentioned: the if (0) { line could be providing a convenient spot for a breakpoint.



                                                        Debugging is often done on non-optimised code so the always-false test will be present and able to have breakpoint set on it. When compiled for production, the line of code would be optimised out. The seemingly useless line gives functionality for development and testing builds without impacting release builds.



                                                        There are other good suggestions above as well; the only way to really know what the purpose is, is to track down the author and ask. Your source code control system might help with that. (Look for blame-type functionality.)






                                                        share|improve this answer























                                                          up vote
                                                          0
                                                          down vote










                                                          up vote
                                                          0
                                                          down vote









                                                          One possibility not yet mentioned: the if (0) { line could be providing a convenient spot for a breakpoint.



                                                          Debugging is often done on non-optimised code so the always-false test will be present and able to have breakpoint set on it. When compiled for production, the line of code would be optimised out. The seemingly useless line gives functionality for development and testing builds without impacting release builds.



                                                          There are other good suggestions above as well; the only way to really know what the purpose is, is to track down the author and ask. Your source code control system might help with that. (Look for blame-type functionality.)






                                                          share|improve this answer












                                                          One possibility not yet mentioned: the if (0) { line could be providing a convenient spot for a breakpoint.



                                                          Debugging is often done on non-optimised code so the always-false test will be present and able to have breakpoint set on it. When compiled for production, the line of code would be optimised out. The seemingly useless line gives functionality for development and testing builds without impacting release builds.



                                                          There are other good suggestions above as well; the only way to really know what the purpose is, is to track down the author and ask. Your source code control system might help with that. (Look for blame-type functionality.)







                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered 37 mins ago









                                                          studog

                                                          798




                                                          798






















                                                              up vote
                                                              -4
                                                              down vote













                                                              if (0) will always evaluate to false. The code in the question is functionally equivalent to



                                                              if (some_fn_call()) {
                                                              // actual code
                                                              } else if (some_other_fn_call()) {
                                                              // another actual code
                                                              ...
                                                              } else {
                                                              // default case
                                                              }


                                                              Possibly, this occurred because of code being deleted in the first if block and there was a possibility of it being reinserted, so the if part was kept.






                                                              share|improve this answer



















                                                              • 14




                                                                The question does not seek definition of the code shown, question is why is it the way it is?
                                                                – Sourav Ghosh
                                                                yesterday










                                                              • I do not see any reason to code it with if (0) as shown by OP. There may be some previous code that was there and later removed with if(0)
                                                                – Rishikesh Raje
                                                                yesterday










                                                              • Usually, these constructs are used to keep other conditions in similar syntax. It's an aesthetic choice.
                                                                – Rushabh Mehta
                                                                yesterday






                                                              • 2




                                                                Actually that's what i do while debugging. Instead of deleting a possibly problematic if clause, I just skip it by adding ( false && 'original boolean check'), just to make sure that if is not causing mischief. This particular answer is suggesting just that. It is a valid answer, dear down-voters please reconsider your position.
                                                                – brett
                                                                23 hours ago















                                                              up vote
                                                              -4
                                                              down vote













                                                              if (0) will always evaluate to false. The code in the question is functionally equivalent to



                                                              if (some_fn_call()) {
                                                              // actual code
                                                              } else if (some_other_fn_call()) {
                                                              // another actual code
                                                              ...
                                                              } else {
                                                              // default case
                                                              }


                                                              Possibly, this occurred because of code being deleted in the first if block and there was a possibility of it being reinserted, so the if part was kept.






                                                              share|improve this answer



















                                                              • 14




                                                                The question does not seek definition of the code shown, question is why is it the way it is?
                                                                – Sourav Ghosh
                                                                yesterday










                                                              • I do not see any reason to code it with if (0) as shown by OP. There may be some previous code that was there and later removed with if(0)
                                                                – Rishikesh Raje
                                                                yesterday










                                                              • Usually, these constructs are used to keep other conditions in similar syntax. It's an aesthetic choice.
                                                                – Rushabh Mehta
                                                                yesterday






                                                              • 2




                                                                Actually that's what i do while debugging. Instead of deleting a possibly problematic if clause, I just skip it by adding ( false && 'original boolean check'), just to make sure that if is not causing mischief. This particular answer is suggesting just that. It is a valid answer, dear down-voters please reconsider your position.
                                                                – brett
                                                                23 hours ago













                                                              up vote
                                                              -4
                                                              down vote










                                                              up vote
                                                              -4
                                                              down vote









                                                              if (0) will always evaluate to false. The code in the question is functionally equivalent to



                                                              if (some_fn_call()) {
                                                              // actual code
                                                              } else if (some_other_fn_call()) {
                                                              // another actual code
                                                              ...
                                                              } else {
                                                              // default case
                                                              }


                                                              Possibly, this occurred because of code being deleted in the first if block and there was a possibility of it being reinserted, so the if part was kept.






                                                              share|improve this answer














                                                              if (0) will always evaluate to false. The code in the question is functionally equivalent to



                                                              if (some_fn_call()) {
                                                              // actual code
                                                              } else if (some_other_fn_call()) {
                                                              // another actual code
                                                              ...
                                                              } else {
                                                              // default case
                                                              }


                                                              Possibly, this occurred because of code being deleted in the first if block and there was a possibility of it being reinserted, so the if part was kept.







                                                              share|improve this answer














                                                              share|improve this answer



                                                              share|improve this answer








                                                              edited yesterday

























                                                              answered yesterday









                                                              Rishikesh Raje

                                                              5,1261825




                                                              5,1261825








                                                              • 14




                                                                The question does not seek definition of the code shown, question is why is it the way it is?
                                                                – Sourav Ghosh
                                                                yesterday










                                                              • I do not see any reason to code it with if (0) as shown by OP. There may be some previous code that was there and later removed with if(0)
                                                                – Rishikesh Raje
                                                                yesterday










                                                              • Usually, these constructs are used to keep other conditions in similar syntax. It's an aesthetic choice.
                                                                – Rushabh Mehta
                                                                yesterday






                                                              • 2




                                                                Actually that's what i do while debugging. Instead of deleting a possibly problematic if clause, I just skip it by adding ( false && 'original boolean check'), just to make sure that if is not causing mischief. This particular answer is suggesting just that. It is a valid answer, dear down-voters please reconsider your position.
                                                                – brett
                                                                23 hours ago














                                                              • 14




                                                                The question does not seek definition of the code shown, question is why is it the way it is?
                                                                – Sourav Ghosh
                                                                yesterday










                                                              • I do not see any reason to code it with if (0) as shown by OP. There may be some previous code that was there and later removed with if(0)
                                                                – Rishikesh Raje
                                                                yesterday










                                                              • Usually, these constructs are used to keep other conditions in similar syntax. It's an aesthetic choice.
                                                                – Rushabh Mehta
                                                                yesterday






                                                              • 2




                                                                Actually that's what i do while debugging. Instead of deleting a possibly problematic if clause, I just skip it by adding ( false && 'original boolean check'), just to make sure that if is not causing mischief. This particular answer is suggesting just that. It is a valid answer, dear down-voters please reconsider your position.
                                                                – brett
                                                                23 hours ago








                                                              14




                                                              14




                                                              The question does not seek definition of the code shown, question is why is it the way it is?
                                                              – Sourav Ghosh
                                                              yesterday




                                                              The question does not seek definition of the code shown, question is why is it the way it is?
                                                              – Sourav Ghosh
                                                              yesterday












                                                              I do not see any reason to code it with if (0) as shown by OP. There may be some previous code that was there and later removed with if(0)
                                                              – Rishikesh Raje
                                                              yesterday




                                                              I do not see any reason to code it with if (0) as shown by OP. There may be some previous code that was there and later removed with if(0)
                                                              – Rishikesh Raje
                                                              yesterday












                                                              Usually, these constructs are used to keep other conditions in similar syntax. It's an aesthetic choice.
                                                              – Rushabh Mehta
                                                              yesterday




                                                              Usually, these constructs are used to keep other conditions in similar syntax. It's an aesthetic choice.
                                                              – Rushabh Mehta
                                                              yesterday




                                                              2




                                                              2




                                                              Actually that's what i do while debugging. Instead of deleting a possibly problematic if clause, I just skip it by adding ( false && 'original boolean check'), just to make sure that if is not causing mischief. This particular answer is suggesting just that. It is a valid answer, dear down-voters please reconsider your position.
                                                              – brett
                                                              23 hours ago




                                                              Actually that's what i do while debugging. Instead of deleting a possibly problematic if clause, I just skip it by adding ( false && 'original boolean check'), just to make sure that if is not causing mischief. This particular answer is suggesting just that. It is a valid answer, dear down-voters please reconsider your position.
                                                              – brett
                                                              23 hours ago










                                                              Zzaponka is a new contributor. Be nice, and check out our Code of Conduct.










                                                               

                                                              draft saved


                                                              draft discarded


















                                                              Zzaponka is a new contributor. Be nice, and check out our Code of Conduct.













                                                              Zzaponka is a new contributor. Be nice, and check out our Code of Conduct.












                                                              Zzaponka is a new contributor. Be nice, and check out our Code of Conduct.















                                                               


                                                              draft saved


                                                              draft discarded














                                                              StackExchange.ready(
                                                              function () {
                                                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298646%2fwhat-is-the-purpose-of-an-if-0-block-in-if-else-block%23new-answer', 'question_page');
                                                              }
                                                              );

                                                              Post as a guest















                                                              Required, but never shown





















































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown

































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown







                                                              Popular posts from this blog

                                                              Quarter-circle Tiles

                                                              build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

                                                              Mont Emei