Deprecation fail

Started by
13 comments, last by Ectara 10 years, 10 months ago

I once found this in a c++ long method while searching for optimizable loops and unnecessary processes, it was a rendering update for a skeletal animated actor, the method itself was a good 150 lines long total and in the midst of its implementation there was this while loop:


//The following block of code should not be executed anymore, keeping it around for fallback reasons
while(0)
{
    ... do about 20 lines of stuff...
}


Seeing this actually hung my brain for about a minute before I yelled "Who the F* doesn't know how to comment a block of code on C++?" to the whole office.

The perpetrator was no longer working for the company and was responsible for many ridiculous pieces of code of which this was one of the last that didn't get deleted and fell through the cracks.

Granted, the code didn't actually get executed and the compiler likely did away with the whole loop, but it was still blasphemous.

Game making is godlike

LinkedIn profile: http://ar.linkedin.com/pub/andres-ricardo-chamarra/2a/28a/272


Advertisement

Was the code originally a while loop?

10x Faster Performance for VR: www.ultraengine.com

That's not too uncommon in big code bases. Using an if instead of a while is more frequent.


#define __FEATURENAME__ 0

if (__FEATURENAME__)
{
 // somewhat dead code here.
}
 

There are several benefits to this.

The most immediate and obvious benefit is in cross-platform code or multi-build code. If you use the #if preprocessor directive it is easy for a programmer to mess up stuff inside an #if/#endif block that happens to not compile on their system. In this case the compiler will still build the code and run all the syntax checks and such.

Another benefit is that while debugging, a programmer can step into the "dead" loop in debug builds. In that case you probably want to mark it with a descriptive name such as DEBUG_ONLY_TEST_SOME_FEATURE, but leaving the code in place makes it mostly dead but still partially alive.

Now on the other hand, if the code really is dead and has no use, nuke it from orbit. If you need it later you can use version control to recover it.

Granted, the code didn't actually get executed and the compiler likely did away with the whole loop, but it was still blasphemous.

Yep, it probably isn't even present in the executable. The main advantage of removing code this way is that the compiler still has to check its syntax is valid which can help keeping it usable if you ever need it back. Of course, chances are by the time you go back to the code the program behavior changed so much that the old code isn't useful anymore anyway =P

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

Was the code originally a while loop?


No, the while was added as a sort of /**/ block comment.
Personally I believe dead code like this should be outright deleted, especially if you have some subversion system in place, having to browse through dead code and in this case even maintain it is a huge waste of time in production.

Game making is godlike

LinkedIn profile: http://ar.linkedin.com/pub/andres-ricardo-chamarra/2a/28a/272


things get ugly with nested dead code


/*
void deadcode()
{
*/
	/*
		deadcode();
	*/

	deadcode();
/*
}
*/

some of this is an area where nestable block-comments would be useful sometimes.

granted, there is always #if 0 ... #endif, but still...

things get ugly with nested dead code

Yea, I wish C++ allowed: /*** /** /* */ **/ ***/, and so on. Sometimes I use /* */ for multiline comments (that's what it's for, after all) and trying to comment out several functions means I need to go and add a space after the stars: /* / * * / */

things get ugly with nested dead code

Yea, I wish C++ allowed: /*** /** /* */ **/ ***/, and so on. Sometimes I use /* */ for multiline comments (that's what it's for, after all) and trying to comment out several functions means I need to go and add a space after the stars: /* / * * / */

or, possibly alternate syntax:

/[[ ... ]]/

which could be nestable, and shouldn't likely interfere with any other valid constructions (and wouldn't break on documentation-comments for Doxygen or similar).

if I could have a wish-list, I might also add:

endianess specifiers for struct members;

triple-quoted strings or similar;

...

granted, there is always #if 0 ... #endif, but still...

I like #if 0. I have my editor setup to identify such "frozen code" blocks and highlight them in a different color (generally light gray or light blue, different from comments). I find it handy to temporarily remove large blocks of code for debugging or prototyping. I never leave this stuff after, though, it just looks ugly and confusing.

Sure, it could probably be argued that it's not using the preprocessor for its intended usage, but what the hell, it's an idiom now so who cares wink.png

I wouldn't mind a clean nested comment feature for C++, though. It should probably nest by default, anyway. What was the reason for not allowing /* /* */ */? Harder/impossible to parse or something? I doubt it so there must be some other explanation.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

This topic is closed to new replies.

Advertisement