Prev: integer
Next: shared memory question
From: Richard Heathfield on 24 Feb 2010 17:33 Scott Lurndal wrote: <snip> > > if (condition) { > > is preferred over > > if (condition) > { Except, of course, by people who prefer the latter to the former. <snip> -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ "Usenet is a strange place" - dmr 29 July 1999 Sig line vacant - apply within
From: Richard Heathfield on 24 Feb 2010 17:34 Anand Hariharan wrote: <snip> > Haven't seen anyone point this out: > > Rather than - > > #define MAXNUMFILES 1024 > > - prefer - > > const int MaxNumFiles = 1024; > > > That way your preprocessor won't do as much damage. Fine in C99, I think, but an issue in C90 if he's using it to define an array size. -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ "Usenet is a strange place" - dmr 29 July 1999 Sig line vacant - apply within
From: Keith Thompson on 24 Feb 2010 17:04 Stephen Sprunk <stephen(a)sprunk.org> writes: > On 24 Feb 2010 12:35, Poster Matt wrote: [...] >> EG: >> Variables: int numFiles = 0; > > This is camelCase. > >> Functions: int CountNumFilesInDir(char* path); > > This is PascalCase. > > Mixing the two in the same project will drive adherents of _both_ styles > nuts. Pick one and stick to it; that way you'll only drive half of your > readers nuts. His convention apparently is to use camelCase for variables and PascalCase for functions. It's not necessarily a style I'd use, but it's not obviously horrible (and it's more or less the style I use at work). As with most of these rules, conforming to existing code is far more important than any benefits of one style over another. I really dislike the brace placement of the code I work on, but mixing my own style into it would be far worse (and wouldn't survive a code review anyway). > (There's also this_type_of_identifier; the same logic applies, i.e. > don't mix that with either of the above. Never, never create some > God-awful hybrid with both underscores and uppercase letters...) Again, This_Type_Of_Identifier isn't obviously horrible. (I use it myself, though not in C.) [...] -- Keith Thompson (The_Other_Keith) kst-u(a)mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"
From: Anand Hariharan on 24 Feb 2010 17:42 On Feb 24, 4:34 pm, Richard Heathfield <r...(a)see.sig.invalid> wrote: > Anand Hariharan wrote: > > <snip> > > > Haven't seen anyone point this out: > > > Rather than - > > > #define MAXNUMFILES 1024 > > > - prefer - > > > const int MaxNumFiles = 1024; > > > That way your preprocessor won't do as much damage. > > Fine in C99, I think, but an issue in C90 if he's using it to define an > array size. > Good point (and that's probably why no one mentioned it?). Even in C99, it is not your good old array but a 'Variable' Length Array. Does the standard have anything to say about VLAs being automatic storage or free store, but cleans itself up when they go out of scope?
From: Ben Bacarisse on 24 Feb 2010 17:46
Richard Heathfield <rjh(a)see.sig.invalid> writes: > Anand Hariharan wrote: > <snip> > >> Haven't seen anyone point this out: >> >> Rather than - >> >> #define MAXNUMFILES 1024 >> >> - prefer - >> >> const int MaxNumFiles = 1024; >> >> >> That way your preprocessor won't do as much damage. > > Fine in C99, I think, but an issue in C90 if he's using it to define > an array size. It's a problem in C99 too, if the array is defined at file scope or it has internal linkage. There are other reasons why it's not a great idea in C99. They stem from the fact that MaxNumFiles is not permitted as part of a constant expression. There are several slight variations depending on what sort of constant expression is required but I think the simplest things is to say that using the above will confuse someone sometime and is therefore not usually considered good style even in C99. -- Ben. |