The meaning of the strange side effect inside if
I was browsing the project and came across this:
if(!StaticAnimatedEntities)
int esko = esko = 2;
(The type StaticAnimatedEntities
here is a simple unsigned character.)
It seemed to me that this is very strange, so I followed the project esko
and found another similar if
one with only that line inside them, f.ex. this is:
if(ItemIDMap.find(ID) != ItemIDMap.end())
int esko = esko = 2;
(Note that there are no other named variables esko
outside of these if
s.)
What's the point of this cryptic piece of code?
source to share
Sometimes you may see code like this just to serve as a bind-point, to put a breakpoint in the interactive debugger to catch some "unusual" (most often erroneous) conditions.
Conditional breakpoints provided by the debugger are usually very slow and / or primitive, so people often deliberately plan ahead and provide such conditional branches to create a compiled location for the breakpoint. Such a compiled conditional breakpoint does not slow down program execution in much the same way as a conditional breakpoint provided by the debugger.
In many cases, such code is surrounded #ifndef NDEBUG/#endif
to prevent it from getting into production builds. In other cases, people just leave it unprotected, believing that the optimizing compiler will remove it anyway.
For it to work, the pod code if
must generate some machine code in debug builds. Otherwise, it would be impossible to set a breakpoint. Different people have different preferences in this regard, but the code almost always looks strange and meaningless.
It's the pointlessness of this code, which gives programmers complete freedom to write whatever they want there. And I would say that it often becomes a part of every signature style of programmers, fingerprints. The guy in question does int esko = esko = 2;
apparently.
source to share