Skip to Content.
Sympa Menu

freetds - Re: [freetds] how to write C

freetds AT lists.ibiblio.org

Subject: FreeTDS Development Group

List archive

Chronological Thread  
  • From: entropy AT freetds.org
  • To: FreeTDS Development Group <freetds AT lists.ibiblio.org>
  • Subject: Re: [freetds] how to write C
  • Date: Fri, 30 Nov 2007 21:15:18 -0500

James K. Lowden wrote:
I have an opinion I want to check with the old hands out there, because
the GNU C compiler issues a warning I think should be ignored.
Given code like this:

if (a && b || c && d)
/* ... */

the compiler "recommends" parentheses, encouraging:

if ((a && b) || (c && d))
/* ... */

Please. To he who would read the code, I say RTFM. If K&R is too long
for you, here's a short version:

The compiler doesn't need the parens, but humans do. If precedence were as simple and obvious as you claim, you wouldn't still be finding precedence bugs like

if((ret=func() != NULL))
instead of
if((ret=func()) != NULL)


in such a mature production code base.

is a good one. I fixed one like that last week. So is:

memset( ptr, len, '\0')

a favorite of mine, not least because '\0' is silly: the value argument is
defined as int.

Well the value should go in argument 2, but I don't see any problem with '\0'. Would you think "memset(ptr, 'x', 23)" was silly? Would "memset(ptr, 120, 23)" be clearer? Specifying '\0' makes it explicit that "this is a character", and making such things explicit makes code easier to read. IMNSHO, of course.

Am I missing something? Are there compilers out there that would get this
wrong? Do the parentheses give the compiler information it doesn't
otherwise have? Or is this just nanny nagging we can turn off by default?

You can probably turn it off, but I don't see anything to be gained from doing that. I like the parens, and I easily get a headache from reading too much smart-ass code written in the "lean and mean" style without them. However, this might just be evidence that I have some kind of brain damage from too much Scheme programming in my formative years. YMMV.

--
Cheers,
entropy




Archive powered by MHonArc 2.6.24.

Top of Page