Putting cyrus-future code in CVS. Also, code style

Bron Gondwana brong at fastmail.fm
Tue Jun 29 21:11:08 EDT 2010


On Tue, 29 Jun 2010 12:22 -0500, "Patrick Goetz" <pgoetz at mail.utexas.edu> wrote:
> Bron Gondwana wrote:
> > Also, a question about coding style.  Is it codified somewhere?
> > There are a few different styles throughout the code, but there
> > seems to be a fairly consistent style in most of the code.
> > 
> 
> Having spent many hours recently looking over Cyrus C/perl code (and not
> having had to spend much time looking at other people's code in the
> preceding 5-10 years), I will happily volunteer my opinion that in
> practice, and for future maintenance purposes, the K&R style:
> 
>   while (x == y) {
>       something();
>       somethingelse();
>       if (some_error) {
>           do_correct();
>           do_something_else;
>       }
>       else
>           continue_as_usual();
>   }
> 
> sucks and is hard to read, while the GNU style

That's because you're not spacing around blocks...

while (x == y) {
    something();
    somethingelse();

    if (some_error) {
        do_correct();
        do_something_else;
    }
    else
        continue_as_usual();

    more_stuff();
}

That's more readable.


>   while (x == y)
>     {
>       something ();
>       somethingelse ();
>     }
> 
> is just kind of silly.  Most readable is the Allman style:

Yeah, braindead insane I'm pretty sure Linus called it.

>   while (x == y)
>   {
>       something();
>       somethingelse();
>   }

The complaint against that is wasted whitespace.  In vi you can bounce
on % to find the opposite end of one of those easily enough anyway.
Honestly, I don't want to do a massive re-indent of the codebase.  I'd
rather codify the standards we're already using so we can insist on
maintaining them.

> That little bit of added white space really makes a difference when the
> eye is skimming down the page.  Since I always use the Allman style and
> am usually only debugging my own code, I didn't really appreciate the
> difference until looking at Cyrus code.

Familiarity as much as anything I'd say.  I've maintained code in both
styles (as well as the GNU style, ugh) and your eye trains itself to
the patterns in the code you're familiar with.  I don't place heaps of
weight on "different from what I'm familiar with" as a good indicator
of "worse".

> Indentation doesn't matter so
> much to me for readability, but I diverge from the norm by preferring a
> 2-space indentation to keep nested blocks from drifting too far off to
> the right which also creates more work for the eye when re-reading code,
> not to mention line wrap.  Mostly I don't think about this much, as vi
> does all the indentation for me as I edit.

We use 2 space for Perl internally at FastMail, There are pluses and minuses.

In Perl, the cost of making a function call inside a nested structure is
actually quite high, so it makes sense to deeply nest your inner loops
rather than calling out.

In C, not so much.  If you find yourself drifting off to the right, chances
are you should be factoring out the inner part into a function.  Linus
insists on "fits within 80 columns" and "8 character tabs for indent".  Now
that's a strict code style!  We can go twice as much indent before we run
up against the edge...

Bron.
-- 
  Bron Gondwana
  brong at fastmail.fm



More information about the Cyrus-devel mailing list