Technically no but very recommended!!!
Forget about "It's personal preference","the code will run just fine","it has been working fine for me","it's more readable" yada yada BS. This could easily lead to very serious problems if you make a mistake and believe me it is very easy to make a mistake when you are coding(Don't belive?, check out the famous Apple go to fail bug).
Argument: "It's personal preference"
No it is not. Unless you are a one man team leaving on mars, no. Most of the time there will be other people reading/modifying your code. In any serious coding team this will be the recommended way, so it is not a 'personal preference'.
Argument: "the code will run just fine"
So does the spaghetti code! Does it mean it's ok to create it?
Argument: "it has been working fine for me"
In my career I have seen so many bugs created because of this problem. You probably don't remember how many times you commented out 'DoSomething()'
and baffled by why 'SomethingElse()'
is called:
if (condition)
DoSomething();
SomethingElse();
Or added 'SomethingMore' and didn't notice it won't be called(even though the indentation implies otherwise):
if (condition)
DoSomething();
SomethingMore();
Here is a real life example I had. Someone wanted to turn of all the logging so they run find&replace "console.log"
=> //"console.log"
:
if (condition)
console.log("something");
SomethingElse();
See the problem?
Even if you think, "these are so trivial, I would never do that"; remember that there will always be a team member with inferior programming skills than you(hopefully you are not the worst in the team!)
Argument: "it's more readable"
If I've learned anything about programming, it is that the simple things become very complex very quickly. It is very common that this:
if (condition)
DoSomething();
turns into the following after it has been tested with different browsers/environments/use cases or new features are added:
if (a != null)
if (condition)
DoSomething();
else
DoSomethingElse();
DoSomethingMore();
else
if (b == null)
alert("error b");
else
alert("error a");
And compare it with this:
if (a != null) {
if (condition) {
DoSomething();
}
else {
DoSomethingElse();
DoSomethingMore();
}
} else if (b == null) {
alert("error b");
} else {
alert("error a");
}
PS: Bonus points go to who noticed the bug in the example above.