www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - DStyle: Braces on same line

reply "Danyal Zia" <catofdanyal yahoo.com> writes:
Hi,

I noticed that in Andrei's talks and his book, he used braces on 
the same line of delcaration, however Phobos and other D 
libraries I know use braces on their own line. Now I'm in a 
position where I need to take decision on coding style of my 
library and I get accustomed to use braces on same line but I'm 
worried if that would make my library less readable to other D 
users.

Should I worry about it? Or is that's just a debatable style that 
won't really matter if it's persistent throughout library?

Thanks
Jul 12 2014
next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 07/12/2014 09:01 PM, Danyal Zia wrote:
 Hi,

 I noticed that in Andrei's talks and his book, he used braces on the
 same line of delcaration,
That's because they do not waste that much space that way.
 however Phobos and other D libraries I know
 use braces on their own line. Now I'm in a position where I need to take
 decision on coding style of my library and I get accustomed to use
 braces on same line but I'm worried if that would make my library less
 readable to other D users.
 ...
I think Phobos is often painful to read exactly because trivial functionality tends to span multiple pages.
 Should I worry about it?
IMO No.
 Or is that's just a debatable style that won't
 really matter if it's persistent throughout library?
Yes.
 Thanks
Jul 12 2014
prev sibling next sibling parent reply "anonymous" <anonymous example.com> writes:
On Saturday, 12 July 2014 at 19:01:56 UTC, Danyal Zia wrote:
 Hi,

 I noticed that in Andrei's talks and his book, he used braces 
 on the same line of delcaration, however Phobos and other D 
 libraries I know use braces on their own line. Now I'm in a 
 position where I need to take decision on coding style of my 
 library and I get accustomed to use braces on same line but I'm 
 worried if that would make my library less readable to other D 
 users.

 Should I worry about it?
no
 Or is that's just a debatable style that won't really matter if 
 it's persistent throughout library?
yes (I read that "if" as "as long as") There is another stylistic choice which I do find confusing: capitalized identifiers for non-types, e.g. variables and functions. Please don't do that.
Jul 12 2014
parent "Danyal Zia" <catofdanyal yahoo.com> writes:
On Saturday, 12 July 2014 at 19:35:11 UTC, anonymous wrote:
 There is another stylistic choice which I do find confusing:
 capitalized identifiers for non-types, e.g. variables and
 functions. Please don't do that.
Agreed about variables and functions. However I personally prefer to use PascalCased identifiers in enum unlike what is preached in Dstyle page.
Jul 12 2014
prev sibling next sibling parent "Dicebot" <public dicebot.lv> writes:
On Saturday, 12 July 2014 at 19:01:56 UTC, Danyal Zia wrote:
 Should I worry about it? Or is that's just a debatable style 
 that won't really matter if it's persistent throughout library?
Depends entirely on whenever you want to match style of standard library - no one will blame you for having own preferences. It is only required for Phobos pull requests.
Jul 12 2014
prev sibling next sibling parent "dysmondad" <raylwhitfield gmail.com> writes:
On Saturday, 12 July 2014 at 19:01:56 UTC, Danyal Zia wrote:
 Hi,

 I noticed that in Andrei's talks and his book, he used braces 
 on the same line of delcaration, however Phobos and other D 
 libraries I know use braces on their own line. Now I'm in a 
 position where I need to take decision on coding style of my 
 library and I get accustomed to use braces on same line but I'm 
 worried if that would make my library less readable to other D 
 users.

 Should I worry about it? Or is that's just a debatable style 
 that won't really matter if it's persistent throughout library?

 Thanks
Pick one and stick with it. There are countless holy wars out there about this sort of thing and it is really a personal choice. Generally it's a good idea to follow the style of extant code if you are working on large project and I do feel that coding teams should pick a style all the code from that group should use the same style.
Jul 12 2014
prev sibling next sibling parent reply Joseph Rushton Wakeling via Digitalmars-d-learn writes:
On 12/07/14 21:01, Danyal Zia via Digitalmars-d-learn wrote:
 I noticed that in Andrei's talks and his book, he used braces on the same line
 of delcaration, however Phobos and other D libraries I know use braces on their
 own line. Now I'm in a position where I need to take decision on coding style
of
 my library and I get accustomed to use braces on same line but I'm worried if
 that would make my library less readable to other D users.

 Should I worry about it? Or is that's just a debatable style that won't really
 matter if it's persistent throughout library?
As long as your coding style is self-consistent, then it really doesn't matter a lot. In particular this choice of bracing style is a very minor issue that people are well used to having to deal with. However, I do think there's value in deliberately matching the code style of the standard library, as it extends the volume of public D code with a common style. So unless you have a strong personal preference, I'd go with that.
Jul 13 2014
parent reply "Danyal Zia" <catofdanyal yahoo.com> writes:
On Sunday, 13 July 2014 at 10:18:23 UTC, Joseph Rushton Wakeling 
via Digitalmars-d-learn wrote:
 However, I do think there's value in deliberately matching the 
 code style of the standard library, as it extends the volume of 
 public D code with a common style.
  So unless you have a strong personal preference, I'd go with 
 that.
I'm going with Andrei's style of preference on his talks ;)
Jul 13 2014
parent reply Joseph Rushton Wakeling via Digitalmars-d-learn writes:
On 13/07/14 14:23, Danyal Zia via Digitalmars-d-learn wrote:
 I'm going with Andrei's style of preference on his talks ;)
Andrei can no doubt speak for himself about his preferences, but I'd be wary of assuming that the style he uses in his talks necessarily reflects his actual stylistic preference. As has been pointed out by others, same-line opening brace is a common style for slides and books because it saves vertical space; people will use it there even if in their actual code they prefer something different.
Jul 13 2014
parent "Dicebot" <public dicebot.lv> writes:
On Sunday, 13 July 2014 at 16:47:00 UTC, Joseph Rushton Wakeling 
via Digitalmars-d-learn wrote:
 On 13/07/14 14:23, Danyal Zia via Digitalmars-d-learn wrote:
 I'm going with Andrei's style of preference on his talks ;)
Andrei can no doubt speak for himself about his preferences, but I'd be wary of assuming that the style he uses in his talks necessarily reflects his actual stylistic preference. As has been pointed out by others, same-line opening brace is a common style for slides and books because it saves vertical space; people will use it there even if in their actual code they prefer something different.
Andrei has stated several times he prefers Phobos-style brackets but uses egyptian ones in slides / books because of spacing constraints.
Jul 13 2014
prev sibling next sibling parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Sun, Jul 13, 2014 at 11:32:23AM +0200, Joseph Rushton Wakeling via
Digitalmars-d-learn wrote:
 On 12/07/14 21:01, Danyal Zia via Digitalmars-d-learn wrote:
I noticed that in Andrei's talks and his book, he used braces on the
same line of delcaration, however Phobos and other D libraries I know
use braces on their own line. Now I'm in a position where I need to
take decision on coding style of my library and I get accustomed to
use braces on same line but I'm worried if that would make my library
less readable to other D users.

Should I worry about it? Or is that's just a debatable style that
won't really matter if it's persistent throughout library?
As long as your coding style is self-consistent, then it really doesn't matter a lot. In particular this choice of bracing style is a very minor issue that people are well used to having to deal with. However, I do think there's value in deliberately matching the code style of the standard library, as it extends the volume of public D code with a common style. So unless you have a strong personal preference, I'd go with that.
I had my own style before, but after I started contributing to Phobos, I found it a pain to keep switching back and forth between styles (and to convert styles before submitting PR's), so eventually I decided to just adopt Phobos style for all my D code, including my personal projects. That way I never have to worry again about which project is in what style, but everything is consistently the same style. It also helps that my previous supervisor at my work also used a similar style, which was different from my own, so I already had to adapt my style to his in the past. That was what convinced me that other inferior styles than my own had any merit at all. ;-) At the end of the day, though, as long as your style is consistent within a project, it's Good Enough(tm). There are far weightier issues of semantics to worry about than fretting over syntax. T -- Verbing weirds language. -- Calvin (& Hobbes)
Jul 13 2014
prev sibling next sibling parent reply "Gary Willoughby" <dev nomad.so> writes:
On Saturday, 12 July 2014 at 19:01:56 UTC, Danyal Zia wrote:
 Hi,

 I noticed that in Andrei's talks and his book, he used braces 
 on the same line of delcaration, however Phobos and other D 
 libraries I know use braces on their own line. Now I'm in a 
 position where I need to take decision on coding style of my 
 library and I get accustomed to use braces on same line but I'm 
 worried if that would make my library less readable to other D 
 users.

 Should I worry about it? Or is that's just a debatable style 
 that won't really matter if it's persistent throughout library?

 Thanks
Here is the 'official' style that is followed by most people including me. http://dlang.org/dstyle.html
Jul 13 2014
parent "Danyal Zia" <catofdanyal yahoo.com> writes:
On Sunday, 13 July 2014 at 16:10:31 UTC, Gary Willoughby wrote:
 Here is the 'official' style that is followed by most people 
 including me.

 http://dlang.org/dstyle.html
Unrelated to my original question. I already read that before asking.
Jul 14 2014
prev sibling next sibling parent reply Joseph Rushton Wakeling via Digitalmars-d-learn writes:
On 13/07/14 16:52, H. S. Teoh via Digitalmars-d-learn wrote:
 I had my own style before, but after I started contributing to Phobos, I
 found it a pain to keep switching back and forth between styles (and to
 convert styles before submitting PR's), so eventually I decided to just
 adopt Phobos style for all my D code, including my personal projects.
 That way I never have to worry again about which project is in what
 style, but everything is consistently the same style.
Same here. :-)
 It also helps that my previous supervisor at my work also used a similar
 style, which was different from my own, so I already had to adapt my
 style to his in the past. That was what convinced me that other inferior
 styles than my own had any merit at all. ;-)
Two consequences of adapting myself to Phobos style were that I realized (i)how little most of these things really matter, and (ii) pretty much any stylistic choice carries both benefits and drawbacks. E.g. in this case, "Egyptian"-style braces definitely make your code more compact, but separate-line opening braces definitely make it easier to see where scopes begin and end.
Jul 13 2014
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 07/13/2014 06:45 PM, Joseph Rushton Wakeling via Digitalmars-d-learn 
wrote:
 Two consequences of adapting myself to Phobos style were that I realized
 (i)how little most of these things really matter,  and (ii) pretty much
 any stylistic choice carries both benefits and drawbacks.
 ...
Wrong. There are things which are simply bad ideas.
 E.g. in this case, "Egyptian"-style braces definitely make your code
 more compact,
I.e. you see where everything is.
 but separate-line opening braces definitely make it easier
 to see where scopes begin and end.
This is the only argument I have heard in favour of doing this, but it is not actually valid. This critique might apply to Lisp style.
Jul 13 2014
next sibling parent reply "Brian Rogoff" <brogoff gmail.com> writes:
On Sunday, 13 July 2014 at 17:24:40 UTC, Timon Gehr wrote:
 On 07/13/2014 06:45 PM, Joseph Rushton Wakeling via 
 Digitalmars-d-learn wrote:
 Wrong. There are things which are simply bad ideas.

 E.g. in this case, "Egyptian"-style braces definitely make 
 your code
 more compact,
I.e. you see where everything is.
Yes, the same argument for books and slides is also applicable to all other media. This style has also caught on amongst the other curly braced languages that I use, so that most of the code I read (and write) has adopted it (C/C++/Java/Javascript code, that is). The Phobos style is incredibly wasteful IMO, but that's what D has adopted, so if you intend to contribute to Phobos, you had better get used to it. The Rust community appears to have made the right choice with Egyptian for everything.
 but separate-line opening braces definitely make it easier
 to see where scopes begin and end.
All of this is subjective, of course, but I definitely don't find that the Phobos style provides this advantage.
 This is the only argument I have heard in favour of doing this, 
 but it is not actually valid. This critique might apply to Lisp 
 style.
Not sure I follow you here. Most of the Lisp I've read is indented like Python, the idea being that you learn not to not see all of the parens and rely on tools like paredit to do the trivial balancing. I'd hate to read Lisp with separate lines for parens that open scopes. I'm sure that's not what you mean!
Jul 13 2014
next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 07/13/2014 07:51 PM, Brian Rogoff wrote:
 On Sunday, 13 July 2014 at 17:24:40 UTC, Timon Gehr wrote:
 On 07/13/2014 06:45 PM, Joseph Rushton Wakeling via
 Digitalmars-d-learn wrote:
 Wrong. There are things which are simply bad ideas.

 E.g. in this case, "Egyptian"-style braces definitely make your code
 more compact,
I.e. you see where everything is.
Yes, the same argument for books and slides is also applicable to all other media.
Exactly.
 This style has also caught on amongst the other curly
 braced languages that I use, so that most of the code I read (and write)
 has adopted it (C/C++/Java/Javascript code, that is). The Phobos style
 is incredibly wasteful IMO, but that's what D has adopted, so if you
 intend to contribute to Phobos, you had better get used to it.

 The Rust community appears to have made the right choice with Egyptian
 for everything.
 ...
Yup, but they also do horrible things like using '+' to denote intersection (multiple trait bounds).
 but separate-line opening braces definitely make it easier
 to see where scopes begin and end.
All of this is subjective, of course, but I definitely don't find that the Phobos style provides this advantage.
 This is the only argument I have heard in favour of doing this, but it
 is not actually valid. This critique might apply to Lisp style.
Not sure I follow you here. Most of the Lisp I've read is indented like Python, the idea being that you learn not to not see all of the parens and rely on tools like paredit to do the trivial balancing. I'd hate to read Lisp with separate lines for parens that open scopes. I'm sure that's not what you mean!
I was suggesting that if someone does this: http://en.wikipedia.org/wiki/Indent_style#Lisp_style Then I would have an easier time seeing where a person is coming from who claims that it makes it in some way harder to see at a glance where scopes begin and end.
Jul 13 2014
prev sibling parent Joseph Rushton Wakeling via Digitalmars-d-learn writes:
On 13/07/14 19:51, Brian Rogoff via Digitalmars-d-learn wrote:
 Yes, the same argument for books and slides is also applicable to all other
 media.
Not really. In a book or a slide you have an unavoidable constraint on how much vertical space you can take up. On a screen, you are unavoidably going to have to scroll up or down at some point; it's just a question of how often. Scrolling media (not just code) allow you to make a tradeoff between less vertically compact styles that better highlight different semantic blocks, versus more compact styles that packs more data into one screen's worth of lines, while usually making it less easy to highlight the semantics of what's being displayed. You may lean towards favouring compact code over other factors, but at the end of the day this is a preference based on your personal priorities, not a hard and fast rule.
Jul 13 2014
prev sibling next sibling parent Joseph Rushton Wakeling via Digitalmars-d-learn writes:
On 13/07/14 19:24, Timon Gehr via Digitalmars-d-learn wrote:
 Wrong. There are things which are simply bad ideas.
I think that we can take it as read that I meant, "Any reasonable stylistic choice." Of course, YMMV about what counts as "reasonable", but most of the things that people fuss over are fairly minimal differences in practice.
 I.e. you see where everything is.
Compactness can also be a disadvantage. Some people have a preference for a hyper-compact style where there are minimal blank lines in the code; I accept their goal as valid, and I think there are cases where it can surely help, but it's not one that I personally find very helpful. In fact, one reason that I've come to appreciate standard D style is the way in which separate opening braces actually help to space out the code into more obvious "paragraphs".
 This is the only argument I have heard in favour of doing this, but it is not
 actually valid. This critique might apply to Lisp style.
Well, I personally find that separate-line opening braces do make it easier to line up the opening and ending of scopes. If it doesn't do anything for you, that's a shame; but it doesn't make the argument invalid.
Jul 13 2014
prev sibling parent "Dicebot" <public dicebot.lv> writes:
On Sunday, 13 July 2014 at 17:24:40 UTC, Timon Gehr wrote:
 but separate-line opening braces definitely make it easier
 to see where scopes begin and end.
This is the only argument I have heard in favour of doing this, but it is not actually valid. This critique might apply to Lisp style.
It is perfectly valid and much more important :P
Jul 14 2014
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Danyal Zia:

 I noticed that in Andrei's talks and his book, he used braces 
 on the same line of delcaration, however Phobos and other D 
 libraries I know use braces on their own line.
Rosettacode D examples always use the Egyptian style. For my code I use the same style, but Phobos contributions (and future ddmd) should put opening braces on their own line. Bye, bearophile
Jul 13 2014