News for hackers. Stuff that matters.
Re:Plan 9 Coding Conventions for C by shin_lao on Wednesday May 22, 2013 @08:08AM (#5750180)
I appreciate that these are old rules, nevertheless, I have the following comments:

don't use // comments; some old Plan 9 code does, but we're converting it as we touch it. We do sometimes use // to comment–out a few lines of code.

Doesn't make sense with any decent editor, you ought to comment via macros.

no braces around single–line blocks (e.g., if, for, and while bodies)

I kind of like enforcing the opposite (always use braces).

Otherwise you end up with things like:

   if (statement)
      one; two;
Re:Plan 9 Coding Conventions for C by jacobparker on Wednesday May 22, 2013 @08:32AM (#5750285)
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

Re:Plan 9 Coding Conventions for C by angersock on Wednesday May 22, 2013 @08:57AM (#5750414)
Just see that made me foam at the mouth a little...grr.

We generally don't even like have non-curlied one-liners in our conditionals.

Re:Plan 9 Coding Conventions for C by mercurial on Wednesday May 22, 2013 @09:11AM (#5750509)
This makes a good point for Python syntax, where you can't by construction end up in situations like that.
Re:Plan 9 Coding Conventions for C by jacobparker on Wednesday May 22, 2013 @09:19AM (#5750565)
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 by maw on Wednesday May 22, 2013 @09:26AM (#5750620)
True, but the flip side is that in Python automatic indentation cannot possibly work.
Re:Plan 9 Coding Conventions for C by safod on Wednesday May 22, 2013 @09:52AM (#5750800)
Why not? vim indents one more when encountering a colon at the end of a line. It also de-indents when encountering a return or break statement. Of course in all other cases you have to de-indent manually, however, hitting backspace once is not more work than typing a closing curly brace.
Re:Plan 9 Coding Conventions for C by maw on Wednesday May 22, 2013 @10:35AM (#5751120)
What I mean is that you can't tell your editor to indent an arbitrary block of code.
Re:Plan 9 Coding Conventions for C by sp332 on Wednesday May 22, 2013 @11:50AM (#5751631)
Right, because a block of un-indented python is syntactically incorrect. It would be just like stripping all the brackets out of some C code and asking the editor to add brackets to the resulting mess properly.
Re:Plan 9 Coding Conventions for C by papsosouid on Wednesday May 22, 2013 @10:56AM (#5751287)
As maw points out, that is not a pure win, it is a tradeoff. And it is a tradeoff that I've found to be a really bad one. Being able to run indent over your code and have it automatically formatted is really helpful in large, long term projects where people come and go and weren't effectively forced into using one style.
Re:Plan 9 Coding Conventions for C by Luyt on Wednesday May 22, 2013 @02:00PM (#5752618)
You can do that with Python code, too.

https://github.com/hhatto/autopep8

Re:Plan 9 Coding Conventions for C by papsosouid on Wednesday May 22, 2013 @03:04PM (#5753037)
Sort of, but not quite. It is quite easy to get situations where code is wrong because of indentation mistakes, copy+paste, etc. All autopep can do is make your wrong code look consistent. Languages with some sort of block delimiter can't get wrong in this manner, so it is safe to munge together random sources with various indenting messes and then just run a tool over them.
Re:Plan 9 Coding Conventions for C by nteon on Wednesday May 22, 2013 @08:37AM (#5750312)
I can't stand macro comments - it makes it impossible to figure out whats going on without being sure to open the file in a specific editor with a specific project loaded. Say you have a separate lib project used by an application - if you open up an individual file from that lib while working on the application in Visual Studio it won't have the necessary context to 'comment out' macro comments. Its also hostile to people who use vi and emacs.
Re:Plan 9 Coding Conventions for C by kleiba on Wednesday May 22, 2013 @09:51AM (#5750783)
Sorry if I'm unenlightened... but what are macro comments?
Re:Plan 9 Coding Conventions for C by enqk on Wednesday May 22, 2013 @09:58AM (#5750841)

  printf("Legit code\n");

  #if 0
    /* bunch of code to comment out */
    printf("Commented code\n");
  #endif
Re:Plan 9 Coding Conventions for C by rcxdude on Wednesday May 22, 2013 @02:33PM (#5752848)
Those do with fine in vim though (even using #if 1 and #else will highlight the #else as a comment)
Re:Plan 9 Coding Conventions for C by gosu on Wednesday May 22, 2013 @08:20AM (#5750231)
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 realize that the "two;" isn't in the if block, right?)

Re:Plan 9 Coding Conventions for C by shin_lao on Wednesday May 22, 2013 @08:40AM (#5750324)
Because developer one wrote

if (statement) one;

And developper two added

if (statement) one; two;

too quickly

Re:Plan 9 Coding Conventions for C by mercurial on Wednesday May 22, 2013 @09:14AM (#5750531)
Sure, but how often does this happen in practice? I have worked with 'no convention'/'multiple conventions'/'mostly no braces for single statements' and have never seen this particular mistake. I can see how it would be difficult to track down, on the other hand, braces for single comments add to line noise.
Re:Plan 9 Coding Conventions for C by k3n on Wednesday May 22, 2013 @10:58AM (#5751300)
I've encountered it just enough times to avoid it 100%. I think in my scenario, the problems have been compounded by an overall lax (lazy) coding style on the part of my predecessors, in that many of them were junior devs who virtually made-up their own coding styles. These devs treated whitespace like it cost money, and avoided any unrequired use of spaces, tabs, and newlines.

These types of conventions -- no curlies around simple blocks -- aren't too bad if a) your devs are adequately seasoned, and b) your devs believe in whitespace.

For instance, this isn't really so bad:

    if (foo)
      foo();
    else
      bar();
Hell, that's clean!

But it's stuff like this that causes issues:

    if(foo)foo();
    else bar();
Sometimes I'm lucky and find this:

    if (foo)
      foo();
    else {
      bar();
      baz();
    }
The best is abominations like this though; first, the sane formatting:

    if (foo)
      while (i++ < x)
        foo(i);
    else
      bar();
Or, as I have found it:

    if(foo)while(i++<x)foo(i);
    else bar();
Seriously, that's enough for me to revoke commit privs.
Re:Plan 9 Coding Conventions for C by to3m on Wednesday May 22, 2013 @11:56AM (#5751693)
I've seen it once, in 13 years of working professionally in C. I think two people changed the same one-line if statement, but for two different reasons (probably adding two different new features to the same bit of code). And after an automatic merge it became something like this:

    if(flag)
        set_other_flag=1;
        call_function();
(both lines necessary)

Anyway, it didn't take long to narrow it down to that bit of code but it took a few more minutes to figure out why the function was always getting called. I couldn't work out what was going on until I looked at the disassembly and was forced to reassess my opinion of what code was actually being compiled. Too much python in my diet, perhaps.

I remember being surprised at the time that it had taken so long for this to happen, and I made a mental note to keep an eye out for more occurrences. That was summer 2006, and I haven't seen it happen since.

Re:Plan 9 Coding Conventions for C by barrkel on Wednesday May 22, 2013 @09:48AM (#5750763)
I've worked on multi-million line projects and have also never seen this mistake made. So in my experience, it is so vanishingly rare that the costs in visual gunk and decreased code density are not worth it.

A lint tool checking for suspicious indentation would be a better use of labour.

Re:Plan 9 Coding Conventions for C by gosu on Wednesday May 22, 2013 @10:49AM (#5751222)
I think that such a developer would be equally as likely to forget to free() memory or overflow a buffer. I assume that the reasoning is that people who don't know C well enough or are too forgetful shouldn't be working on the Plan9 codebase anyway, so why optimize for such a bad case when there are definite costs to doing so (as I mentioned before)?
Re:Plan 9 Coding Conventions for C by mosburger on Wednesday May 22, 2013 @09:34AM (#5750678)
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 by jomar on Wednesday May 22, 2013 @08:48AM (#5750370)
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.

Re:Plan 9 Coding Conventions for C by qznc on Wednesday May 22, 2013 @09:13AM (#5750521)
"you still have the underlying problem that people are doing things without thinking"

You can solve this problem?

Re:Plan 9 Coding Conventions for C by zimpenfish on Wednesday May 22, 2013 @09:23AM (#5750601)
Why not take advantage of every safety net you can?

People are fallible, mistakes get made.

Re:Plan 9 Coding Conventions for C by loup-vaillant on Wednesday May 22, 2013 @10:48AM (#5751214)
Because if you do, the safety net will collapse your circus tent by their sheer weight.

The actual argument has been made in sibling comments: braces are verbose, and the mistake we speak of is exceedingly rare in practice. For simple one-liners, the verbosity costs more than the lack of safety net.

Re:Plan 9 Coding Conventions for C by snov on Wednesday May 22, 2013 @08:27AM (#5750260)
my favorite is

  if (statement)
  //  one;
    two;
Re:Plan 9 Coding Conventions for C by mheathr on Wednesday May 22, 2013 @03:04PM (#5753038)
I always use braces in order to protect my future self from my current self. I don't want to end up introducing a logic error in the program because I overlooked proper scope while less coherent later on.
Re:Plan 9 Coding Conventions for C by ibotty on Wednesday May 22, 2013 @08:21AM (#5750233)
but two statements in one line is not exactly encouraged by the rules either.
Re:Plan 9 Coding Conventions for C by papsosouid on Wednesday May 22, 2013 @10:59AM (#5751307)
>I appreciate that these are old rules

They are current rules also. The rules are largely the same as BSD rules, and go rules. Just because they are old, doesn't mean the people who wrote them moved on to doing what the trendy javascript kids do.