Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic Code Layout Question

HI, I have a simple question, I've asked 3-4 different people and have had different answers from each of them.

Which code layout is better and used more?

does it really matter as long as it's consistent?

Which is seen as better practice in the world of working as a programmer?

Eg

A)

for(int i=0;i<8;i++)
{
    for(int p=0;p<8;p++)
    {
        if(array[i][p]->Equals(String))
        {
                    //Do Stuff
        }
    }
}

OR

B)

for(int i=0;i<8;i++){
 for(int p=0;p<8;p++){
  if(array[i][p]->Equals(String)){
                    //Do Stuff
                }
        }
}

Thanks, Tim

like image 800
Tim McDougall Avatar asked Feb 20 '26 04:02

Tim McDougall


1 Answers

Several published style guides exist -- for example, Google's is here, and it mandates, for functions:

ReturnType ClassName::FunctionName(Type par_name1, Type par_name2) {
  DoSomething();
  ...
}

and for blocks:

if (condition) {  // no spaces inside parentheses
  ...  // 2 space indent.
} else {  // The else goes on the same line as the closing brace.
  ...
}

with similar examples for other blocks.

So, look around a few such style guides, pick one that's from a somewhat prestigious source and that you like, and if anybody objects to your style just say "oh, I picked it up from X" (where X may be Google, geosoft, or whatever other source you like (many more are listed here).

like image 79
Alex Martelli Avatar answered Feb 22 '26 19:02

Alex Martelli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!