D style guide
Joseph Rushton Wakeling
joseph.wakeling at webdrake.net
Mon Jun 17 09:13:07 PDT 2013
Hi all,
A recent Phobos pull request got critiqued over some stylistic aspects -- which
was obviously disappointing as I thought I'd learned those guidelines fairly well.
Just to make sure, are the rules at http://dlang.org/dstyle.html considered to
be the ones to follow -- and up to date? There are no extra guidelines
available elsewhere (e.g. on the wiki)?
I ask because several of the rules that I was requested to follow are not listed
there:
* Space after if -- e.g. if (x < 0) not if(x < 0). This isn't cited, and in
fact the example given (at the end of the page) has no space after if.
Also, what about other statements such as while, for, foreach, etc.?
* Full bracing. It's mentioned that opening braces should be on their own
line, but not that braces should be used even in the case of single-line
statements, i.e. that one should do,
if (x < 0)
{
negative = true;
}
else
{
negative = false;
}
and not
if (x < 0)
negative = true;
else
negative = false;
I have a memory of being instructed specifically _not_ to use bracing for
single-line statements at the time I submitted my first ever pull request,
but I may be mistaken.
* Use of brackets around sides of a conditional, e.g. to write
if (x + y < w * z)
and not
if ((x + y) < (w * z))
So, have I missed something in a style guide in a different location, or have
these rules simply been adopted by custom without making it into official
guidelines? If the latter, should I make a pull request for the website? :-)
I'd also appreciate guidelines about template if statements and indentation: is
it preferred to write e.g.
auto func(R)(R input)
if(isInputRange!R)
{
...
}
or,
auto func(R)(R input)
if(isInputRange!R)
{
...
}
I see both in Phobos. I must say I prefer the former, as it somehow highlights
the presence of the 'if' statement and its connection to function declaration
and body, but is there an official view?
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.)
Thanks & best wishes,
-- Joe
More information about the Digitalmars-d-learn
mailing list