`return` with no `else` — no more papercuts

I understand this or that omission in given language — not enough resources, somebody was not bitten by specific error, but it is strange when you see on multiple occasions a guideline which goes against software safety. Consider such code:

def foo() Int
  if x > y then
    return 17;
  end
  return 44;
end

Such code is legit in every language I know, what’s more it is advised way to write according to Mozilla guidelines and Go as well ¹. I admit it is more pleasing for the eye than version with added explicit `else`, but when it is time to refactor the code it is way too easy to make a stupid mistake like this:

def foo() 
  if x > y then
    my_flag = 17;
  end
  my_flag = 44;
end

I noticed it because I was hit by it — you don‘t have clear mind all the time. And since Skila is designed to prevent exactly this kind of errors (“stupid mistakes”) I am about to add not even an opposite guideline, but simply a check — any flow interruption instruction has to be followed with `else` branch. Safety first.


¹ Read more: “No-else-after-return” considered harmful, Effective Go.

Advertisement
Tagged , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: