Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualServant of the Lord

Posted 15 April 2013 - 02:47 PM

My method is similar to the one CornStalks mentioned, with minor differences.

For simple single-line statements, I'd go without brackets:
if(...) //simple single-line statement
if(...) //simple single-line statement
But if they are chained ('else-if' or 'else'), I make the entire chain use brackets:
if(...)
{
    //...more complex statements....
}
else if(...)
{
    //...more complex statements....
}
If an 'if' is stand-alone, but is multi-lined, or even single-lined but cluttered, I'll fully brace it.
Or, even if the statement itself is simple, but the conditional is more complex, I'd brace it.
if(x == foo && y == (N - 7)
|| z == PI)
{
    //Fully braced, because of non-simple conditional.
}

#2Servant of the Lord

Posted 15 April 2013 - 02:43 PM

I also prefer the way that CornStalks mentioned, though I remove the space after the word 'if':
if(...) //simple single-line statement
else if(...) //simple single-line statement

if(...)
{
    //...more complex statements....
}
else if(...)
{
    //...more complex statements....
}
I also don't mix and match them in one chain:
None of this:
if(...)
{
    //if... 
}
else if(...) //else-if...      //<-- Braced in the midst of non-braced? No thanks!
else
{
	//else...
}

#1Servant of the Lord

Posted 15 April 2013 - 02:42 PM

I also prefer the way that CornStalks mentioned, though I remove the space after the word 'if':
//Simple statements:
if (x < 0) x = 0;
else if (x > 100) x = 100;

//More complex statements:
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 don't mix and match them in one chain:
None of this:
if(...)
{
    //if... 
}
else if(...) //else-if...      //<-- Braced in the midst of non-braced? No thanks!
else
{
	//else...
}

PARTNERS