From: Scott Lurndal on
Poster Matt <postermatt(a)no_spam_for_me.org> writes:
>Hi,
>
>I've a few questions concerning style when programming C on UNIX systems. I
>don't want to look like an amateur. :)
>
>1. Having been programming in higher level languages for the last 15 years, I'm
>finding it hard to get used to DEFINES in all capitals. Is it really frowned on
>not to do so? Is CamelCase acceptable?
>
>EG. '#define MaxNumFiles 1024' not '#define MAXNUMFILES 1024'.

Well, defines are macros, not constants, so I try to avoid them for this use.
With C99 and C++, one should probably use static const
<type> (where type a signed or unsigned integer of the appropriate width).

e.g.
static const unsigned int MaxNumFiles = 1024;

For C++, the visibility should be the most restrictive possible
(i.e. private if the constant is only used by class members).

This allows the compiler to use more strict type checking with the
constants, and signedness is explicit.

>
>
>2. My personal variable and function naming style is camel case, with variable
>names beginning with a lower case char and function names not. Is that
>acceptable, if not what is?
>
>EG:
>Variables: int numFiles = 0;
>Functions: int CountNumFilesInDir(char* path);

There are many different conventions; I prefer underscores myself.

I also prefer
int
count_dir_entries(const char *path)

or
inline int
c_class::count_dir_entries(const char *path)

(With vi/vim, ^functioname will move the cursor to start of
the function if it starts the line).

>
>
>3. Is there an accepted maximum line length? I've got a 24" monitor, if I reach
>120 chars I start thinking this might not look great in someone else's editor.
>

80. Please.



>
>5. On a slightly different note, I've been handling my error messages by using
>#define string constants in a header file. I saw some code which did this and it
>looked good to me. Is that standard practise, if not what is?
>
>EG. #define ErrorDirNotFound "The directory was not found."

Bad practice. The string value will be duplicated in every compilation
unit that uses it. Better is to have a function that returns a const char *
given an error message number (e.g. strerror(3)).

In general, for your own code, follow what you like (or your employer requires).

If you are modifying existing code, follow the existing style.

scott
From: Scott Lurndal on
James Harris <james.harris.1(a)googlemail.com> writes:
>On 24 Feb, 20:53, BruceS <bruce...(a)hotmail.com> wrote:
>
>...
>
>> I would like to add that, as long as you're trying to use good style,
>> for God's sake don't use the wrong indentation style. =A0If you put your
>> opening braces on the same line as your conditional, you'll just look
>> like a fool in front of your friends and colleagues.
>
>Snobbish nonsense!
>
>

Indeed.

if (condition) {

is preferred over

if (condition)
{

Makes it much more likely that a properly written function will fit on a single
page/screen.

In 30 years of C programming, no employer or project has used the latter form.

scott
From: Scott Lurndal on
Chris Friesen <cbf123(a)mail.usask.ca> writes:
>On 02/24/2010 12:35 PM, Poster Matt wrote:

>5) error messages are in the code explicitly

In order to effectively i18n the code, this practice should
be avoided. Rather, one should use gettext or equiv.

scott
From: Anand Hariharan on
On Feb 24, 12:35 pm, Poster Matt <postermatt(a)no_spam_for_me.org>
wrote:
> Hi,
>
> I've a few questions concerning style when programming C on UNIX systems. I
> don't want to look like an amateur. :)
>
> 1. Having been programming in higher level languages for the last 15 years, I'm
> finding it hard to get used to DEFINES in all capitals. Is it really frowned on
> not to do so? Is CamelCase acceptable?
>
> EG. '#define MaxNumFiles 1024' not '#define MAXNUMFILES 1024'.
>

As see from the umpteen replies, you are not going to get one answer
when it comes to a style-related question.

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.
From: Ersek, Laszlo on
In article <3qhhn.9821$8y6.7846(a)news.usenetserver.com>, scott(a)slp53.sl.home (Scott Lurndal) writes:

> static const unsigned int MaxNumFiles = 1024;
>
> [...]
>
> This allows the compiler to use more strict type checking with the
> constants, and signedness is explicit.

#define MAX_FILES 1024u
#define BUFSIZE ((size_t)4096)

(Yes, when I *use* MAX_FILES, I don't necessarily remember its type. But
in that situation the same applies to "MaxNumFiles": we have to look up
the definition.)

Signedness is equally explicit in

#define MAX_FILES 1024

as 1024 is ((int)1024) -- that macro definition is just ugly.

Cheers,
lacos
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Prev: integer
Next: shared memory question