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












138















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

























  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Samuel Liew
    Nov 16 '18 at 1:10






  • 2





    That statement seems irrelevant. Generate assembly code with and without that statement and you will see what is going under the hood.

    – haccks
    Nov 16 '18 at 9:38






  • 2





    It's possible this is an automatically generated code.

    – freakish
    Nov 16 '18 at 12:19
















138















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

























  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Samuel Liew
    Nov 16 '18 at 1:10






  • 2





    That statement seems irrelevant. Generate assembly code with and without that statement and you will see what is going under the hood.

    – haccks
    Nov 16 '18 at 9:38






  • 2





    It's possible this is an automatically generated code.

    – freakish
    Nov 16 '18 at 12:19














138












138








138


9






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
















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















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 14:04









Boann

36.9k1290121




36.9k1290121










asked Nov 14 '18 at 11:00









ZzaponkaZzaponka

802267




802267













  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Samuel Liew
    Nov 16 '18 at 1:10






  • 2





    That statement seems irrelevant. Generate assembly code with and without that statement and you will see what is going under the hood.

    – haccks
    Nov 16 '18 at 9:38






  • 2





    It's possible this is an automatically generated code.

    – freakish
    Nov 16 '18 at 12:19



















  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Samuel Liew
    Nov 16 '18 at 1:10






  • 2





    That statement seems irrelevant. Generate assembly code with and without that statement and you will see what is going under the hood.

    – haccks
    Nov 16 '18 at 9:38






  • 2





    It's possible this is an automatically generated code.

    – freakish
    Nov 16 '18 at 12:19

















Comments are not for extended discussion; this conversation has been moved to chat.

– Samuel Liew
Nov 16 '18 at 1:10





Comments are not for extended discussion; this conversation has been moved to chat.

– Samuel Liew
Nov 16 '18 at 1:10




2




2





That statement seems irrelevant. Generate assembly code with and without that statement and you will see what is going under the hood.

– haccks
Nov 16 '18 at 9:38





That statement seems irrelevant. Generate assembly code with and without that statement and you will see what is going under the hood.

– haccks
Nov 16 '18 at 9:38




2




2





It's possible this is an automatically generated code.

– freakish
Nov 16 '18 at 12:19





It's possible this is an automatically generated code.

– freakish
Nov 16 '18 at 12:19












15 Answers
15






active

oldest

votes


















88














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





















  • 236





    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
    Nov 14 '18 at 11:15








  • 13





    @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
    Nov 14 '18 at 11:25






  • 72





    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.
    Nov 14 '18 at 13:53






  • 77





    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
    Nov 14 '18 at 21:58






  • 26





    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
    Nov 15 '18 at 3:44





















104














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



















  • 5





    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
    Nov 14 '18 at 20:31






  • 1





    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
    Nov 15 '18 at 9:49






  • 1





    @KonradRudolph I'm not following; how would you write it?

    – JiK
    Nov 15 '18 at 17:19






  • 1





    @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
    Nov 15 '18 at 17:54








  • 5





    @KonradRudolph that's fine if you want to double the number of guards and triple the number of guard conditions mentioned, I suppose.

    – hobbs
    Nov 15 '18 at 22:38



















44














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



















  • 4





    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
    Nov 14 '18 at 21:08






  • 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
    Nov 14 '18 at 21:55






  • 7





    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
    Nov 15 '18 at 12:32













  • This is really handy approach when generating some kind of dynamic SQL with unknown number of final conditions.

    – Skipper
    Nov 16 '18 at 12:22






  • 1





    @freakish indeed I wrote the opposite: poorly readable syntax is acceptable in generated code since it will most likely never be read, not in high-level functional code that is maintained by developers.

    – phagio
    Nov 16 '18 at 13:41



















42














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



















  • 2





    Nailed it. I couldn't see any other reason beside debugging.

    – tfont
    Nov 23 '18 at 15:18



















15














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































    15














    I am 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 developers/ 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





















    • 3





      Isn't if(0) decrease branch coverage too?

      – David Szalai
      Nov 14 '18 at 15:40






    • 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
      Nov 14 '18 at 15:41





















    14














    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































      9














      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



















      • 1





        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
        Nov 15 '18 at 10:34



















      8














      That construct may also be used in C to implement generic programming with type safety, relying on the fact that the unreachable code is still checked by the compiler:



      // this is a generic unsafe function, that will call fun(arg) at a later time
      void defer(void *fun, void *arg);

      // this is a macro that makes it safer, by checking the argument
      // matches the function signature
      #define DEFER(f, arg)
      if(0) f(arg); // never actually called, but compile-time checked
      else defer(f, (void *)arg); // do the unsafe call after safety check

      void myfunction(int *p);

      DEFER(myfunction, 42); // compile error
      int *b;
      DEFER(myfunction, b); // compiles OK





      share|improve this answer































        6














        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































          6














          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

































            1














            It helps to debug this block just putting if block 1. This disable all if else block functionality. And also we can expand the if else block.






            share|improve this answer































              1














                  Actually according to my opinion, if we put any variable for checking inside
              e.g:-
              public static void main(string args)
              {
              var status;
              var empList=_unitofWork.EmpRepository.Get(con=>con.isRetired==true);
              //some code logic
              if(empList.count>0)
              {
              status=true;
              }
              if(status)
              {
              //do something
              }
              else
              {
              //do something else
              }
              }
              if then its dynamically get the value in run time and invoke the logic inside it, else its simply extra line of code i guess.

              Anybody have any depth knowledge why this thing is used....or agree with me.
              kindly respond.





              share|improve this answer































                1














                @PSkocik's answer is fine, but I add my two cents. Unsure if I should do this as a comment, or as an answer; choosing the latter, because IMHO worth others seeing, whereas comments are frequently invisible.



                Not only do I occasionally use



                if(0) {
                //deliberately left empty
                } else if( cond1 ) {
                //deliberately left empty
                } else if( cond2 ) {
                //deliberately left empty
                ...
                } else {
                // no conditions matched
                }


                But I also occasionally do



                if( 1 
                && cond1
                && cond2
                ...
                && condN
                ) {


                or



                if( 0 
                || cond1
                || cond2
                ...
                || condN
                ) {


                for complicated conditions. For the same reasons - easier to edit, #ifdef, etc.



                For that matter, in Perl I will do



                @array = (  
                elem1,
                elem2,
                ...
                elem1,
                ) {



                • note the comma at the end of the list. I forget if commas are separators or delimiters in C and C++ lists. IMHO this is one thing we have learned: [Are trailing commas in Perl a bad practice? commas] are a good thing. Like any new notation, it takes a while to get used to.


                I compare the if(0) code to lisp



                (cond   (test1    action1)
                (test2 action2)
                ...
                (testn actionn))


                which, you guessed it, I may indent as



                (cond   
                (test1 action1)
                (test2 action2)
                ...
                (testn actionn)
                )


                I have sometimes tried to imagine what a more human readable syntax for this might look like.



                Perhaps



                IF
                :: cond1 THEN code1
                :: cond2 THEN code2
                ...
                :: condN THEN codeN
                FI


                inspired by Dikstra's [https://en.wikipedia.org/wiki/Guarded_Command_Language#Selection:_if][Guarded Command Language].



                But this syntax implies that the conditions are evaluated in parallel, whereas if...else-if implies sequential and prioritized evaluation of conditions.



                I started doing this sort of thing when writing programs that generated other programs, where it is especially convenient.



                While we are at it, when writing RTL using Intel's old iHDL, I have coded stuff like



                   IF 0 THEN /*nothing*/
                **FORC i FROM 1 TO 10 DOC**
                ELSE IF signal%i% THEN
                // stuff to do if signal%i% is active
                **ENDC**
                ELSE
                // nothing matched
                ENDIF


                where the FORC..DOC..ENDC is a macro preprocessor loop construct, that expands to



                   IF 0 THEN /*nothing*/
                ELSE IF signal1 THEN
                // stuff to do if signal1 is active
                ELSE IF signal2 THEN
                // stuff to do if signal2 is active
                ...
                ELSE IF signal100 THEN
                // stuff to do if signal100 is active
                ELSE
                // nothing matched
                ENDIF


                This was single assignment, non-imperative, code, so setting a state variable was not allowed, if you needed to do things like find first set bit.



                   IF 0 THEN /*nothing*/
                ELSE IF signal1 THEN
                found := 1
                ELSE IF signal2 THEN
                found := 2
                ...
                ELSE IF signal100 THEN
                found := 100
                ELSE
                // nothing matched
                ENDIF


                Come to think of it, this may have been the first place that I encountered such constructs.



                BTW, the objections that some had to the if(0) style - that the else-if-conditions are sequentially dependent and cannot be arbitrarily reordered - do not apply to AND and OR and XOR logic in RTL - but do apply to short-circuit && and ||.






                share|improve this answer

































                  -2














                  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























                    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',
                    autoActivateHeartbeat: false,
                    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
                    });


                    }
                    });














                    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

























                    15 Answers
                    15






                    active

                    oldest

                    votes








                    15 Answers
                    15






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    88














                    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





















                    • 236





                      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
                      Nov 14 '18 at 11:15








                    • 13





                      @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
                      Nov 14 '18 at 11:25






                    • 72





                      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.
                      Nov 14 '18 at 13:53






                    • 77





                      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
                      Nov 14 '18 at 21:58






                    • 26





                      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
                      Nov 15 '18 at 3:44


















                    88














                    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





















                    • 236





                      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
                      Nov 14 '18 at 11:15








                    • 13





                      @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
                      Nov 14 '18 at 11:25






                    • 72





                      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.
                      Nov 14 '18 at 13:53






                    • 77





                      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
                      Nov 14 '18 at 21:58






                    • 26





                      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
                      Nov 15 '18 at 3:44
















                    88












                    88








                    88







                    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 Nov 15 '18 at 7:49

























                    answered Nov 14 '18 at 11:09









                    PSkocikPSkocik

                    33.1k65173




                    33.1k65173








                    • 236





                      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
                      Nov 14 '18 at 11:15








                    • 13





                      @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
                      Nov 14 '18 at 11:25






                    • 72





                      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.
                      Nov 14 '18 at 13:53






                    • 77





                      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
                      Nov 14 '18 at 21:58






                    • 26





                      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
                      Nov 15 '18 at 3:44
















                    • 236





                      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
                      Nov 14 '18 at 11:15








                    • 13





                      @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
                      Nov 14 '18 at 11:25






                    • 72





                      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.
                      Nov 14 '18 at 13:53






                    • 77





                      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
                      Nov 14 '18 at 21:58






                    • 26





                      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
                      Nov 15 '18 at 3:44










                    236




                    236





                    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
                    Nov 14 '18 at 11:15







                    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
                    Nov 14 '18 at 11:15






                    13




                    13





                    @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
                    Nov 14 '18 at 11:25





                    @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
                    Nov 14 '18 at 11:25




                    72




                    72





                    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.
                    Nov 14 '18 at 13:53





                    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.
                    Nov 14 '18 at 13:53




                    77




                    77





                    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
                    Nov 14 '18 at 21:58





                    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
                    Nov 14 '18 at 21:58




                    26




                    26





                    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
                    Nov 15 '18 at 3:44







                    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
                    Nov 15 '18 at 3:44















                    104














                    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



















                    • 5





                      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
                      Nov 14 '18 at 20:31






                    • 1





                      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
                      Nov 15 '18 at 9:49






                    • 1





                      @KonradRudolph I'm not following; how would you write it?

                      – JiK
                      Nov 15 '18 at 17:19






                    • 1





                      @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
                      Nov 15 '18 at 17:54








                    • 5





                      @KonradRudolph that's fine if you want to double the number of guards and triple the number of guard conditions mentioned, I suppose.

                      – hobbs
                      Nov 15 '18 at 22:38
















                    104














                    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



















                    • 5





                      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
                      Nov 14 '18 at 20:31






                    • 1





                      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
                      Nov 15 '18 at 9:49






                    • 1





                      @KonradRudolph I'm not following; how would you write it?

                      – JiK
                      Nov 15 '18 at 17:19






                    • 1





                      @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
                      Nov 15 '18 at 17:54








                    • 5





                      @KonradRudolph that's fine if you want to double the number of guards and triple the number of guard conditions mentioned, I suppose.

                      – hobbs
                      Nov 15 '18 at 22:38














                    104












                    104








                    104







                    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 Nov 14 '18 at 20:14









                    CSMCSM

                    1,070179




                    1,070179








                    • 5





                      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
                      Nov 14 '18 at 20:31






                    • 1





                      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
                      Nov 15 '18 at 9:49






                    • 1





                      @KonradRudolph I'm not following; how would you write it?

                      – JiK
                      Nov 15 '18 at 17:19






                    • 1





                      @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
                      Nov 15 '18 at 17:54








                    • 5





                      @KonradRudolph that's fine if you want to double the number of guards and triple the number of guard conditions mentioned, I suppose.

                      – hobbs
                      Nov 15 '18 at 22:38














                    • 5





                      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
                      Nov 14 '18 at 20:31






                    • 1





                      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
                      Nov 15 '18 at 9:49






                    • 1





                      @KonradRudolph I'm not following; how would you write it?

                      – JiK
                      Nov 15 '18 at 17:19






                    • 1





                      @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
                      Nov 15 '18 at 17:54








                    • 5





                      @KonradRudolph that's fine if you want to double the number of guards and triple the number of guard conditions mentioned, I suppose.

                      – hobbs
                      Nov 15 '18 at 22:38








                    5




                    5





                    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
                    Nov 14 '18 at 20:31





                    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
                    Nov 14 '18 at 20:31




                    1




                    1





                    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
                    Nov 15 '18 at 9:49





                    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
                    Nov 15 '18 at 9:49




                    1




                    1





                    @KonradRudolph I'm not following; how would you write it?

                    – JiK
                    Nov 15 '18 at 17:19





                    @KonradRudolph I'm not following; how would you write it?

                    – JiK
                    Nov 15 '18 at 17:19




                    1




                    1





                    @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
                    Nov 15 '18 at 17:54







                    @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
                    Nov 15 '18 at 17:54






                    5




                    5





                    @KonradRudolph that's fine if you want to double the number of guards and triple the number of guard conditions mentioned, I suppose.

                    – hobbs
                    Nov 15 '18 at 22:38





                    @KonradRudolph that's fine if you want to double the number of guards and triple the number of guard conditions mentioned, I suppose.

                    – hobbs
                    Nov 15 '18 at 22:38











                    44














                    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



















                    • 4





                      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
                      Nov 14 '18 at 21:08






                    • 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
                      Nov 14 '18 at 21:55






                    • 7





                      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
                      Nov 15 '18 at 12:32













                    • This is really handy approach when generating some kind of dynamic SQL with unknown number of final conditions.

                      – Skipper
                      Nov 16 '18 at 12:22






                    • 1





                      @freakish indeed I wrote the opposite: poorly readable syntax is acceptable in generated code since it will most likely never be read, not in high-level functional code that is maintained by developers.

                      – phagio
                      Nov 16 '18 at 13:41
















                    44














                    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



















                    • 4





                      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
                      Nov 14 '18 at 21:08






                    • 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
                      Nov 14 '18 at 21:55






                    • 7





                      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
                      Nov 15 '18 at 12:32













                    • This is really handy approach when generating some kind of dynamic SQL with unknown number of final conditions.

                      – Skipper
                      Nov 16 '18 at 12:22






                    • 1





                      @freakish indeed I wrote the opposite: poorly readable syntax is acceptable in generated code since it will most likely never be read, not in high-level functional code that is maintained by developers.

                      – phagio
                      Nov 16 '18 at 13:41














                    44












                    44








                    44







                    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 Nov 14 '18 at 16:01









                    seth flowersseth flowers

                    7,73221835




                    7,73221835








                    • 4





                      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
                      Nov 14 '18 at 21:08






                    • 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
                      Nov 14 '18 at 21:55






                    • 7





                      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
                      Nov 15 '18 at 12:32













                    • This is really handy approach when generating some kind of dynamic SQL with unknown number of final conditions.

                      – Skipper
                      Nov 16 '18 at 12:22






                    • 1





                      @freakish indeed I wrote the opposite: poorly readable syntax is acceptable in generated code since it will most likely never be read, not in high-level functional code that is maintained by developers.

                      – phagio
                      Nov 16 '18 at 13:41














                    • 4





                      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
                      Nov 14 '18 at 21:08






                    • 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
                      Nov 14 '18 at 21:55






                    • 7





                      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
                      Nov 15 '18 at 12:32













                    • This is really handy approach when generating some kind of dynamic SQL with unknown number of final conditions.

                      – Skipper
                      Nov 16 '18 at 12:22






                    • 1





                      @freakish indeed I wrote the opposite: poorly readable syntax is acceptable in generated code since it will most likely never be read, not in high-level functional code that is maintained by developers.

                      – phagio
                      Nov 16 '18 at 13:41








                    4




                    4





                    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
                    Nov 14 '18 at 21:08





                    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
                    Nov 14 '18 at 21:08




                    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
                    Nov 14 '18 at 21:55





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

                    – Nic Hartley
                    Nov 14 '18 at 21:55




                    7




                    7





                    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
                    Nov 15 '18 at 12:32







                    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
                    Nov 15 '18 at 12:32















                    This is really handy approach when generating some kind of dynamic SQL with unknown number of final conditions.

                    – Skipper
                    Nov 16 '18 at 12:22





                    This is really handy approach when generating some kind of dynamic SQL with unknown number of final conditions.

                    – Skipper
                    Nov 16 '18 at 12:22




                    1




                    1





                    @freakish indeed I wrote the opposite: poorly readable syntax is acceptable in generated code since it will most likely never be read, not in high-level functional code that is maintained by developers.

                    – phagio
                    Nov 16 '18 at 13:41





                    @freakish indeed I wrote the opposite: poorly readable syntax is acceptable in generated code since it will most likely never be read, not in high-level functional code that is maintained by developers.

                    – phagio
                    Nov 16 '18 at 13:41











                    42














                    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



















                    • 2





                      Nailed it. I couldn't see any other reason beside debugging.

                      – tfont
                      Nov 23 '18 at 15:18
















                    42














                    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



















                    • 2





                      Nailed it. I couldn't see any other reason beside debugging.

                      – tfont
                      Nov 23 '18 at 15:18














                    42












                    42








                    42







                    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 Nov 14 '18 at 19:09









                    Russell BorogoveRussell Borogove

                    13.5k3040




                    13.5k3040








                    • 2





                      Nailed it. I couldn't see any other reason beside debugging.

                      – tfont
                      Nov 23 '18 at 15:18














                    • 2





                      Nailed it. I couldn't see any other reason beside debugging.

                      – tfont
                      Nov 23 '18 at 15:18








                    2




                    2





                    Nailed it. I couldn't see any other reason beside debugging.

                    – tfont
                    Nov 23 '18 at 15:18





                    Nailed it. I couldn't see any other reason beside debugging.

                    – tfont
                    Nov 23 '18 at 15:18











                    15














                    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




























                      15














                      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


























                        15












                        15








                        15







                        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 Nov 15 '18 at 19:19









                        studogstudog

                        33729




                        33729























                            15














                            I am 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 developers/ 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





















                            • 3





                              Isn't if(0) decrease branch coverage too?

                              – David Szalai
                              Nov 14 '18 at 15:40






                            • 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
                              Nov 14 '18 at 15:41


















                            15














                            I am 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 developers/ 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





















                            • 3





                              Isn't if(0) decrease branch coverage too?

                              – David Szalai
                              Nov 14 '18 at 15:40






                            • 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
                              Nov 14 '18 at 15:41
















                            15












                            15








                            15







                            I am 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 developers/ 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















                            I am 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 developers/ 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








                            edited Nov 17 '18 at 16:15









                            Peter Mortensen

                            13.6k1984111




                            13.6k1984111










                            answered Nov 14 '18 at 11:14









                            Sourav GhoshSourav Ghosh

                            110k14130188




                            110k14130188








                            • 3





                              Isn't if(0) decrease branch coverage too?

                              – David Szalai
                              Nov 14 '18 at 15:40






                            • 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
                              Nov 14 '18 at 15:41
















                            • 3





                              Isn't if(0) decrease branch coverage too?

                              – David Szalai
                              Nov 14 '18 at 15:40






                            • 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
                              Nov 14 '18 at 15:41










                            3




                            3





                            Isn't if(0) decrease branch coverage too?

                            – David Szalai
                            Nov 14 '18 at 15:40





                            Isn't if(0) decrease branch coverage too?

                            – David Szalai
                            Nov 14 '18 at 15:40




                            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
                            Nov 14 '18 at 15:41







                            @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
                            Nov 14 '18 at 15:41













                            14














                            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




























                              14














                              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


























                                14












                                14








                                14







                                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 Nov 15 '18 at 14:59









                                Dark Matter Dark Matter

                                28016




                                28016























                                    9














                                    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



















                                    • 1





                                      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
                                      Nov 15 '18 at 10:34
















                                    9














                                    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



















                                    • 1





                                      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
                                      Nov 15 '18 at 10:34














                                    9












                                    9








                                    9







                                    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 Nov 15 '18 at 3:49









                                    simonaramesimonarame

                                    1806




                                    1806








                                    • 1





                                      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
                                      Nov 15 '18 at 10:34














                                    • 1





                                      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
                                      Nov 15 '18 at 10:34








                                    1




                                    1





                                    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
                                    Nov 15 '18 at 10:34





                                    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
                                    Nov 15 '18 at 10:34











                                    8














                                    That construct may also be used in C to implement generic programming with type safety, relying on the fact that the unreachable code is still checked by the compiler:



                                    // this is a generic unsafe function, that will call fun(arg) at a later time
                                    void defer(void *fun, void *arg);

                                    // this is a macro that makes it safer, by checking the argument
                                    // matches the function signature
                                    #define DEFER(f, arg)
                                    if(0) f(arg); // never actually called, but compile-time checked
                                    else defer(f, (void *)arg); // do the unsafe call after safety check

                                    void myfunction(int *p);

                                    DEFER(myfunction, 42); // compile error
                                    int *b;
                                    DEFER(myfunction, b); // compiles OK





                                    share|improve this answer




























                                      8














                                      That construct may also be used in C to implement generic programming with type safety, relying on the fact that the unreachable code is still checked by the compiler:



                                      // this is a generic unsafe function, that will call fun(arg) at a later time
                                      void defer(void *fun, void *arg);

                                      // this is a macro that makes it safer, by checking the argument
                                      // matches the function signature
                                      #define DEFER(f, arg)
                                      if(0) f(arg); // never actually called, but compile-time checked
                                      else defer(f, (void *)arg); // do the unsafe call after safety check

                                      void myfunction(int *p);

                                      DEFER(myfunction, 42); // compile error
                                      int *b;
                                      DEFER(myfunction, b); // compiles OK





                                      share|improve this answer


























                                        8












                                        8








                                        8







                                        That construct may also be used in C to implement generic programming with type safety, relying on the fact that the unreachable code is still checked by the compiler:



                                        // this is a generic unsafe function, that will call fun(arg) at a later time
                                        void defer(void *fun, void *arg);

                                        // this is a macro that makes it safer, by checking the argument
                                        // matches the function signature
                                        #define DEFER(f, arg)
                                        if(0) f(arg); // never actually called, but compile-time checked
                                        else defer(f, (void *)arg); // do the unsafe call after safety check

                                        void myfunction(int *p);

                                        DEFER(myfunction, 42); // compile error
                                        int *b;
                                        DEFER(myfunction, b); // compiles OK





                                        share|improve this answer













                                        That construct may also be used in C to implement generic programming with type safety, relying on the fact that the unreachable code is still checked by the compiler:



                                        // this is a generic unsafe function, that will call fun(arg) at a later time
                                        void defer(void *fun, void *arg);

                                        // this is a macro that makes it safer, by checking the argument
                                        // matches the function signature
                                        #define DEFER(f, arg)
                                        if(0) f(arg); // never actually called, but compile-time checked
                                        else defer(f, (void *)arg); // do the unsafe call after safety check

                                        void myfunction(int *p);

                                        DEFER(myfunction, 42); // compile error
                                        int *b;
                                        DEFER(myfunction, b); // compiles OK






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Nov 20 '18 at 19:53









                                        philfrphilfr

                                        980712




                                        980712























                                            6














                                            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




























                                              6














                                              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


























                                                6












                                                6








                                                6







                                                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 Nov 14 '18 at 11:12









                                                cha0sitecha0site

                                                8,06622744




                                                8,06622744























                                                    6














                                                    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






























                                                      6














                                                      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




























                                                        6












                                                        6








                                                        6







                                                        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















                                                        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














                                                        share|improve this answer



                                                        share|improve this answer








                                                        edited Nov 14 '18 at 13:39

























                                                        answered Nov 14 '18 at 11:28









                                                        sergiopmsergiopm

                                                        1514




                                                        1514























                                                            1














                                                            It helps to debug this block just putting if block 1. This disable all if else block functionality. And also we can expand the if else block.






                                                            share|improve this answer




























                                                              1














                                                              It helps to debug this block just putting if block 1. This disable all if else block functionality. And also we can expand the if else block.






                                                              share|improve this answer


























                                                                1












                                                                1








                                                                1







                                                                It helps to debug this block just putting if block 1. This disable all if else block functionality. And also we can expand the if else block.






                                                                share|improve this answer













                                                                It helps to debug this block just putting if block 1. This disable all if else block functionality. And also we can expand the if else block.







                                                                share|improve this answer












                                                                share|improve this answer



                                                                share|improve this answer










                                                                answered Nov 21 '18 at 6:07









                                                                Abdul Ahad SheikhAbdul Ahad Sheikh

                                                                112




                                                                112























                                                                    1














                                                                        Actually according to my opinion, if we put any variable for checking inside
                                                                    e.g:-
                                                                    public static void main(string args)
                                                                    {
                                                                    var status;
                                                                    var empList=_unitofWork.EmpRepository.Get(con=>con.isRetired==true);
                                                                    //some code logic
                                                                    if(empList.count>0)
                                                                    {
                                                                    status=true;
                                                                    }
                                                                    if(status)
                                                                    {
                                                                    //do something
                                                                    }
                                                                    else
                                                                    {
                                                                    //do something else
                                                                    }
                                                                    }
                                                                    if then its dynamically get the value in run time and invoke the logic inside it, else its simply extra line of code i guess.

                                                                    Anybody have any depth knowledge why this thing is used....or agree with me.
                                                                    kindly respond.





                                                                    share|improve this answer




























                                                                      1














                                                                          Actually according to my opinion, if we put any variable for checking inside
                                                                      e.g:-
                                                                      public static void main(string args)
                                                                      {
                                                                      var status;
                                                                      var empList=_unitofWork.EmpRepository.Get(con=>con.isRetired==true);
                                                                      //some code logic
                                                                      if(empList.count>0)
                                                                      {
                                                                      status=true;
                                                                      }
                                                                      if(status)
                                                                      {
                                                                      //do something
                                                                      }
                                                                      else
                                                                      {
                                                                      //do something else
                                                                      }
                                                                      }
                                                                      if then its dynamically get the value in run time and invoke the logic inside it, else its simply extra line of code i guess.

                                                                      Anybody have any depth knowledge why this thing is used....or agree with me.
                                                                      kindly respond.





                                                                      share|improve this answer


























                                                                        1












                                                                        1








                                                                        1







                                                                            Actually according to my opinion, if we put any variable for checking inside
                                                                        e.g:-
                                                                        public static void main(string args)
                                                                        {
                                                                        var status;
                                                                        var empList=_unitofWork.EmpRepository.Get(con=>con.isRetired==true);
                                                                        //some code logic
                                                                        if(empList.count>0)
                                                                        {
                                                                        status=true;
                                                                        }
                                                                        if(status)
                                                                        {
                                                                        //do something
                                                                        }
                                                                        else
                                                                        {
                                                                        //do something else
                                                                        }
                                                                        }
                                                                        if then its dynamically get the value in run time and invoke the logic inside it, else its simply extra line of code i guess.

                                                                        Anybody have any depth knowledge why this thing is used....or agree with me.
                                                                        kindly respond.





                                                                        share|improve this answer













                                                                            Actually according to my opinion, if we put any variable for checking inside
                                                                        e.g:-
                                                                        public static void main(string args)
                                                                        {
                                                                        var status;
                                                                        var empList=_unitofWork.EmpRepository.Get(con=>con.isRetired==true);
                                                                        //some code logic
                                                                        if(empList.count>0)
                                                                        {
                                                                        status=true;
                                                                        }
                                                                        if(status)
                                                                        {
                                                                        //do something
                                                                        }
                                                                        else
                                                                        {
                                                                        //do something else
                                                                        }
                                                                        }
                                                                        if then its dynamically get the value in run time and invoke the logic inside it, else its simply extra line of code i guess.

                                                                        Anybody have any depth knowledge why this thing is used....or agree with me.
                                                                        kindly respond.






                                                                        share|improve this answer












                                                                        share|improve this answer



                                                                        share|improve this answer










                                                                        answered Dec 4 '18 at 8:06









                                                                        Sagar Kumar ChoudhurySagar Kumar Choudhury

                                                                        213




                                                                        213























                                                                            1














                                                                            @PSkocik's answer is fine, but I add my two cents. Unsure if I should do this as a comment, or as an answer; choosing the latter, because IMHO worth others seeing, whereas comments are frequently invisible.



                                                                            Not only do I occasionally use



                                                                            if(0) {
                                                                            //deliberately left empty
                                                                            } else if( cond1 ) {
                                                                            //deliberately left empty
                                                                            } else if( cond2 ) {
                                                                            //deliberately left empty
                                                                            ...
                                                                            } else {
                                                                            // no conditions matched
                                                                            }


                                                                            But I also occasionally do



                                                                            if( 1 
                                                                            && cond1
                                                                            && cond2
                                                                            ...
                                                                            && condN
                                                                            ) {


                                                                            or



                                                                            if( 0 
                                                                            || cond1
                                                                            || cond2
                                                                            ...
                                                                            || condN
                                                                            ) {


                                                                            for complicated conditions. For the same reasons - easier to edit, #ifdef, etc.



                                                                            For that matter, in Perl I will do



                                                                            @array = (  
                                                                            elem1,
                                                                            elem2,
                                                                            ...
                                                                            elem1,
                                                                            ) {



                                                                            • note the comma at the end of the list. I forget if commas are separators or delimiters in C and C++ lists. IMHO this is one thing we have learned: [Are trailing commas in Perl a bad practice? commas] are a good thing. Like any new notation, it takes a while to get used to.


                                                                            I compare the if(0) code to lisp



                                                                            (cond   (test1    action1)
                                                                            (test2 action2)
                                                                            ...
                                                                            (testn actionn))


                                                                            which, you guessed it, I may indent as



                                                                            (cond   
                                                                            (test1 action1)
                                                                            (test2 action2)
                                                                            ...
                                                                            (testn actionn)
                                                                            )


                                                                            I have sometimes tried to imagine what a more human readable syntax for this might look like.



                                                                            Perhaps



                                                                            IF
                                                                            :: cond1 THEN code1
                                                                            :: cond2 THEN code2
                                                                            ...
                                                                            :: condN THEN codeN
                                                                            FI


                                                                            inspired by Dikstra's [https://en.wikipedia.org/wiki/Guarded_Command_Language#Selection:_if][Guarded Command Language].



                                                                            But this syntax implies that the conditions are evaluated in parallel, whereas if...else-if implies sequential and prioritized evaluation of conditions.



                                                                            I started doing this sort of thing when writing programs that generated other programs, where it is especially convenient.



                                                                            While we are at it, when writing RTL using Intel's old iHDL, I have coded stuff like



                                                                               IF 0 THEN /*nothing*/
                                                                            **FORC i FROM 1 TO 10 DOC**
                                                                            ELSE IF signal%i% THEN
                                                                            // stuff to do if signal%i% is active
                                                                            **ENDC**
                                                                            ELSE
                                                                            // nothing matched
                                                                            ENDIF


                                                                            where the FORC..DOC..ENDC is a macro preprocessor loop construct, that expands to



                                                                               IF 0 THEN /*nothing*/
                                                                            ELSE IF signal1 THEN
                                                                            // stuff to do if signal1 is active
                                                                            ELSE IF signal2 THEN
                                                                            // stuff to do if signal2 is active
                                                                            ...
                                                                            ELSE IF signal100 THEN
                                                                            // stuff to do if signal100 is active
                                                                            ELSE
                                                                            // nothing matched
                                                                            ENDIF


                                                                            This was single assignment, non-imperative, code, so setting a state variable was not allowed, if you needed to do things like find first set bit.



                                                                               IF 0 THEN /*nothing*/
                                                                            ELSE IF signal1 THEN
                                                                            found := 1
                                                                            ELSE IF signal2 THEN
                                                                            found := 2
                                                                            ...
                                                                            ELSE IF signal100 THEN
                                                                            found := 100
                                                                            ELSE
                                                                            // nothing matched
                                                                            ENDIF


                                                                            Come to think of it, this may have been the first place that I encountered such constructs.



                                                                            BTW, the objections that some had to the if(0) style - that the else-if-conditions are sequentially dependent and cannot be arbitrarily reordered - do not apply to AND and OR and XOR logic in RTL - but do apply to short-circuit && and ||.






                                                                            share|improve this answer






























                                                                              1














                                                                              @PSkocik's answer is fine, but I add my two cents. Unsure if I should do this as a comment, or as an answer; choosing the latter, because IMHO worth others seeing, whereas comments are frequently invisible.



                                                                              Not only do I occasionally use



                                                                              if(0) {
                                                                              //deliberately left empty
                                                                              } else if( cond1 ) {
                                                                              //deliberately left empty
                                                                              } else if( cond2 ) {
                                                                              //deliberately left empty
                                                                              ...
                                                                              } else {
                                                                              // no conditions matched
                                                                              }


                                                                              But I also occasionally do



                                                                              if( 1 
                                                                              && cond1
                                                                              && cond2
                                                                              ...
                                                                              && condN
                                                                              ) {


                                                                              or



                                                                              if( 0 
                                                                              || cond1
                                                                              || cond2
                                                                              ...
                                                                              || condN
                                                                              ) {


                                                                              for complicated conditions. For the same reasons - easier to edit, #ifdef, etc.



                                                                              For that matter, in Perl I will do



                                                                              @array = (  
                                                                              elem1,
                                                                              elem2,
                                                                              ...
                                                                              elem1,
                                                                              ) {



                                                                              • note the comma at the end of the list. I forget if commas are separators or delimiters in C and C++ lists. IMHO this is one thing we have learned: [Are trailing commas in Perl a bad practice? commas] are a good thing. Like any new notation, it takes a while to get used to.


                                                                              I compare the if(0) code to lisp



                                                                              (cond   (test1    action1)
                                                                              (test2 action2)
                                                                              ...
                                                                              (testn actionn))


                                                                              which, you guessed it, I may indent as



                                                                              (cond   
                                                                              (test1 action1)
                                                                              (test2 action2)
                                                                              ...
                                                                              (testn actionn)
                                                                              )


                                                                              I have sometimes tried to imagine what a more human readable syntax for this might look like.



                                                                              Perhaps



                                                                              IF
                                                                              :: cond1 THEN code1
                                                                              :: cond2 THEN code2
                                                                              ...
                                                                              :: condN THEN codeN
                                                                              FI


                                                                              inspired by Dikstra's [https://en.wikipedia.org/wiki/Guarded_Command_Language#Selection:_if][Guarded Command Language].



                                                                              But this syntax implies that the conditions are evaluated in parallel, whereas if...else-if implies sequential and prioritized evaluation of conditions.



                                                                              I started doing this sort of thing when writing programs that generated other programs, where it is especially convenient.



                                                                              While we are at it, when writing RTL using Intel's old iHDL, I have coded stuff like



                                                                                 IF 0 THEN /*nothing*/
                                                                              **FORC i FROM 1 TO 10 DOC**
                                                                              ELSE IF signal%i% THEN
                                                                              // stuff to do if signal%i% is active
                                                                              **ENDC**
                                                                              ELSE
                                                                              // nothing matched
                                                                              ENDIF


                                                                              where the FORC..DOC..ENDC is a macro preprocessor loop construct, that expands to



                                                                                 IF 0 THEN /*nothing*/
                                                                              ELSE IF signal1 THEN
                                                                              // stuff to do if signal1 is active
                                                                              ELSE IF signal2 THEN
                                                                              // stuff to do if signal2 is active
                                                                              ...
                                                                              ELSE IF signal100 THEN
                                                                              // stuff to do if signal100 is active
                                                                              ELSE
                                                                              // nothing matched
                                                                              ENDIF


                                                                              This was single assignment, non-imperative, code, so setting a state variable was not allowed, if you needed to do things like find first set bit.



                                                                                 IF 0 THEN /*nothing*/
                                                                              ELSE IF signal1 THEN
                                                                              found := 1
                                                                              ELSE IF signal2 THEN
                                                                              found := 2
                                                                              ...
                                                                              ELSE IF signal100 THEN
                                                                              found := 100
                                                                              ELSE
                                                                              // nothing matched
                                                                              ENDIF


                                                                              Come to think of it, this may have been the first place that I encountered such constructs.



                                                                              BTW, the objections that some had to the if(0) style - that the else-if-conditions are sequentially dependent and cannot be arbitrarily reordered - do not apply to AND and OR and XOR logic in RTL - but do apply to short-circuit && and ||.






                                                                              share|improve this answer




























                                                                                1












                                                                                1








                                                                                1







                                                                                @PSkocik's answer is fine, but I add my two cents. Unsure if I should do this as a comment, or as an answer; choosing the latter, because IMHO worth others seeing, whereas comments are frequently invisible.



                                                                                Not only do I occasionally use



                                                                                if(0) {
                                                                                //deliberately left empty
                                                                                } else if( cond1 ) {
                                                                                //deliberately left empty
                                                                                } else if( cond2 ) {
                                                                                //deliberately left empty
                                                                                ...
                                                                                } else {
                                                                                // no conditions matched
                                                                                }


                                                                                But I also occasionally do



                                                                                if( 1 
                                                                                && cond1
                                                                                && cond2
                                                                                ...
                                                                                && condN
                                                                                ) {


                                                                                or



                                                                                if( 0 
                                                                                || cond1
                                                                                || cond2
                                                                                ...
                                                                                || condN
                                                                                ) {


                                                                                for complicated conditions. For the same reasons - easier to edit, #ifdef, etc.



                                                                                For that matter, in Perl I will do



                                                                                @array = (  
                                                                                elem1,
                                                                                elem2,
                                                                                ...
                                                                                elem1,
                                                                                ) {



                                                                                • note the comma at the end of the list. I forget if commas are separators or delimiters in C and C++ lists. IMHO this is one thing we have learned: [Are trailing commas in Perl a bad practice? commas] are a good thing. Like any new notation, it takes a while to get used to.


                                                                                I compare the if(0) code to lisp



                                                                                (cond   (test1    action1)
                                                                                (test2 action2)
                                                                                ...
                                                                                (testn actionn))


                                                                                which, you guessed it, I may indent as



                                                                                (cond   
                                                                                (test1 action1)
                                                                                (test2 action2)
                                                                                ...
                                                                                (testn actionn)
                                                                                )


                                                                                I have sometimes tried to imagine what a more human readable syntax for this might look like.



                                                                                Perhaps



                                                                                IF
                                                                                :: cond1 THEN code1
                                                                                :: cond2 THEN code2
                                                                                ...
                                                                                :: condN THEN codeN
                                                                                FI


                                                                                inspired by Dikstra's [https://en.wikipedia.org/wiki/Guarded_Command_Language#Selection:_if][Guarded Command Language].



                                                                                But this syntax implies that the conditions are evaluated in parallel, whereas if...else-if implies sequential and prioritized evaluation of conditions.



                                                                                I started doing this sort of thing when writing programs that generated other programs, where it is especially convenient.



                                                                                While we are at it, when writing RTL using Intel's old iHDL, I have coded stuff like



                                                                                   IF 0 THEN /*nothing*/
                                                                                **FORC i FROM 1 TO 10 DOC**
                                                                                ELSE IF signal%i% THEN
                                                                                // stuff to do if signal%i% is active
                                                                                **ENDC**
                                                                                ELSE
                                                                                // nothing matched
                                                                                ENDIF


                                                                                where the FORC..DOC..ENDC is a macro preprocessor loop construct, that expands to



                                                                                   IF 0 THEN /*nothing*/
                                                                                ELSE IF signal1 THEN
                                                                                // stuff to do if signal1 is active
                                                                                ELSE IF signal2 THEN
                                                                                // stuff to do if signal2 is active
                                                                                ...
                                                                                ELSE IF signal100 THEN
                                                                                // stuff to do if signal100 is active
                                                                                ELSE
                                                                                // nothing matched
                                                                                ENDIF


                                                                                This was single assignment, non-imperative, code, so setting a state variable was not allowed, if you needed to do things like find first set bit.



                                                                                   IF 0 THEN /*nothing*/
                                                                                ELSE IF signal1 THEN
                                                                                found := 1
                                                                                ELSE IF signal2 THEN
                                                                                found := 2
                                                                                ...
                                                                                ELSE IF signal100 THEN
                                                                                found := 100
                                                                                ELSE
                                                                                // nothing matched
                                                                                ENDIF


                                                                                Come to think of it, this may have been the first place that I encountered such constructs.



                                                                                BTW, the objections that some had to the if(0) style - that the else-if-conditions are sequentially dependent and cannot be arbitrarily reordered - do not apply to AND and OR and XOR logic in RTL - but do apply to short-circuit && and ||.






                                                                                share|improve this answer















                                                                                @PSkocik's answer is fine, but I add my two cents. Unsure if I should do this as a comment, or as an answer; choosing the latter, because IMHO worth others seeing, whereas comments are frequently invisible.



                                                                                Not only do I occasionally use



                                                                                if(0) {
                                                                                //deliberately left empty
                                                                                } else if( cond1 ) {
                                                                                //deliberately left empty
                                                                                } else if( cond2 ) {
                                                                                //deliberately left empty
                                                                                ...
                                                                                } else {
                                                                                // no conditions matched
                                                                                }


                                                                                But I also occasionally do



                                                                                if( 1 
                                                                                && cond1
                                                                                && cond2
                                                                                ...
                                                                                && condN
                                                                                ) {


                                                                                or



                                                                                if( 0 
                                                                                || cond1
                                                                                || cond2
                                                                                ...
                                                                                || condN
                                                                                ) {


                                                                                for complicated conditions. For the same reasons - easier to edit, #ifdef, etc.



                                                                                For that matter, in Perl I will do



                                                                                @array = (  
                                                                                elem1,
                                                                                elem2,
                                                                                ...
                                                                                elem1,
                                                                                ) {



                                                                                • note the comma at the end of the list. I forget if commas are separators or delimiters in C and C++ lists. IMHO this is one thing we have learned: [Are trailing commas in Perl a bad practice? commas] are a good thing. Like any new notation, it takes a while to get used to.


                                                                                I compare the if(0) code to lisp



                                                                                (cond   (test1    action1)
                                                                                (test2 action2)
                                                                                ...
                                                                                (testn actionn))


                                                                                which, you guessed it, I may indent as



                                                                                (cond   
                                                                                (test1 action1)
                                                                                (test2 action2)
                                                                                ...
                                                                                (testn actionn)
                                                                                )


                                                                                I have sometimes tried to imagine what a more human readable syntax for this might look like.



                                                                                Perhaps



                                                                                IF
                                                                                :: cond1 THEN code1
                                                                                :: cond2 THEN code2
                                                                                ...
                                                                                :: condN THEN codeN
                                                                                FI


                                                                                inspired by Dikstra's [https://en.wikipedia.org/wiki/Guarded_Command_Language#Selection:_if][Guarded Command Language].



                                                                                But this syntax implies that the conditions are evaluated in parallel, whereas if...else-if implies sequential and prioritized evaluation of conditions.



                                                                                I started doing this sort of thing when writing programs that generated other programs, where it is especially convenient.



                                                                                While we are at it, when writing RTL using Intel's old iHDL, I have coded stuff like



                                                                                   IF 0 THEN /*nothing*/
                                                                                **FORC i FROM 1 TO 10 DOC**
                                                                                ELSE IF signal%i% THEN
                                                                                // stuff to do if signal%i% is active
                                                                                **ENDC**
                                                                                ELSE
                                                                                // nothing matched
                                                                                ENDIF


                                                                                where the FORC..DOC..ENDC is a macro preprocessor loop construct, that expands to



                                                                                   IF 0 THEN /*nothing*/
                                                                                ELSE IF signal1 THEN
                                                                                // stuff to do if signal1 is active
                                                                                ELSE IF signal2 THEN
                                                                                // stuff to do if signal2 is active
                                                                                ...
                                                                                ELSE IF signal100 THEN
                                                                                // stuff to do if signal100 is active
                                                                                ELSE
                                                                                // nothing matched
                                                                                ENDIF


                                                                                This was single assignment, non-imperative, code, so setting a state variable was not allowed, if you needed to do things like find first set bit.



                                                                                   IF 0 THEN /*nothing*/
                                                                                ELSE IF signal1 THEN
                                                                                found := 1
                                                                                ELSE IF signal2 THEN
                                                                                found := 2
                                                                                ...
                                                                                ELSE IF signal100 THEN
                                                                                found := 100
                                                                                ELSE
                                                                                // nothing matched
                                                                                ENDIF


                                                                                Come to think of it, this may have been the first place that I encountered such constructs.



                                                                                BTW, the objections that some had to the if(0) style - that the else-if-conditions are sequentially dependent and cannot be arbitrarily reordered - do not apply to AND and OR and XOR logic in RTL - but do apply to short-circuit && and ||.







                                                                                share|improve this answer














                                                                                share|improve this answer



                                                                                share|improve this answer








                                                                                edited Dec 15 '18 at 20:22

























                                                                                answered Nov 29 '18 at 2:57









                                                                                Krazy GlewKrazy Glew

                                                                                4,8123653




                                                                                4,8123653























                                                                                    -2














                                                                                    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




























                                                                                      -2














                                                                                      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


























                                                                                        -2












                                                                                        -2








                                                                                        -2







                                                                                        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 Nov 14 '18 at 17:55









                                                                                        John UJohn U

                                                                                        1,65111530




                                                                                        1,65111530






























                                                                                            draft saved

                                                                                            draft discarded




















































                                                                                            Thanks for contributing an answer to Stack Overflow!


                                                                                            • Please be sure to answer the question. Provide details and share your research!

                                                                                            But avoid



                                                                                            • Asking for help, clarification, or responding to other answers.

                                                                                            • Making statements based on opinion; back them up with references or personal experience.


                                                                                            To learn more, see our tips on writing great answers.




                                                                                            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

                                                                                            Bressuire

                                                                                            Vorschmack

                                                                                            Quarantine