bookmarkd icon indicating copy to clipboard operation
bookmarkd copied to clipboard

If statements without blocks

Open Jwhiles opened this issue 8 years ago • 2 comments

Here - don't do it kids.

Jwhiles avatar May 11 '17 18:05 Jwhiles

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

oliverjam avatar May 11 '17 22:05 oliverjam

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.

Jwhiles avatar May 12 '17 06:05 Jwhiles