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
-
I usually don't do this since it hampers readibility because of the bloat.
However, there are coding standards (e.g. MISRA) that demand it because it can prevent bugs when you add a statement in the if path and forget to also add the braces.
In a project with such a coding standard, I follow that standard even if I don't like it.
See also https://wiki.sei.cmu.edu/confluence... . -
actually it's relatively a good practice, because if one day you need to add more logic to that single line you won't forget { }
-
C0D4669025yWhen it's something like
If(true)
return x;
else{
//a whole bunch of backup logic
}
No braces are needed, as this if can only serve a single purpose.
However when it's just
If(true)
x = "sweet";
else{
//some more logic
}
Then that if could be extended later and really should contain braces. -
He's introducing you to the type of shit that will be a large part of your software development career---eg code style debates.
People are always giving Uni lecturers stick about being detached from the real world and now we're giving them shit for acting just like devs. They can't win.
Ask him what his favourite editor is and say you prefer another one. -
NullUSER765y@platypus at this point i already displeased him when i handed my tutorial work with if (){}. Need to cool Down for about a month. 😂😂😂
-
I also do this because Rust always requires {}. And by now I prefer it that way, it clearly indicates how that command is called.
And in exchange you don't have parentheses around the condition, so that's nice. -
zetef4395yyeah, sure. not using brackets for a single statement is idiomatic C.
from my experience it makes it even harder to debug programs and i only remove the brackets if it is the final thing i want to write to save the file size (in a contest usually) -
Rikan2835yUsing curly braces even if you habe only one line ist good practice and can prevent mistakes that result in heavy security issues. A few years ago, Apple I think, had some security issues because there was some code that should have been wrapped in an if, but wasn't because of missing curly braces
-
Rikan2835y@Coffe2Code No, they don't prevent security issues, but they help developers to see what belongs to the of and what not. The easier the code is to read, the easier it is to understand and the harder it is to have such bugs
-
irby2285yI also add braces around single line if statements, and it's in the event that I need to add more logic within the if statement. I also find that it is more readable, personally, to have more spaces around code blocks -- and braces help add this space.
Related Rants
in C I use {} for ifs even when they have a single line. for some reason, my C proffesor hated this. For him, this is the equivalent of Tabs vs Spaces.
rant
c
tabs vs spaces