www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: Basic coding style

reply Kagamin <spam here.lot> writes:
spir Wrote:

 void func(string[] strings) {
     string[] result;
     foreach (auto s ; strings) {
         if (s != "") {
             writefln("found: %s", s);
             result ~= s; }}}

bearophile, look!
Nov 23 2010
parent reply Max Samukha <spambox d-coding.com> writes:
On 11/23/2010 01:17 PM, Kagamin wrote:
 void func(string[] strings) {
       string[] result;
       foreach (auto s ; strings) {
           if (s != "") {
               writefln("found: %s", s);
               result ~= s; }}}


This takes too much vertical space and should be rewritten to: void func(string[] strings) {string[] result; foreach(auto s; strings){ if (s != "") { writefln("found: %s", s); result ~= s; }}} Nowadays most programmers have 16:9 displays, so the good coding style is to write as much as possible in one line. And the more misaligned the curies are, the better.
Nov 23 2010
next sibling parent so <so so.do> writes:
 This takes too much vertical space and should be rewritten to:

 void func(string[] strings) {string[] result; foreach(auto s; strings){  
 if (s != "") { writefln("found: %s", s); result ~= s; }}}

 Nowadays most programmers have 16:9 displays, so the good coding style  
 is to write as much as possible in one line. And the more misaligned the  
 curies are, the better.

Compile! Error (line 1) .... Error (line 1) .... Error (line 1) .... Error (line 1) .... .... -- Using Opera's revolutionary email client: http://www.opera.com/mail/
Nov 23 2010
prev sibling next sibling parent reply Kagamin <spam here.lot> writes:
Max Samukha Wrote:

 On 11/23/2010 01:17 PM, Kagamin wrote:
 void func(string[] strings) {
       string[] result;
       foreach (auto s ; strings) {
           if (s != "") {
               writefln("found: %s", s);
               result ~= s; }}}


This takes too much vertical space and should be rewritten to: void func(string[] strings) {string[] result; foreach(auto s; strings){ if (s != "") { writefln("found: %s", s); result ~= s; }}}

I wanted to say D allows even python-style formatting. Brace matching editor will help in syntactical differences.
Nov 23 2010
next sibling parent Max Samukha <spambox d-coding.com> writes:
On 11/23/2010 11:18 PM, spir wrote:
 Seriously, I fail to see how braces one their own line help visually catching
code structure

I agree that brace alignment has little to do with catching code structure. It is more about symmetry. And visual symmetry of braces in code is a matter of preference. I prefer symmetry. I have read and written quite a lot of code in various coding styles and aligned braces still look and feel cleaner to me, no matter how hard I try to be unbiased. Regarding that vertical space argument, in my experience, big contiguous chunks of code are rarely read or written in one go (except when you are unlucky to read one of those just-get-the-job-done(TM) functions spreading a few dozens of screens). So saving vertical space is not a big concern in practice. And if you are using a good IDE, it is no concern at all.
 (and it's not only me

http://en.wikipedia.org/wiki/Argumentum_ad_populum
Nov 23 2010
prev sibling parent sybrandy <sybrandy gmail.com> writes:
 Seriously, I fail to see how braces one their own line help visually catching
code structure (and it's not only me ;-)

For me, it really helps me make sure that I'm not missing a brace as I can see them line up visually. It's easier to see the missing brace in code that looks like this: if (condition) { foo(); } Than this: if (condition) { foo(); } Yes, this is a simplified case, but if you start multiple levels of nesting, it can be confusing. Of course, a Erlang/Haskell/Python style indentation syntax does work, but then, IMHO, it works best if your functions are very small. Of course, I've never done any Python development, however I have done Erlang and Haskell in the past and the fact that they really make it easier to write small functions helps a lot. Smaller functions are better in all languages, but at least with code I've seen for Java and some D, methods can end up being fairly long. Casey
Nov 24 2010
prev sibling next sibling parent spir <denis.spir gmail.com> writes:
On Tue, 23 Nov 2010 10:00:07 -0500
Kagamin <spam here.lot> wrote:

 Max Samukha Wrote:
=20
 On 11/23/2010 01:17 PM, Kagamin wrote:
 void func(string[] strings) {
       string[] result;
       foreach (auto s ; strings) {
           if (s !=3D "") {
               writefln("found: %s", s);
               result ~=3D s; }}}


This takes too much vertical space and should be rewritten to: =20 void func(string[] strings) {string[] result; foreach(auto s; strings){=


 if (s !=3D "") { writefln("found: %s", s); result ~=3D s; }}}


Seriously, I fail to see how braces one their own line help visually catchi= ng code structure (and it's not only me ;-)
 I wanted to say D allows even python-style formatting. Brace matching edi=

That's similar what some Lisp/Scheme do ;-) Just add end/closing parens unt= il your editor is happy (highlighting the first opening one). Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Nov 23 2010
prev sibling parent spir <denis.spir gmail.com> writes:
On Wed, 24 Nov 2010 02:03:17 +0200
Max Samukha <spambox d-coding.com> wrote:

  > (and it's not only me =20
=20
 http://en.wikipedia.org/wiki/Argumentum_ad_populum

Good catch ;-) (But I was not exactly pretending something to be true because more people = think so -- instead that others also do not find braces-on-their-own-line h= elpful). Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Nov 24 2010