55

I always use brackets for clarity even if there is only one statement inside them

if (boolean){
function ();
}

Cus it's so much easier to read, and if I need to add statements after the if I don't need to remember to add brackets. Plus the else may need brackets and an if with no brackets but an else with brackets looks awful.

Comments
  • 1
  • 5
    Python takes away that opportunity. It's all spaces and indentations.
  • 5
    Im on the fence with simple one line ifs.

    Like:
    if (asd === null) return;

    Breaking those into multiple lines makes my design nerve twitch. It seems too long and drawn out. On the other hand your points make sense and I don't really have a good justification for having them on a single line so I always try to write:
    if (x === null) {
    return;
    }

    Tho here's the kicker. By same logic, you may want to add conditions. And when you do you should also split the conditional part. So then you should write it like so so you can easily add conditions later:
    if (
    x === null
    ) {
    return;
    }

    But now it's really fucking drawn out.
    I don't know... Currently I stick with what the OP suggests. Seems like the best of both worlds

    Also, if you write:
    if (x === null)
    return;

    then please fuck off! Go die in a hole. Burn!
    No-one can read that shit and it provides 0 benefit. It even looks ugly 😄
  • 2
    @Froot
    if (
    x === null
    ) {
    return;
    }
    That's just beyond ugly
  • 1
    @Froot Yeah, I absolutely agree with the one-liner thing. Do it all the time.
  • 4
    I disagree in cases of multiple single-line blocks, e.g.

    if (!flag_one) blah;
    if ( flag_two) blah;

    The aligning absolutely makes it easier to read.
  • 1
    @fun2code I don't ever find scanning a problem.
  • 1
    What about python? #gottemmmmm
  • 0
  • 1
    I seriously like the one-line-style from bash:
    [ $a -eq $b ] && do_stuff || do_other_stuff

    Makes code look so advanced 😂
  • 2
    I use auto format package with my sublime and it put {} even if I wrote a one line if statement.
  • 3
    @Ashkin I usually use switch statements instead of multiple one liner if.
  • 0
    We have a badass Dev here !!!!
  • 0
    @mrlinnth that works, too.
Add Comment