Jump to content

  • Log In with Google      Sign In   
  • Create Account


Deprecation fail

  • You cannot reply to this topic
14 replies to this topic

#1 NEXUSKill   Members   -  Reputation: 444

Like
5Likes
Like

Posted 15 May 2013 - 02:01 PM

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



Sponsor:

#2 Josh Klint   Crossbones+   -  Reputation: 1347

Like
0Likes
Like

Posted 15 May 2013 - 02:46 PM

Was the code originally a while loop?


Edited by Josh Klint, 15 May 2013 - 02:48 PM.

Build mobile games with native code

http://www.leadwerks.com


#3 frob   Moderators   -  Reputation: 18363

Like
4Likes
Like

Posted 15 May 2013 - 03:41 PM

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.


Check out my personal indie blog at bryanwagstaff.com.

#4 Sik_the_hedgehog   Crossbones+   -  Reputation: 1467

Like
2Likes
Like

Posted 16 May 2013 - 09:55 AM

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.

#5 NEXUSKill   Members   -  Reputation: 444

Like
0Likes
Like

Posted 16 May 2013 - 11:47 AM

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



#6 froop   Members   -  Reputation: 636

Like
1Likes
Like

Posted 16 May 2013 - 07:46 PM

things get ugly with nested dead code

 

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

	deadcode();
/*
}
*/

Edited by froop, 16 May 2013 - 07:47 PM.


#7 BGB   Crossbones+   -  Reputation: 1545

Like
0Likes
Like

Posted 16 May 2013 - 09:30 PM

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

 

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



#8 Servant of the Lord   Crossbones+   -  Reputation: 16675

Like
0Likes
Like

Posted 16 May 2013 - 09:35 PM

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


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#9 BGB   Crossbones+   -  Reputation: 1545

Like
0Likes
Like

Posted 17 May 2013 - 02:50 AM

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;

...



#10 Bacterius   Crossbones+   -  Reputation: 7963

Like
0Likes
Like

Posted 17 May 2013 - 07:42 AM

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.


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#11 BGB   Crossbones+   -  Reputation: 1545

Like
0Likes
Like

Posted 17 May 2013 - 09:09 AM

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


Edited by cr88192, 17 May 2013 - 11:35 AM.


#12 Sik_the_hedgehog   Crossbones+   -  Reputation: 1467

Like
0Likes
Like

Posted 17 May 2013 - 02:12 PM

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.

#13 Servant of the Lord   Crossbones+   -  Reputation: 16675

Like
0Likes
Like

Posted 17 May 2013 - 04:47 PM

A GCC extension would be nice.


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#14 iMalc   Crossbones+   -  Reputation: 2250

Like
0Likes
Like

Posted 02 June 2013 - 03:35 PM

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.


Edited by iMalc, 02 June 2013 - 03:35 PM.

"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

#15 Ectara   Crossbones+   -  Reputation: 2741

Like
0Likes
Like

Posted 03 June 2013 - 05:53 PM

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.







PARTNERS