TIP: Click on subject to list as thread! ANSI
echo: artware
to: Meikel Brandmeyer
from: andrew clarke
date: 2003-03-29 06:03:32
subject: coding style

Fri 2003-03-28 16:46, Meikel Brandmeyer (2:244/1165) wrote to All:

 MB> during the work on my last patch I noticed the complete chaos of
 MB> naming function and variables.

A while ago I worked on cleaning-up some of the source files, but there
is still work left to do in that area, but it's rather time consuming
and not very interesting.

 MB> I noticed at least three different
 MB> styles:
 MB>  - foobar()
 MB>  - foo_bar()
 MB>  - FooBar()
 MB> Maybe we should work out a style, which is used in new code and to
 MB> which old code can be migrated step by step. Right now the code
 MB> looks like a mess.

foobar() or fooBar() or FooBar() are fine with me.  I prefer not to use
'_' because occasionally I'll type '-' leading to odd syntax errors,
etc.

I wouldn't bother with changing the all function names of the old code
unless you can work out some automated way of doing it.  This is not
necessarily as easy as just doing a string replace on all the source
files, because you can end up changing things that aren't function
names, eg. string literals.

 MB>  - indent is 4 spaces (no tabs)

OK.

 MB>  - global variables are named with the 'FooBar'-scheme

Put them all in a struct, eg.

static struct
{
    word NumMacros;
}
glob;

Or similar.  Care will be needed to ensure the variables are set to the
correct values at runtime, so have a function named to do that and call
it from main().

static void InitGlobals(void)
{
    glob.NumMacros = 0;
}

Of course if you declare the struct "static" then all variables in the
struct will be set to zero, so you only have to set the variables that
aren't meant to be zero (if there are any) in InitGlobals.

It may be useful to calloc glob instead of using a static struct, but
you'd need to make this decision early on, because you'd then need to
use "glob->" instead of "glob." throughout the code.

 MB>  - global functions are named with the 'foo_bar'-scheme with some prefix
 MB>  - local (static) functions are named with the 'foo_bar'-scheme

I tend to think there are a lot of functions that aren't marked static
that should be.  Working out which ones they are sounds rather laborious
without automated tools.

 MB>  - only one way of commenting. I prefer '/**/', but '//' is also
possible (C99)

/**/ is fine.

 MB>  - never use strcpy, etc. Always use strncpy, etc. Maybe one should
 MB> add wrapper for those. (something like strlcpy, which ensures, that
 MB> a '\0' is present at the end)

OK.

 MB> I'd like to suggest also to include some parts of style(9) of
 MB> FreeBSD. Most of that resembles my opinion on good coding style.

Which ones?  But I probably agree.

 MB> Something, what looks strange on the first sight, but is much more
 MB> readable than 

 MB> char *foo;
 MB> unsigned int bla;
 MB> int bar, baz;

 MB> is 

 MB> unsigned int    bla;
 MB> char           *foo;
 MB> int             bar, baz;

I find the first one more readable actually.

The following are my ~/.indent.pro settings (for GNU Indent, not BSD
Indent):

-kr
-bl
-bli0
-bls
-i4
-ts1
-nce
-ncs
-fca
-nfc1

-- mail{at}ozzmosis.com

--- timEd/FreeBSD 1.11.a5
* Origin: Blizzard of Ozz, Mt Eliza, Victoria, Australia (3:633/267)
SEEN-BY: 633/267 270
@PATH: 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.