Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Skayo85688yI mostly hate statements like this (with whitespaces at the condition):
if ( condition ) {
// code
} -
Root797268yI can think of three patterns where foregoing curlies ({} brackets) is acceptable. Each one only applies when the block is very simple -- as in a single, very concise, easy-to-read line.
// multiple (very simple) ifs with (very simple) blocks
// (e.g. guards, or a series of conditions similar to one another)
If (x) a;
If (y) b;
If (z) c;
// multiple nested ifs with (very simple) else clauses
If (x) {
a;
If (y) {
b;
If (z) {
c;
} else w;
} else v;
} else u;
// a one-line block nested several layers deep
// (I personally dislike this, but find it acceptable)
If (x) {
for()
for()
a;
}
In general, foregoing curlies is only ever acceptable when:
a) the block is very very simple
b) doing so improves readability
c) it's still extremely obvious what the code is doing
d) you can tell at-a-glance where all the blocks end
For everything else, use curlies! -
silkfire1538yTotally agree with you. If I'd have a penny for every time I fixed my colleagues' missing brackets I'd be quitting my day job by now ;)
-
nocgod17558yIt's generally a bad practice (imo) to leave the if without scopes even if syntactically it is ok.
The main problem is that people sometimes add statements or comment out the statement and then the behavior is not as expected if they forget to scope the if or comment it out.
The only place when I leave the if without brackets is where the action is when validating I put and throwing exceptions.
if (string.IsNullOrEmpty(name)) throw new ArgumentException(nameof(name), "should not be null or empty"); -
@davide it is not about syntax errors, it is about what bothers one programmer and not the other.
-
beriba8558yBrackets. Always. Period.
You're not writing code for yourself if you work in a team. In that case remember one important rule: Explicit is better than implicit. Save your teammates a trouble and write clean explicit code. In the end your project and your reputation will benefit from that. -
Maybe if this guys used brackets the bug wouldn't appear (if a git merge) or could be spotted easily in code review
https://google.ie/amp/s/...
Related Rants
I so hate the following statement
if (condition)
makeOperation()
I prefer
if (condition) {
makeOperation()
}
Who is with me?
undefined
if
brackets