Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#ActualCornstalks

Posted 15 April 2013 - 02:23 PM

If I have code on multiple lines, I always use braces. But if the code can comfortably fit on one line, then I use no braces (and only one line). For example:
// The following comfortably fits in one line, so I put it on one line with no braces:
if (x < 0) x = 0;
else if (x > 100) x = 100;

// The following doesn't comfortably fit on one line, so it goes on multiple lines with braces:
if (someObject.getPosition().x < 0)
{
    someObject.setPosition(0, someObject.getPosition().y);
}
else if (someObject.getPosition().x > 100)
{
    someObject.setPosition(100, someObject.getPosition().y);
}
I also put braces on their own line.

But I don't get too religious about it, and if I'm working on a project that uses a different convention I'm fine doing it (like K&R style). I like the vertical real estate that putting braces on the same line as the code saves, but I also like the spacing/formatting that putting braces on their on line gives, which is why I think I can easily switch between styles depending on the conventions of each project. I've learned to find the positives in various styles, and if I'm working on a project where I can't set the coding conventions, I focus on the positives of the chosen style which has helped me get over the religiosity of braces.

#1Cornstalks

Posted 15 April 2013 - 02:18 PM

If I have code on multiple lines, I always use braces. But if the code can comfortably fit on one line, then I use no braces (and only one line). For example:
// The following comfortably fits in one line, so I put it on one line with no braces:
if (x < 0) x = 0;
else if (x > 100) x = 100;

// The following doesn't comfortably fit on one line, so it goes on multiple lines with braces:
if (someObject.getPosition().x < 0)
{
    someObject.setPosition(0, someObject.getPosition().y);
}
else if (someObject.getPosition().x > 100)
{
    someObject.setPosition(100, someObject.getPosition().y);
}
I also put braces on their own line.

But I don't get too religious about it, and if I'm working on a project that uses a different convention I'm fine doing it (like K&R style).

PARTNERS