Jump to content
  • Advertisement
Sign in to follow this  
Grahf750

Popular coding style?

This topic is 4349 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

95% of the code I see has code that looks like this:
public void doesSomething(int x){
    if (x == 0){
	// stuff
    }
    else{
	//other stuff
    }  
}



When learning to code I learned to do it like this:
public void doesSomething(int x)
{
    if (x == 0)
    {
	// stuff
    }
    else
    {
	//other stuff
    }  
}



The way I learned seems much more readable to me with the brackets lining up. It also makes it easy to debug when your missing a curly brace. The problem is that when looking at the first example it takes me longer to understand and grasp it then the second because I am less familiar with it. I am wondering should I switch now to the first example because almost everybody else uses it and I will waste a lot of time trying to understand things simply because of my different format? Also I know jobs have certain requirements for the way things should be coded. Would something like this part of those requirements?

Share this post


Link to post
Share on other sites
Advertisement
I personally use the second style, because its what I learned in originally. Honestly, once you've looked at enough code its easy enough to read either style. I would program in whatever you're most comfortable with, unless you're working with a team who all strictly adhear to the first style.

Share this post


Link to post
Share on other sites
I would just choose a format that works for you and is sensical and stick with it. I have come across a ton of styles, all of which are "the best," and none of which fit just right with me.

Personally, my style adjusts over time as my experience dictates what is more readable and easy to type.

I would have written it:

public void doesSomething( int x ) {
if ( x ) {
// other stuff
}
else {
// stuff
}
}

Share this post


Link to post
Share on other sites
My advice is to be flexible. Learn to read code that's in any half-way reasonable coding style. If you have trouble reading other styles, you'll lose out on a lot of resources. Also, be willing to change your style to match the requirements of your project.

I code in:
1) The style laid out in the project's guidelines
2) Failing (1), the style in which the majority of the code is already written
3) Failing (2), the style of the dominant libraries in the project (not really applicable when discussing bracing style)
4) Failing (3), roughly the first style you listed (it's my personal preference)

I probably would have written it:
public void doesSomething(int x) {
if (x) {
// stuff
} else {
// other stuff
}
}

I rely strongly on indentation to clarify scope.

EDIT: Removed quote, it wasn't need

Share this post


Link to post
Share on other sites
The second example is more readable to me, the first looks like a lot of effort is taken to save as much space as possible. Only sometimes I make an exception for single statements and if statements, like:

for (int i= 0; i < 10; ++i)
if this and that related to i
do something with i;

If it's a really whacky style to read, there are source code beautifiers, like uncrustify

Share this post


Link to post
Share on other sites
Meh. Personally, I prefer brace after keyword:

public void doesSomething(int x) {
if (x) {
// stuff
} else {
// other stuff
}
}


Always bracing after ifs. It's not really that big a deal though. If you're using VS2005 (and likely others), you can specify your own preferences and the IDE will reformat others' style into your own (and then back to the company standard).

Share this post


Link to post
Share on other sites
I like to use a hybrid of the first and second methods, unless my supervisor makes me use the first. Sometimes I use the second if I need to see the brackets' alignment.


public void function(int x)
{
if (x == 0){
//stuff
}
else{
//stuff
}
}

Share this post


Link to post
Share on other sites
I found a very good reason to abandon the second style after a bit of experience, and that was due to misreading code. Let me explain:

A new scope {} can be created anywhere. I did not know that. So I saw code similar to this:
x=0;
if (cond) x=1;//skip first element
{
for (;x<10;x++)
//something done here
}


I misread this as conditionally executing the for loop, while it was actually conditionally skipping a single iteration. Sure I could have blamed the person that wrote the code, but instead I switched styles and aimed to be able to read all code no matter how much I disagree with the style.

Shortly afterwards I was on a project which had a practice of commenting every ending-brace with what it corresponded to. This made the transition easier.

Most people I have encountered had an easier time learning with the second style, but everyone seems to eventually move to the first. I doubt it's always for the same reason I did, so maybe it's more information fitting on a single screen/page or just people adapting to the norm.

Share this post


Link to post
Share on other sites
Quote:
Original post by richardurich
...

You didn't misread the code, the original author miswrote it. Changing styles won't do a thing to prevent such misunderstandings.

CM

Share this post


Link to post
Share on other sites
Quote:
but everyone seems to eventually move to the first


I beg to differ. I used to do it the first way; now I do it the second way. And though I can certainly read both styles fine, I much prefer the second.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!