zwol: stylized sketch of a face in profile (Default)
[personal profile] zwol
if (constant == variable)
if (pointer != 0)
if (pointer != NULL)
if (boolean == true)

}
else {

fprintf(stdout, ...);

printf("%s", variable);
printf("string with no substitutions");
printf("\n"); // or any other single character

Date: 2009-08-15 06:40 am (UTC)
brooksmoses: (Default)
From: [personal profile] brooksmoses
Hmm. Now I'm concerned, since I do the last two. What's wrong with those (and the first one)?

Date: 2009-08-15 11:10 pm (UTC)
From: [identity profile] zwol.livejournal.com
The examples involving printf() are less troublesome than the rest -- as [livejournal.com profile] elsmi points out, consistent use of the most general thing in the toolkit (printf, in this case) means less chance of bugs. However, printf has enormous overhead compared to the non-formatting stdio routines, so where you're doing a lot of output, I want to see fputs(variable, stdout) etc.

I'd be quite a bit more dogmatic about this if puts() didn't tack on a newline. But it does. Bleh.

Date: 2009-08-15 11:10 pm (UTC)
From: [identity profile] zwol.livejournal.com
Re the first one, see my reply to [livejournal.com profile] aldren below.

Date: 2009-08-15 09:09 am (UTC)
From: [identity profile] aldren.livejournal.com
The first one is not a bad practice at all. It's saved me before by generating a compiler error when doing something like:
if( 4 = variable ) {
    ...
}


And as for doing an fprintf to stdout, I've done that when I previously had code printing to stderr, but went through and changed it. Though I suppose a regexp could have replaced the fprintf with a printf...

Date: 2009-08-15 11:17 pm (UTC)
From: [identity profile] zwol.livejournal.com
I've heard people say this before and I don't buy it. It is much easier to read if one consistently puts the variable on the left, particularly in a long chain of if-else-if all involving the same variable.

The compiler should be complaining about "if (variable = 4)", and I find that I basically never miss one of those when reading, anyway.

fprintf(stdout,...) as a lazy man's conversion from fprintf(stderr,...) makes a lot of sense in the context of the code that prompted the original post; although what really bugged me about that was it was an inconsistent mishmosh of both.

Date: 2009-08-15 09:27 am (UTC)
From: [identity profile] elsmi.livejournal.com
The last three are handy for uniformity. Most errors occur when someone goes starts thinking, right?

(In particular, I'd hesitate to deprecate printf("%s", variable) when its nearest cousin -- printf(variable) -- is so very nasty. Also, printf("%s", variable) gives you protection when variable may be NULL.)

Date: 2009-08-15 11:22 pm (UTC)
From: [identity profile] zwol.livejournal.com
You have an excellent point. Mainly I was grumping about those because the code I was working on couldn't even be consistent about using one of printf() and fprintf(stdout).

Date: 2009-08-15 01:33 pm (UTC)
From: [identity profile] superconductorz.livejournal.com
The first 4 seem sound to me. Any modern compiler will catch if(variable = constant), unless you have disabled warnings, in which case that's your real problem, and it reads easier than putting the constant first.

The rest I don't quite get.

I assume the 5th is in reference to this specific brace placement, but I'm not quite sure I see your point.

On the last, is the idea s/printf("\n")/putchar('\n')/? It seems better to me to always use printf here, so that when you suddenly need '\n\n' the code rewriting is minimal. (Of course generally I would say: don't use printf for anything).

Date: 2009-08-15 11:21 pm (UTC)
From: [identity profile] zwol.livejournal.com
The problem with the fifth example is it's inconsistent, it should be either "} else {" -- all on one line -- or both curly braces should be on their own lines.

Yeah, on the last few, the idea is there are more efficient ways to write it. I don't think there's anything wrong with printf() when you actually need formatting, but when you don't (and the speed matters) you really want to be using fputs() and putchar().

April 2017

S M T W T F S
      1
2345678
9101112131415
16171819 202122
23242526272829
30      

Style Credit

Expand Cut Tags

No cut tags
Page generated Jan. 10th, 2026 09:27 am
Powered by Dreamwidth Studios