[chbot] C Coding Style

mikael stewart mikael.stewart at gmail.com
Fri Sep 23 00:14:26 BST 2016


Unless a pre-existing style exists, I default to using the "BSD/Allman"
coding style as a matter of preference.

The Eclipse IDE has this built in as an option under the code formatter
settings, although the "K&R" style format is set by default.

My preference for the BSD/Allman style comes from:

1. Use of TAB to indent. All decent, modern IDEs all allow scaling of the
tab character. I like large indents (I have a 16:9 monitor and I'm not
afraid to use it!), so set TAB to appear as the equivalent of 4 monospaced
characters. If I pass my code over to someone else, they can easily
re-scale to suit their eyes without reformatting the content (think tab
stops in a word processor).

And, using a single character for a single indent is intuitive. Having a
single character means that backspace will always go back one indent level
no matter what smarts your IDE is trying to force upon you. It seems that
every IDE handles auto-indenting/unindenting differently, and so by
natively sticking to a 1:1 ratio, you wont get frustrated when your IDE
falls over in confusion over whether you want to go back one space or one
indent.

2. Opening brace always on new line and indented to the same level as the
above control statement. I think having symmetrical opening/closing braces
helps to visualize context a bit better when you're constantly scrolling
code from either direction and avoids redundancy and confusion when
conditional compilation creates alternative control statements that would
otherwise each have their own opening brace. It also helps to match up
opening and closing braces because you know they share the same horizontal
offset, and because inevitably at some point I end up viewing code in a
simple text editor, having done this can help a lot..

Of course it remains a matter or preference. Consistency of style is more
important methinks..



On Wed, Sep 21, 2016 at 10:47 PM, Richard Jones <
richard.jones.1952 at gmail.com> wrote:

> The topic of 'C' coding style came up today. At the risk of sounding like
> a troll I thought I would ask if people have a favourite 'C' style guide?
>
> Some may ask why have a style guide? My answer would be to make the code
> easier and quicker to understand, and less prone to the introduction of
> errors during maintenance by others or even the original author.
>
> I had a quick look around a few style guides for 'C' and liked Linus on
> the kernel coding style. The document is reasonably brief for a complex
> topic, educational and believe it or not amusing in parts. However it is
> definitely not for a beginner.
>
> https://www.kernel.org/doc/Documentation/CodingStyle
>
> This one is really brief:
>
> https://www.arduino.cc/en/Reference/StyleGuide
>
> Offers anyone?
>
> _______________________________________________
> Chchrobotics mailing list Chchrobotics at lists.linuxnut.co.nz
> http://lists.ourshack.com/mailman/listinfo/chchrobotics
> Mail Archives: http://lists.ourshack.com/pipermail/chchrobotics/
> Meetings usually 3rd Monday each month. See http://kiwibots.org for
> venue, directions and dates.
> When replying, please edit your Subject line to reflect new subjects.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ourshack.com/pipermail/chchrobotics/attachments/20160923/93e15d41/attachment.html>


More information about the Chchrobotics mailing list