Stupid Question 219: In C# should you use braces on one line if-statements? And are one-liners on the same line considered a bad practice?
Now here are two questions I’ve had for a while, as I’ve seen both ways by good developers and I’m a bit unsure what the most popular rule is. If an if-statement only executes one line then you can omit the braces, I tend to do that as the braces add too much noise. I know some might argue that it might cause problems if you change the code later, I would argue that you would read the code before changing it and therefore you shouldn’t miss adding the braces. But it is absolutely a valid point, I might be the one being wrong here thinking ‘should we add things just in case?’. Look at these two examples, which one would you consider to be the best practice?
if_two And what about the one-liners? Such as this one:
if_three I’ve been avoiding them, although I might have used one here and there (I know consistency is key but I’m pretty sure that I’ve unknowingly been inconsistent a few times although I do my best not to do so). I read somewhere that we read better from top to bottom than left to right (in regards to line length). I’m very curious what you do and what you think :)
Last modified on 2013-07-15