Always write if statements with braces

This tip submitted by Webmaster on 2004-12-29 00:00:00. It has been viewed 56081 times.
Rating of 5.6 with 615 votes



By putting braces around every block of code you write, you ensure that future edits won't introduce bizarre bugs.

If you have a one-line if statement:

if(<condition>)
    execute();


you should still surround
execute();


with braces:
if(<condition>)
{
    execute();
}


Now, if you go back and add a second instruction
if(<condition>)
{
    execute();
    execute2();
}


you don't have to worry about putting in the braces, and you know that you won't forget to put them in.



More tips

Help your fellow programmers! Add a tip!