if – Why should you always put curly braces: {and}?


I don't understand why everyone says to always use { and } .
For example, here's the code:

    test = true;

Why write parentheses here?
Many people say that for readability, but any programmer knows about such writing, why increase the code?

Also, personally, I don't understand why open a parenthesis should be written on a new line in order to take up one more extra line?

In my opinion, this is wrong, perhaps you can convince me of this?

Personally, it's much faster and more convenient for me to parse this code:

if ($a === $b) bar();
elseif($a > $b) $foo->bar($arg1);
else BazClass::bar($arg2, $arg3);


if ($a === $b)
} elseif ($a > $b)
} else
    BazClass::bar($arg2, $arg3);


The practice of parentheses always saves you the annoying, stupid mistakes.

Horror story 1 . Once upon a time there was a method with a guard condition:


One day you are going home, you are asked to quickly add logging there. We poked on the buttons (maybe even on the hotkey for the live template), committed:

    log.error("Something bad happened");

And they left. And someone then spends a couple of hours to find this nondescript error.

Horror story 2 . Once upon a time there was a code:

if (reallyRareCondition())

One day the junior was asked to temporarily remove the Very Rare Event treatment. He boldly commented out the corresponding line and left to play kicker with his colleagues.

if (reallyRareCondition())
//    handleIt();

And then suddenly Something Very Important stopped working except in rare cases.

PS. All coincidences are not accidental, and the characters are not invented.

Scroll to Top