bookmarkd
bookmarkd copied to clipboard
If statements without blocks
Is this a personal preference thing or is there a reason for it? It's come up a few times on the course as it seems quite common to write
if (thing) return a;
return b;
instead of
if (thing) {
return a;
} else {
return b;
}
We've never managed to figure out if there's a reason to do the latter, since the former is cleaner/less code
It's not particularly dangerous, but there are ways you can break that line if you insert semicolons in the wrong place.
Having multi line if statements block scoped, and single line if statements without blocks makes it slightly harder to glance at the code and understand what's happening.
Finally, I can't see any benefit to omitting the braces. You are saving 1 or 2 keypresses and nothing else.