views:

281

answers:

8

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  A: 

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.

Kyle Trauberman
+6  A: 

Personal preference is really the only real "reason".

levi rosol
+4  A: 

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.

Greg Hewgill
+4  A: 

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.

HS
+1  A: 

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.

Cd-MaN
+1  A: 

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.

PhiLho
A: 

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.

+1  A: 

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

Aron Rotteveel