Code Style

Graham St Jack grahams at acres.com.au
Sun Jun 17 17:36:46 PDT 2007


Robert Fraser wrote:
> Hi all,
> 
> Right now, I'm working on a code formatter for the Descent IDE (http://www.dsource.org/projects/descent ), and I'm planning to make a (or possibly more than one) "default" profile for code style. All this stuff will be configurable down to the tiniest detail (if you want spaces before the comma but not after in superclass/interface lists and after the comma but not before everywhere else, that'll be evry possible), but having some sensible defaults would be a good thing. 
> 
> So, what style do you code with (I know I'm not going to get a consensus, but I'm looking for what's most popular)? You don't have to answer everything, just give me a general idea; saying "I follow the Java conventions" or "I follow the conventions at this link: [...] except for braces, which I always put on a separate line" or "check out this source file: [...]" is fine. These are "questions to get you/me thinking" ratehr than specific questions, so an implicit "etc., etc." should be added to the end of every paragraph here.
> 
> Where do you indent and where don't you (do you indent the body of every type? Every function? Modifier blocks? Switch statements? Switch cases? Do you un-indent the break on a switch case?)? Do you indent with tabs or spaces (or both?) What size tabs/how many spaces do you use?
> 
> Where do you put your opening braces (same line, the next line, or next -line-indented?) Do you do this differently for different braces (i.e. next line for declaration blocks but same line for statement blocks or something?)
> 
> What sort of spacing do you like to use? Do you put spaces before and/or after commas in lists? Do you treat different comma-separated lists differently? How about around (before and/or after) parentheses? Do you put a space before semicolons? Before the colon in switch cases/labels?
> 
> Do you ever align your members, i.e.:
> int foobar = 1;
> int baz     = 1;
> (It's hard to show with variable-spaced text, but you get the idea). Do you align parameters, and put them on separate lines?
> 
> Where do you put your line breaks and empty lines? Do you like empty lines before/after the module and import declarations? Do you put two blank lines between every function? Do you like a blank line at the beginning of every function? Do you put a simple (one-statement) if on one line or split it up? Do you put new lines after closing braces for "else if", "catch"/"finally" and/or "while" (in a do-while) statements?
> 
> How about line wrapping? Do you always wrap your lines at a specific column (i.e. 80) and do you like to wrap at specific places (i.e. don't strand the type from it's name in an argument list)? Do you put every parameter on a separate line?
> 
> Again, it's all going to be configurable, but having a few sensible defaults will probably be helpful. There won't be any comment formatting in the first version, so don't worry about that part (they'll just be dumped as they are... so will inline assembly).
> 
> Also, a more general question... would you ever consider actually USING a code formatter/have used one in the past?
> 
> Thanks for your help,
> All the best,
> Fraser

Regardless of coding style, I would like to see this available as a
utility in its own package, so anyone can use it.

The style I use is like this, with tabs converted to spaces and trailing
whitespace removed - but I aren't all that fussy. What I really want is
automatic code formatting so that I don't need to worry about formatting
at all.

void fooBar() {
    int   a; // aligned
    short b;

    if (blah) {
        a = b;
    }
    else if {
        a = 2 * b;
    }
    callStuff(long_parameter_name,
              another_long_parameter_name); // aligned
}



More information about the Digitalmars-d mailing list