Reputation: 29316
I'm aware that with if statements, if the statement in the body of the if statement is only one line long, you don't need to wrap the if statement in curly braces.
I prefer to anyway, though, because sometimes you'll add more lines in later, it's easier to see where it ends, etc. I just prefer to always wrap it in curly braces.
Is doing this bad style for coding? Should I switch to not using curly braces if it's only one line long? Or is this alright?
Upvotes: 1
Views: 55
Reputation: 389
Most style guides I've seen (not many) say seperate lines, but say that the brackets are optional. The reason for the seperate lines is debugging, so that you can see if the statement evaluates true or not.
For example, here http://geosoft.no/development/cppstyle.html#Conditionals, for C++. I would imagine most languages are similar.
So for debugging reasons, the braces are optional, but I think it looks better and as others have said, makes it easier to add lines later.
Upvotes: 2
Reputation: 1937
Unless I'm having to match the existing style of another project, I typically always include braces, even if the body of the if statement is only one line long. To be honest, I think it's more a matter of preference unless you're dealing with code on a project with a style that dictates a specific practice.
Upvotes: 1
Reputation: 3273
It is generally better style to always use curly braces. It makes your code more uniform in appearance, and as you mentioned, it's better if you end up adding lines later.
Upvotes: 2