5

I'm reading through the Zend Framework coding standards, where they state that curly brace after a Class definitions should be on the next line, the "one true brace form".

class MyClass
{
    function....
}

I usually have the braces on the same line:

class OtherClass {
    function ...
}

What's the reason for putting the brace on the next line? Or using any other style, for that matter?

1
  • Objectively, the first style you mentioned helps to visually find the matching bracket and helps to visually spot start and ending of the blocks of code. Consequentially it is objectively the best style to use in programming because helps the programmer to mind-organize the code and distinct at a glance various parts of code. The other is instead much more confusing. I Never use the second style and I hope in the future nobody will anymore because its only advantage is to use one less line so Objectively readability has priority for me reading my own code and for others reading others code. Oct 1, 2022 at 18:15

9 Answers 9

9

Having the braces on lines by themselves helps to visually separate the inner part from the outer part. That makes it easier to quickly scan through source code and distinguish blocks from their surroundings.

Plus, having the braces at the same indentation level makes it easier for the eye to find a match.

8

I find that the first style you mentioned helps visually offset the class name from its member definitions. This helps me find the top of the class declaration more easily when scanning code.

6

Personal preference is really the only real "reason".

4
  • 2
    I think there is more than just "personal preference". The programmers being human, are subject to cognitive restrictions that would make some format (whichever it be) be more useful to our brain when it needs to extract information from an "image" (e.g., separation in blocks).
    – Jorge
    Jun 24, 2013 at 19:25
  • Moreover, I wonder why we usually discuss the formatting differences in code and not in other standard text languages, like XML. I don't remember seen XML formatted in the K&R style (the second one in the original question).
    – Jorge
    Jun 24, 2013 at 19:28
  • I've found that for most cases, separate lines are more followable, but one case, UIView blocks, it's easier to read K & R. This is a matter of ability to follow lines on screen with my eyes, not related to personal desires to use one style over another.
    – Stephen J
    Nov 26, 2013 at 1:21
  • also, thanks for the tip on what to call it :D
    – Stephen J
    Nov 26, 2013 at 1:22
3

Often cited reasons are:

  • easier to match up opening and closing braces (for the first example)
  • don't waste an other line (so that you can fit more lines of code on the screen - the second example)

As others have said: if you work on 3rd party code, just follow its conventions. If you work on your own code, just use whichever style you find better.

3

I think coding style and naming conventions on personal or team projects are mostly a matter of personal taste (although it is wise a team adopts a single coding style and naming conventions).

Personally, I like to follow the Allman Style convention, as it gives me a quick overview of my code and indent structure. Sure, it will cost you some extra lines in your code, but I don't think that weighs up to the advantages.

Good resources on this matter are the following Wikipedia articles:

Indent Style

Programming Style

Naming Conventions

1

User preference. It really makes no difference. When I developed in PHP, I used the second option, but now using C#, I use the first.

1

There are already several topics on this highly subjective topic...

Some people are passionate about it, personally I chose the "readable" option of aligning braces (I don't pay for real estate used by my code on screen... so compactness doesn't interest me) but when I contribute to a project using another style, I just use the one around my contribution.
Same for tab size, hard vs. soft, etc.

0

The real reason is to have uniform looking code throughout the codebase. Both styles have their advantages, which style is "better" depends on personal preference and what has been used "traditionally" in the language and the project.

If a style of braces is prescribed, use that, else use what was being used before you arrived. If you're starting a new project use what is normally used in the language of choice.

Same goes for tabs vs. spaces.

0

Does it matter which brace style you use? No. Does it matter that everyone working on the same project or source file use the same brace style? No; not only does it not matter, but it's useful if brace styles differ from one coder to the next, even in the same file.

"Why exactly does this chunk of code do this?"

"I don't know. It's in Kevin's handwriting. Let's go ask him."

Not the answer you're looking for? Browse other questions tagged or ask your own question.