📜 ⬆️ ⬇️

What's the point?

SomeFunction() { ... if (false) { ... } ... } 

Why might an if (false) block be needed?

Let's pretend that:
  1. This is not a mistake and not forgetfulness.
  2. if and false are precisely the conditional operator and logical false, and not some tricks with macros \ strings, etc.

A couple of answers under the cut. Those who wish to express their ideas are invited by the otgadki immediately in the comments .

Two inadequate options

  1. The programmer needed to create the appearance of work \ paid for the number of lines.
    Stupid, not interesting, but it happens. We drove.
  2. Somewhere in this function there is a goto into the if block (false)
    In general, I am not an ideological opponent of goto - sometimes its use is justified. But the transition into an unattainable condition - here or just a mess in the code, or some such deep sacred meaning that I can not understand it. We drove.

Two adequate options


1. Use the if (false) block to temporarily comment on the code.
Yes, of course, we have \\ , / ** / (well, or whatever the comment means in your language). But with these things there are the following troubles:

When commenting with the if (false) block, we are relieved of some of these difficulties.

2. Sometimes performing the necessary operations in debug mode
In the process of step-by-step debugging (for example, in Visual Studio) we can move the current point of the program execution, including we can move it inside the never-executed if block.
')


Why this may be needed? Well, here are a couple of options:
  1. In this block, you can make some extra-heavy logging (well, for example, a dump of the entire memory). In the normal mode (and even in the debag configuration), we do not need this, but sometimes, during the debugging of a hard place, it may be necessary. Wrap this dump in if (false) and at the right moment jump inside it, then back.
  2. In the course of the same debugging, we need to transfer the object to a certain reference state for the test. Moreover, we don’t need it either in the release or in the debug - only now for debugging. Plus, perhaps in the future - if problems arise. We wrap the object's initialization in if (false) - this guarantees us the presence of code in the program and at the right moment we transfer the execution inside.
Thus, we get a code that will never be called by the program itself, but to which we can, if desired, be able to “jump” in the course of debugging.

Source: https://habr.com/ru/post/137385/


All Articles