D style guide

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Mon Jun 17 12:09:11 PDT 2013


On 06/17/2013 05:36 PM, Ali Çehreli wrote:
> I want that space. It is very common in most C++ guidelines as well.

Any particular reason?  It's not something that had ever occurred to me as being
important one way or the other.

> Yes, curly brackets are very helpful in readability even when there is no reason.

Is also useful functionally, as it avoids the problem of someone accidentally
forgetting to add braces when they add an extra line inside the scope.

> I am on the other side on that one: Use parentheses to show intent even when
> unnecessary.

Me too, although in my case it was also unfounded paranoia about operator
precedence ...

>> Same for in/out contracts: is it preferred to have,
>>
>>      auto func(...)
>>          in
>>          {
>>              ...
>>          }
>>          out
>>          {
>>              ...
>>          }
>>      body
>>      {
>>          ...
>>      }
>>
>> ... or all on the same indentation level?  (Again, I personally prefer the
>> indentation of the in/out blocks.)
> 
> I don't like that indentation.

I came to it by accident, as it was the default way in which my editor chose to
indent when I started typing 'in', but I do rather like the way in which it
highlights the contracts as distinct from the function declaration and body.
But it's not something I feel strongly about.

> I know that Phobos wants the opening curly bracket on its own and that's what I
> am used to elsewhere, but I have gotten used to seeing it at the end of the
> previous line. Except, I still don't like it for functions (member or regular),
> structs, classes, version, etc. I think I like it only after control-flow
> keywords like if, foreach, while, etc.

I found myself rather liking the spec Linus Torvalds gave for the Linux kernel,
which was: separate-line opening brace for functions, same-line for control
statements (if, while, etc.).  It's what I've tended to use for my own D code,
although I'm considering that it might be best to go with strict Phobos style
just in order to help with uniformity of D style.

> But if we go with others' preference, then I have seen code like this:
> 
> auto func(...)
> in {
>     ...
> 
> } out {
>     ...
> 
> } body {
>     ...
> }

I _really_ don't like that, although I can see why someone would come up with
it.  It just clashes so horribly with both Phobos style and with the Linus-style
"separate line opening brace for functions" which I'm so used to using.  (Though
I've never contributed to the kernel!).


More information about the Digitalmars-d-learn mailing list