[chbot] C Coding Style

Volker Kuhlmann list0570 at paradise.net.nz
Wed Sep 21 13:04:17 BST 2016


On Wed 21 Sep 2016 22:47:06 NZST +1200, Richard Jones wrote:

> Offers anyone?

There are as many opinions as there are C programmers...
The main thing is to be consistent. Beyond that, take your personal
pick. I always tell people if they expect the rest of the world to
format to their personal preference they're going to be onto a loser,
like when they have to read other peoples' code.

By and large I like the Linux kernel style, it's verbose enough to be
legible and doesn't waste screen real estate. Some people get hyper on
this:
  if (something == 45)
  {
    for (i = ...)
    {
      a += 5;
    }
  }
  else
  {
    do something else;
  }
I find it idiotic. Lots of space for not doing much. The more of the
code I can see at once the less time I have to flick around.

The arduino one looks OK at a glance, but contains some nonsense too.

If you want to get really serious, check what Jack Ganssle has to say
about it.

More practically speaking, I'd say stick to something you have a source
formatter for, so you can reformat, fix up, and enforce. Basically there
is astyle (command line and library), which offers a number of options,
all with their tuning. It can be a little confusing because there's a
difference between what it'll tolerate and what it'll forcefully change,
and I found it not totally elaborate enough in some cases. It's
integrated into codeblocks, a general purpose IDE that supports AVR (all
FOSS). Worth a look, but I found the editor still sucks too much. The
style formatter it good though (within astyle's limits). No reason to
only use one editor for a project. I find all suck, just in different
ways.

The best formatter is in eclipse. The interface looks a bit like astyle
but it must have its own.

If there are other (Linux, not interested in MS whatsit studio)
formatters I'd like to hear.

HTH,

Volker

-- 
Volker Kuhlmann
http://volker.top.geek.nz/	Please do not CC list postings to me.



More information about the Chchrobotics mailing list