intention

Jared Carroll

Now conditional logic asks questions, which sucks, but its always still in code (try to get rid of it though). In Ruby, we have the ability to use the ? in method names e.g. #empty?, #has_key?, etc. I’m to the point now where I want to see a ? method in every conditional.

I don’t want to see some condition like:

if group_membership.group_membership_type == 'Admin'
end

I want this:

if group_membership.admin?
end

I’m not arguing for the sake of DRYness either, even though it is DRYer because you’ll probably duplicate this code…

group_membership.group_membership_type == 'Admin'

…in a couple places. But even if the condition is only in 1 place, I still say refactor it into a query method.

My argument is for clarity and understandability. Why keep a cryptic conditional, when you can put that conditional behind a nice intention revealing message. Especially since Ruby allows the ‘?’ character in method names, now you can get even closer to normal language. Ruby aside, this refactoring is applicable in any language.

Fowler has a refactoring in his catalog related to this: Introduce Explaining Variable which is basically a rehash of some ideas found in here