Live data from Hacker News

Plan 9 Coding Conventions for C

plan9.bell-labs.com

31–40 of 68 posts

Re: Plan 9 Coding Conventions for C

#31

> no white space after the keywords if, for, while, etc. Woh, my workplace enforces the exact opposite! The idea is that if, while, etc are not functions, therefore `if (x)` is unacceptable but `my_func(x)` is ok. I personally prefer `if(x)` though.

There is no right or wrong here. The important thing is consistency across a project.

Re: Plan 9 Coding Conventions for C

#32

Earlier quoted context omitted.

And don't forget: if(a) if(b) x; else y; This doesn't do what the indentation implies. ( http://en.wikipedia.org/wiki/Dangling_else ) Walter Bright (creator of D, etc.) has an article where he discusses his interesting choice to make that grammar form illegal in D. He mentions that after doing this he found a bug in D's runtime library. http://www.drdobbs.com/cpp/dangling-else-yet-again/231602010

This makes a good point for Python syntax, where you can't by construction end up in situations like that.

Note that any static analyzer that is worth something will warn about this in C/C++. For example, clang will warn with the option -Wdangling-else (implied by -Wall)

Re: Plan 9 Coding Conventions for C

#33
post #20

Earlier quoted context omitted.

Because developer one wrote if (statement) one; And developper two added if (statement) one; two; too quickly

The actual problem there is that developer two checked something in without thinking about it, reading it, or examining the diffs of the change. If you had a coding convention of always using braces in if statements, you might be insulated from this particular symptom or instance of the problem, but you still have the underlying problem that people are doing things without thinking.

Why not take advantage of every safety net you can?

People are fallible, mistakes get made.

Re: Plan 9 Coding Conventions for C

#34
post #8

* variable and function names are all lowercase, with no underscores. That sounds odd

In the standard there is a notion of "significant prefix length". ie, your symbol names may actually be truncated. In C89 the minimum guaranteed length for exported symbols was 6 (IIRC) which explains why the libc symbols are so terse. Adding underscores would greatly reduce the number of significant characters (of course, all of this is mostly obsolete these days).

Re: Plan 9 Coding Conventions for C

#35

Earlier quoted context omitted.

And don't forget: if(a) if(b) x; else y; This doesn't do what the indentation implies. ( http://en.wikipedia.org/wiki/Dangling_else ) Walter Bright (creator of D, etc.) has an article where he discusses his interesting choice to make that grammar form illegal in D. He mentions that after doing this he found a bug in D's runtime library. http://www.drdobbs.com/cpp/dangling-else-yet-again/231602010

This makes a good point for Python syntax, where you can't by construction end up in situations like that.

True, but the flip side is that in Python automatic indentation cannot possibly work.

Re: Plan 9 Coding Conventions for C

#36
post #8

* variable and function names are all lowercase, with no underscores. That sounds odd

Not really. Look at everything in the C standard library: fopen(), strcmp(), malloc(), etc.

yes and c stdlib style naming is mainly considered legacy these days and multi word "variable and function [in] all lowercase, with no underscores" is frowned upon.

Re: Plan 9 Coding Conventions for C

#37
post #6

Earlier quoted context omitted.

OS code has to do a lot of validating input and braceful style causes functions to be visually dominated by boring validation code. It's also surprisingly nice to have a visual distinction between boring conditions like if(!found) return; and the more interesting branches that require the braces. Not sure I follow you on the one; two; example. Why would you have two statements per line in the first place? (You do rea…

Because developer one wrote if (statement) one; And developper two added if (statement) one; two; too quickly

stuff like this can happen if you're not careful w/ preprocessor macros too.

#define one a;b;

if (statement) one;

Re: Plan 9 Coding Conventions for C

#38
post #31

> no white space after the keywords if, for, while, etc. Woh, my workplace enforces the exact opposite! The idea is that if, while, etc are not functions, therefore `if (x)` is unacceptable but `my_func(x)` is ok. I personally prefer `if(x)` though.

There is no right or wrong here. The important thing is consistency across a project.

Spaces before parens doesn't matter too much, but with respect to spaces before commas all relativism goes straight out the window: putting a space before a comma is objectively better practice.
Post reply on HN