[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: coding style



Hi. It is easy for coding style to spark a religious war, and I'm not
interested in futile debate. So please bear in mind that my comments are my
observations, not a call-to-arms.

Coding style is motivated by important but incompatible goals.

1. Readability of the code
2. Compatibility with existing code style
3. Ego and officiousness

I've listed these in order of importance, but ranking is a matter of
opinion.

The K&R C style is to use hanging right open braces. The Stroustrup C++
style is to use aligned left open braces. I asked Stroustrup why he chose a
style contrary to existing C code, and he responded that all human cognition
studies indicate that self-consistent alignment is easier to comprehend.
However, there is a caveat. If a person is already trained in another system
then the confusion of retraining can be more than the advantage of the
generally more obvious approach.

So, C++ indentation style is superior with all things equal, even for new C
programmers. Compatibility with existing code in C style and the retraining
inconvenience to existing C programmers may make C style a better compromise
in some circumstances.

>From a typing standpoint, tabs are much less effort for most than spaces.

There are good reasons to desire coding style, but sometimes it is about
power. The third goal, listed but not presumed here, is to have an excuse to
interfere.

On a practical note there are tools to convert from one style to another. It
is possible to have programmers use any style, then make the code consistent
by machine. If absolute consistency is a goal, this is the only workable
approach.

Cheers,

Robin
---------------------------------------------------------------------------
www.LinuxMovies.org
http://filmgimp.sourceforge.net
www.OpenSourceProgrammers.org

----- Original Message -----
From: "Tollef Fog Heen" <tollef@add.no>
To: <debian-boot@lists.debian.org>
Sent: Sunday, November 03, 2002 11:38 AM
Subject: codingstyle?


>
> After multiple proddings by Martin Sjögren, I've finally gotten around
> to writing down at least something which can become a coding style wrt
> bracing, tabs/notabs and such.
>
> This is not a final version.  This is what I'd like to see.
>
> no tabs.  spaces.  four spaces.
> opening braces on opening line
> closing braces unindented to the correct level
>
> like:
>
>         if (x is true) {
>                 we do y
>         }
>
> and as in CodingStyle from the kernel:
>
> Note that the closing brace is empty on a line of its own, _except_ in
> the cases where it is followed by a continuation of the same statement,
> ie a "while" in a do-statement or an "else" in an if-statement, like
> this:
>
>         do {
>                 body of do-loop
>         } while (condition);
>
> and
>
>         if (x == y) {
>                 ..
>         } else if (x > y) {
>                 ...
>         } else {
>                 ....
>         }
>
> Rationale: K&R.
>
> Name stuff sanely.  Try to stay away from globals.
>
> (yes, this pretty loose, basically it's K&R.)
>
> Any thoughts, ideas, flames?
>
> --
> Tollef Fog Heen
,''`.
> UNIX is user friendly, it's just picky about who its friends are      : :'
:
>                                                                       `.
`'
>                                                                         `-
>
>
> --
> To UNSUBSCRIBE, email to debian-boot-request@lists.debian.org
> with a subject of "unsubscribe". Trouble? Contact
listmaster@lists.debian.org
>
>



Reply to: