Lucian Ghinda Profile picture
Product Engineer, Ruby on Rails Developer Curator of @shortrubynews Trainer at https://t.co/XFRrJeCJ6K

Aug 28, 2024, 6 tweets

Consider a list of predicates for an object and the fact that there is a logical priority of considering them.
This is just an example, so please do not focus on the requirements themselves.

Left side contains a case statement without a case condition and the right side the same logic implemented with if/else.

Which one do you prefer and why?

Here is an alternative (right side) using guard clauses instead of if/else.
Which one do you prefer and why?

What if there are more statuses to take into consideration?

Which one do you prefer and why?

Somehow, for me, the case statement makes more sense when I take into consideration:

• the order in how the conditions are assessed

• it is a bit more open to add more statuses

• I somehow feel a bit uneasy after using 2 guard clauses (or early returns) in a method

But I like also the variant with guard clauses or the if/else.

@igor_alexandrov I know Rubcop has a rule about this which maybe it is on by default? But I was wondering why is that

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling