Question:
I don't understand why everyone says to always use {
and }
.
For example, here's the code:
if(1>2)
{
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);
How:
if ($a === $b)
{
bar();
} elseif ($a > $b)
{
$foo->bar($arg1);
} else
{
BazClass::bar($arg2, $arg3);
}
Answer:
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:
if(somethingBad())
return;
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:
if(somethingBad())
log.error("Something bad happened");
return;
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())
handleIt();
doImportantThings();
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();
doImportantThings();
And then suddenly Something Very Important stopped working except in rare cases.
PS. All coincidences are not accidental, and the characters are not invented.