Skip to main content
GameDev.net gamedev.net
🔒 Locked

Deprecation fail

Started by NEXUSKill May 15, 2013 at 8:01 PM 13 replies 5.3k views
Original Post
NEXUSKill
NEXUSKill

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


frob
frob

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.

Sik_the_hedgehog
Sik_the_hedgehog


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.
NEXUSKill
NEXUSKill

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


froop
froop

things get ugly with nested dead code


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

	deadcode();
/*
}
*/
Servant of the Lord
Servant of the Lord

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: /* / * * / */

cr88192
cr88192


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;

...

Bacterius
Bacterius


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.”
cr88192
cr88192


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.

I am not sure why people made the comments not nest.

in my language they do nest.

however, this can't be as-easily added to existing languages mostly as there is a risk of it breaking existing code (which may depend on the non-nesting behavior of these comments).

(decided to leave out a rant about the tendency of people to retain problematic edge-case behaviors in new languages with C-like syntax... even though a new language doesn't necessarily have piles of legacy code to maintain, and the new language will probably break stuff in other areas anyways, so some obscure edge-case behaviors are probably better off fixed as they are probably more often stumbled on by accident than by intention...).

Sik_the_hedgehog
Sik_the_hedgehog


I am not sure why people made the comments not nest.

Remember when C was made and it isn't hard to understand why. Basically when the preprocessor finds /* it keeps skipping characters until it finds a */ sequence, without doing anything else. I guess implementing nesting wouldn't have been hard (it's just a counter!), but that's what they decided to go with.


however, this can't be as-easily added to existing languages mostly as there is a risk of it breaking existing code (which may depend on the non-nesting behavior of these comments).

For the record, several old C compilers actually would parse comments like they could be nested. I know Borland C used to do this.

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.
iMalc
iMalc

There could actually be a valid reason for putting the while(0) around it rather than commenting it out.

It avoids the situation where some later maintanence or refactoring caused the commented out code to not compile. That can help for when it starts getting used again.

Ectara
Ectara


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: /* / * * / */

That would likely break things like Doxygen, where comment blocks beginning with /** are parsed to generate documentation.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.