Reverse engineering code and I’m kind of appalled at the style, but I wanted to make sure there’s no good reason for doing these things….
Is it just me or is this a horrible coding style
if ( pwbuf ) sprintf(username,"%s",pwbuf->pw_name);
else sprintf(username,"%d",user_id);
And why wrap code not intended for compilation in an
#if 0
....
#endif
Instead of comments?
EDIT: So as some explained below, this is due to the possibility to flummox /* */ which I didn’t realize.
But I still don’t understand, why not just use your programming environment tools or favorite text editor’s macro’s to block comment it out using “//”
wouldn’t this be MUCH more straightforward and easy to know to visually skip?
Am I just inexperienced in C and missing why these things might be a good idea — or is there no excuse, and I’m justified in feeling irritated at how ugly this code is?
#if 0is used pretty frequently when the removed block contains block-commentsI won’t say it’s a good practice, but I see it rather often.
The single line flow-control+statement is easy enough to understand, although I personally avoid it (and most of the coding guidelines I’ve worked under forbid it)
BTW, I’d probably edit the title to be somewhat useful “Why use #if 0 instead of block comments”
If you have the following
If you naively replace the
#if 0/#endifwith/* */, that will cause the comment to end right after flumuxiation, causing a syntax error when you hit the*/in the place of the#endifabove..EDIT: One final note, often the
#if 0syntax is just used while developing, particularly if you have to support multiple versions or dependencies or hardware platforms. It’s not unusual for the code to be modified toWith a centralized header defining (or not) hundreds of those #define constants. It’s not the prettiest thing in the world, but every time I’ve worked on a decent sized project, we’ve used some combination of runtime switches, compile-time constants (this), compile-time compilation decisions (just use different .cpp’s depending on the version), and the occasional template solution. It all depends on the details.
While you’re the developer just getting the thing working in the first place, though…
#if 0is pretty common if you’re not sure if the old code still has value.