19

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.

Comments
  • 10
    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... .
  • 9
    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 { }
  • 0
    When 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.
  • 6
    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.
  • 1
    @platypus at this point i already displeased him when i handed my tutorial work with if (){}. Need to cool Down for about a month. 😂😂😂
  • 0
    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.
  • 0
    yeah, 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)
  • 0
    Using 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
  • 0
    @Rikan that doesn't mean that braces prevent security issues!
  • 0
    @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
  • 0
    Its obvious, Its not a coding standards
  • 0
    Wait till you discover ruby poetry mode
  • 1
    it's because {} looks like a vagina
  • 0
    I am also another person that does this.
  • 0
    I 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.
Add Comment