Should curly braces appear on their own line?

  • Should curly braces be on their own line or not? What do you think about it?

    if (you.hasAnswer()) {
    } else {

    or should it be

    if (you.hasAnswer())

    or even

    if (you.hasAnswer())

    Please be constructive! Explain why, share experiences, back it up with facts and references.

    I find the "== true" more distracting than the choice of brace placement.

    @Dan: I think that always explicating the conditional expression greatly helps in clearness.

    Excellent question. Can we get som esources as to where the different styles originated?

    The only reason this would matter would be if your IDE/editor doesn't support matching curly bracket recognition.

    @leeand00: some of us still *print out* complex / unfamiliar code in order to study / annotate it. A good pretty-printer mitigates most of the problems though.

    @leeand00: oh yes. Doesn't matter how big your monitor is, it can't beat crawling over a conference-room floor covered in taped-together printouts with a Sharpie.

    Honestly, aren't there **tools** for this sort of thing?

    Whenever I get code from someone else that has the brackets barfed onto the ends of the if statement and tacked onto the last line of the block, the first thing I do is change it so that I can see what's happening. I get lazy sometimes when it is just one line and leave off the brackets altogether but I'm not proud of that fact.

    sad the question is closed. After some time of indent based syntax usage I switched to (maybe weird) another braces structure. Like your first but closing brace in the last line of block. (after code line)

    Irony: "Please be constructive!" and "closed as not constructive"

    Anyway I prefer the second method. I have OCD, the lack of symmetry annoys me. ;P

    I heard that the second style was invented by low-profile coders who were paid per LoC... That could explain why it's the predominant style at Microsoft ;)

    I prefer the second and third methods for readability, though I'm not sure what the convention is when the condition breaks a line and is indented. Do you indent the parenthesis as well?

    Answering just the title question: no. Curly braces are social animals, and don't like being left alone. If you have only one on a line, make sure you give it some friends: "int main () {{} ..."

    This really shouldn't have an accepted answer seeing as it is mostly up to user preference.

    This is mostly just a preference. I don't have a strong opinion on it either way, other than to say people who put curly braces on the same line should be rounded up.

    the older i get the less whitespace i need

    Coming from a visual studio background and learning Java now, I like the braces on their own line. I used these settings for IntelliJ contains names for the various styles, as well as more information, which I found useful to know.

    I prefer braces on their own line for readability. I know people don't like the white-space, but to me being able to pick out beginning and end of brackets is more important than the extra lines that are gained from making it more compact. Also to be consistent I never assume implicit brackets.

  • dbza

    dbza Correct answer

    10 years ago

    When I was a student I used to put curly braces on the same line, so that there are fewer lines, and the code gets printed on fewer pages. Looking at a single bracket character printed as the only thing in a line is annoying. (environment,paper wastage)

    But when coding large applications, allowing some lines with only braces in them are affordable, considering the 'grouping' feeling it gives.

    Whichever style you choose, be consistent so that it does not become an overhead for your own brain to process multiple styles in related pieces of code. In different scenarios (like above) i would say it is okay to use different styles, it's easier to 'switch context' at a high level.

    On the other hand, the brace on the new line is an ANSI STANDARD, K&R is not. But the beauty about standards is, that there are so many different ones (see also! on uncyclopedia).

    "there are fewer lines" I have Terabytes of Space and lots of Pixels. Why should i care about use more lines?

    @12431234123412341234123: I think he means because some people print the code out for code-review. And each not absolutely necessary newline is paper wasted, or a km² of forrest wasted at scale. However, if you don't print it out (i certainly don't) then ANSI is a lot better than K&R. Also, anybody who intends to print should probably use an automated code formatter - so this should be a question of tooling, not one of coding style.

    I agree that you should stay consistent, I'm been using curly braces on new lines for years, but I have to mix in the other way in my code, such as when calling functions, anonymous functions, object literals etc. Any expression-based languages / anonymous code make brackets on the same line much easier

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM