0

Note: I'm not sure if this is the right place for this. If another site suits this question better, please point me there. I was going to place it on cs.SE, but there was no tag about formatting, so I figured it didn't belong there, and this isn't actually runnable code, so I figure it doesn't belong on codereview.SE either.


When I was first taught to program in Java in 2009, my teacher (and all the examples from the book) had the code formatted as such:

public void method()
{
  if (condition)
  {
    doStuff();
  }
}

However, in the field, I rarely, if ever, see it like that. Almost always, it's like this (whether or not it's Java code):

public void method() {
    if (condition) {
        doStuff();
    }
}

Why do so many programmers choose to put the curly braces on the end of a line instead of their own line? Is it to save scrolling length or file size? Perhaps it was the standard before my education? To me, it just makes it less clear whether we're entering a new block.

Of course, I can chock up doing things like how many spaces to indent or adding spaces within parentheses, like if ( condition ), to personal preference, since it's not done nearly as often, but placing the brace at the end seems to happen way too often to just be personal preference.

Ky -
  • 30,724
  • 51
  • 192
  • 308
  • Sorry, voting to close as it is an opinion-based question. I don't know if codereview would be a better place to ask the question. Maybe someone else can offer advice on that. – ZombieSheep Jul 29 '14 at 06:22
  • 1
    Programmers for you, http://programmers.stackexchange.com/questions/2715/should-curly-braces-appear-on-their-own-line – 9dan Jul 29 '14 at 06:24
  • Sorry, that post was "closed as not constructive by Jeff Atwood♦ Sep 26 '11 at 3:19" – 9dan Jul 29 '14 at 06:25
  • Indentation makes it clear you're entering a new block. You only get a handful of lines of code on a screen, why would you waste an entire line on a single open brace? – TessellatingHeckler Jul 29 '14 at 06:26
  • 2
    I disagree with the closure - the question "why is this a common format?" is a valid historical inquiry which is what Supuhstar seems to be primarily asking. – Brad Jul 29 '14 at 06:27
  • 1
    I too prefer the first style (aka "Allman" or BSD). But I think the second one is indeed driven by the need to preserve screen estate (less lines). Here is an overview of several different styles: https://en.wikipedia.org/wiki/Indent_style –  Jul 29 '14 at 06:29
  • @BitBlitz - it was closed because there isn't a definitive answer. The answers could be anything from "My teacher always said to do it this way" to "I don't want to wear out my return key by overusing it". Neither are particularly helpful. – ZombieSheep Jul 29 '14 at 06:34
  • 2
    @Zombie: Few historical "why" questions have a nice definitive clear-cut binary answer, but that doesn't mean an honest historical inquiry from a new practitioner of our art is invalid. – Brad Jul 29 '14 at 06:39
  • @BitBlitz - Agreed, but that's not what SO is here for. – ZombieSheep Jul 29 '14 at 06:41
  • @TessellatingHeckler indentation also indicates a continuation of the previous line and blockless structures like `if (contition) doStuff();` – Ky - Jul 29 '14 at 16:33
  • @ZombieSheep I'd hoped there was a definitive answer. Perhaps an answer telling me that there's no definitive reason would work better than a hold. – Ky - Jul 29 '14 at 16:34

1 Answers1

1

My personal preference matches your first example, but I agree the same line curly brace is exceedingly common.

The reason (as I understand it) is that the same line format was the style in the original Kernigan & Richie (K&R) book The C Programming Language.

Brad
  • 3,190
  • 1
  • 22
  • 36
  • 1
    Why the down vote? I was answering the question "Why is this so common?" which seems a valid fact-based question based on the history of computing - regardless of one's own personal preference – Brad Jul 29 '14 at 06:24
  • 1
    K&R actually mixed the two styles: https://en.wikipedia.org/wiki/Indent_style#K.26R_style –  Jul 29 '14 at 06:27