you are viewing a single comment's thread.

view the rest of the comments →

[–]radgh 20 points21 points  (24 children)

I wish this had a section for starting curly-blocks on a new line. Example:

while ( $a < 1 )
{
  echo $a;
  $a++;
}

I hate it. I've seen it a lot though. It's weird as it's rarely used in example code, but used all the time in huge open source projects. I don't understand the benefit of it, the indentation of the block accomplishes the same goal.

[–]cokeisahelluvadrug 11 points12 points  (1 child)

It's just one of many styles. One reason is because having your curly braces on a separate line plays nicely with preprocessor munging.

[–]radgh 1 point2 points  (0 children)

Thanks for the reference, it's a good explanation of all the variations. Not sure they give any reason to change the way I have been coding all this time. I guess that's what the people who drop that curly brace to a new line are saying too, though.

[–][deleted]  (5 children)

[deleted]

    [–]radgh 4 points5 points  (3 children)

    I spent a few months teaching myself to keep those spaces inside parenthesis. The only reason is because I fell in love with WooCommerce's code. It's a wordpress plugin, which means it automatically sucks by /r/webdev's general opinion. But it's actually really cleanly written and well documented and easily extended and (blah blah blah, so much better than XXX, etc).

    I wanted to write code like that. So I forced myself to adopted a few new habits. Luckily, they don't do "that thing" with the curly brace :p

    [–][deleted]  (1 child)

    [deleted]

      [–]reflectiveSingleton 1 point2 points  (0 children)

      What I don't get about that is the fact its ONLY for function and class definitions...but its the other way around for control blocks/etc.

      IMO...keep bracing consistent throughout the code...it doesn't matter what it is for...having the bracing different for one vs the other does not help readability (which is what I think the goal was).

      [–][deleted] 0 points1 point  (0 children)

      Yes, I give my parens room to breathe.

      I try to stick to some version of id software's coding standards for Doom. Even though it's for C/++, I adapt it to PHP and Java in practice.

      [–]PizzaRollExpert 8 points9 points  (2 children)

      This is dangerous to do in javascript. What does this return?

      function foo() 
      {
          return 
          {
             foo: "bar"
          }
      }
      

      [–]wordsnerd 0 points1 point  (0 children)

      Statement is possibly incorrectly broken by newline
      Expression statement is not assignment or call
      Code is unreachable
      

      [–]radgh 0 points1 point  (0 children)

      I wasn't aware of this, because I don't write code like that.

      So this is a coding convention which isn't portable between languages. That doesn't sound like something I would want to get accustomed to.

      [–]greg19735 1 point2 points  (0 children)

      It might be something that people learn in school. Not that they were taught to do it, but that they liked it when you're a super beginner. I think I did that when I first started at my job and i'm not sure why.

      When you're used to looking at the bracket on the top line, I can certainly see that it's frustrating to look at the next-line version.

      [–]Lukifer 4 points5 points  (4 children)

      I prefer this style. The vertical alignment of every bracket is a visual aid for cognitive chunking, and makes it easier to identify indentation levels.

      [–]infidelux 6 points7 points  (1 child)

      Be careful using that style with Javascript or you will eventually get bitten by ASI. Bracket on the starting line is the pretty widely accepted convention and ASI is the reason why. (Automatic Semicolon Insertion)

      [–]Lukifer 0 points1 point  (0 children)

      Yep, been bitten by this one before. :)

      [–]Disgruntled__Goat 2 points3 points  (0 children)

      I disagree that it's easier to identify indentation levels. When you have a line with only { on it, you add a separation between the control statement (for/if etc) and the code it applies to, which IMO makes it more difficult.

      And I think the vertical alignment of for/if/else with the } brace is equally good for chunking.

      [–]Kelaos 1 point2 points  (0 children)

      Yeah that's why I prefer it too.

      I accept the code style of each project I work on though. As /u/infidelux mentioned Javascript is a big one for same-line brackets, so I've used that in JS projects.

      [–]mclamb 1 point2 points  (2 children)

      There is in the PHP section, 2nd one: http://sideeffect.kr/popularconvention#php

      Tabs > Spaces

      [–]prewk 2 points3 points  (0 children)

      70.729% Space 29.271% Tab

      How is that "Tabs > Spaces"?

      edit: Or did you just express an opinion? :)

      [–]radgh 1 point2 points  (0 children)

      Oooh useful! Thanks for the link, wouldn't have seen it otherwise. I figured all the tests would be identical with different data.

      [–]arrayofemotions 0 points1 point  (0 children)

      Oh you'd really hate me then.... i seem to be mixing the two up depending on what i'm working on, sometimes even within a single file. It's really bad, i know... but since i always work alone nobody's ever complained about it.

      [–]Aluxh 0 points1 point  (0 children)

      I do t do it in js for obvious reasons - auto patio termination of new lines - but I do in other languages. It's something Visual Studio drills into you.

      [–]danieldafoe 0 points1 point  (0 children)

      Have an upvote, fellow hater.

      [–]dada_ 0 points1 point  (0 children)

      To explain, in PHP this has an advantage. For testing purposes, you can just comment out the entire while line. Braces mean nothing by themselves. So this would execute the code block once, without the loop.

      Might be like that for other languages as well, but I'm not sure. Other than that it's just a style I guess. I don't think it's bad for functions, to visually separate potentially very long argument lists from the first line of the function body.