tags:

views:

480

answers:

11

I know it doesn't matter to the compiler, but for best programming practices are any of these a superior style to the other?

function myfunction () {


}

or

function myfunction ()
{

}

I have seen both and sometimes I see:

function myfunction ()
{
  if (something) {

  }
}

so it is mixed.

I remember seeing some people say that one of those is "bad" (the first one), but I don't know why. Is one better than the other and please why? Thank you.

EDIT: Thank you for your answers. If you like one style over the other, though, can you tell me why, please, not just that you prefer one. I understand now it is subjective, but really thought there might have been some objective reason. Apparently, not.

EDIT: I see this was closed as argumentative. That is absolutely false. I was genuinely concerned. I appreciate all the answers. I agree, after reading the answers, it is subjective, but did not know that before.

+13  A: 

This is down to personal taste, and anyone who says otherwise is probably red in the face with anger, and should be quietly ignored.

The only rule that can be reasonably made here is to be consistent with the code you're changing, and be consistent with the code you write (so the third one is bad).

Dominic Rodger
+2  A: 

I prefer to line up my braces to easier read the blocks. However, in most languages it comes down to personal preference.

Mr. Will
+3  A: 

The third one is the only one I would consider bad. Inconsistency is bad.

I prefer the look of the second and it's my opinion that the first is harder to read.

Spencer Ruport
Why do you think it is harder to read? Thanks.
johnny
+2  A: 

If you want to base the answer on how Visual Studio 2008 auto-pretties the bracket placements, it would be:

function myfunction ()
{
  if (something)
  {

  }
}
Keith
+1  A: 

There is no "best practice" for positioning of statement block brackets; it depends on your programming style and on whichever one you prefer. Of course, it's probably not a good idea to mix the two styles, as there is the potential for confusion.

Personally, I prefer the latter style.

JAB
+3  A: 

Use whatever is the standard in your language and use it constantly.

In Java the first one is the official recommendation from Sun. Some other languages use the second one more often. The third style is K&R style and I've seen some C projects using it. But each project may have its own preference.

Esko Luontola
very helpful. thanks.
johnny
thank you to everyone. all comments are answers and good.
johnny
A: 

I prefer the first one but I come from a strong university java background.

I think this depends on the language you are using and the styles that are common in this language. If you are working in a team the most important thing is consistency in the team.

Janusz
A: 

Indeed this is personal taste - except when the language really wants to set a ; at the end of the line

Javascript :

return       // here javascript will set a ; at the end and return undefined.
{
  val : ""
}

So I guess it depends on personal taste and the language :)

sirrocco
+1  A: 

it depends

in Java there is a widely accepted conventions to put the opening brace on the same line:

public void foo() {

in C there are several conventions, one of the most widely accepted looks like:

void foo() 
{

it is not only a personal taste problem, but you should try to follow conventions of your language or team. The most important thing is about consistency.

dfa
+1 QFT: "follow conventions of your language or team". That's by far the most important factor here. Everything else is commentary.
Mike
+2  A: 

This subject is a Holy War.

See indent style in the Jargon File.

scvalex
I did not know it was a "holy war" or I would not have posted it. Sorry everyone.
johnny
A: 

I prefer the second method, but I sometimes use the third as well. It's a very subjective matter with no definitive answer (there is no "best" way), and generally follows personal taste.

However, the most important thing about brace matching is that whichever method you use, you do it consistently. Many development shops will have a coding standard that addresses brace matching for this very reason.

zombat