views:

78

answers:

4

I couldn't seem to find a good question on SO, or a good Google search that seemed to answer my question. What is the industry or popular preference when it comes to formating code "chunks" and their brackets. Here are some examples for what I am trying to explain...

if($foo)
{
    return $bar;
}
else
{
    return $stackOverflow;
}

VS

if($foo){
    return $bar;
}else{
    return $stackOverflow;
}

Of course, with similarities in the other major languages, and of course with any code blocks that use curly brackets. I have seen across the board both methods used, and the first one seems to make more sense as it allows one to quickly look up and down to find the matching bracket.

What are your thoughts?

+4  A: 

Depending on whether or not you are working with a company that prefers a standardized convention, you would be obliged to use that.

Otherwise, use what you feel comfortable using and be consistent.

Response to OP's comment:

Adapting to a standard before knowing what the specific companies standards's are might not be worth while. Depending if you hop from one job to another they may have a completely different set of standards that you would have to now re-adapt to.

Anthony Forloney
This is true! But I would rather "become" comfortable with the more popular method. I am very standard in my formatting, but does my question make more sense from that viewpoint?
Urda
I have seen a good majority of people use both conventions, which conventions were first taught to you when learning your first program? The first or second? Whichever one, stick to it. If you are not working for a company where they laid out the "standard" convention, then there is no need to worry :)
Anthony Forloney
A: 

I prefer the second one, because I don't consider curly bracket an statement. Anyway, just my thought.

Aito
A: 

Sigh. It doesn't matter. Stick with whatever the standard is where you work. If there is no standard, be consistent when writing your own code, and use whatever is already there when modifying others code.

Kevin
+2  A: 

Most companies i worked for used

if ($foo)
{
  bar();
}

As i currently prefer to develop in C# which follows this style it now comes naturally.

I've also noticed that it greatly depends on the language, C(++) and Perl programmers prefer {} on the same line, C#, Java and PHP developers prefer { and } on their own lines.

dbemerlin