86

For fuck's sake please add braces for blocks even if there is only a single loc inside. 1 line wont ruin anything but it would be a very big favor for the person reading the code.

I am talking about java. Python guys, you good ✌️

Comments
  • 4
    Nah, if blocks with one statement look bad and no one I know, be it certified Java Dev or a guy who just started working for the first time, ever said that they make reading easier.
    I'm gonna cry if next time I'll see a post saying that breaking if else block into oneliner (return x == null ? y : x) is also bad and makes it hard to read.
    I'll stick to removing them when they are not necessary, thank you very much.
  • 3
    Here's a +1 from a c# guy. It's hard to read code without braces. It's open to mistakes.
  • 1
    C# dev here and I too hate when developers don't use braces.
  • 3
    @Doruk Exactly open to mistakes. Also, due to the formatting, the single line of code may be word wrapped and it may go upto 2-3 lines and ya that is difficult to read and comprehend.
  • 3
    @Agred no I would still prefer an if-else with { } above ternery operator any day. Why are you so hell bent on reducing the locs??? Readable code is the best code.

    Also the scenarios where ? : actualy makes a good readable code is very hard to come by.
  • 1
    Imho it depends. For a longer line I'd definitely still use the {}, but if it's only a "i++;" or "return i;" I find it more readable to just write it without a block.
  • 3
    When I started programming, I avoided having braces if there's one line, till I started working on unit testing and coverage using PHPUnit, it seems it does not know how to evaluate coverage of a block unless it's between braces.
    Now it became a habit to put everything between braces. And to be honest, the code seems more organised.
  • 1
    @bitsnpieces Why am I bent on writing like this? It's probably a problem like with Big and little endian. One doesn't really make sense to people that like the other better.
    I guess the problem is not really with having braces or removing them but with writing clean, readable code. I think that if you write your code good there won't be any issue with or without them.
  • 1
    It makes sense to always add braces all the time, even for 1 line, because otherwise, at some point, someone, either tired or inexperienced (or both), is going to add a line just after the 1 line, thinking it's in the "if" statement.

    And that mistake can cost you, a lot. In fact, I'm pretty sure I heard about this specific mistake in a public report of a costly programming error.
  • 2
    We are still in the middle of tabs vs spaces war in python
Add Comment