Last Updated: October 29, 2016
·
220
· neutralino1

Conditional method call pattern

In Ruby, and other languages, we often do this

def perform
   perform_action
end

def perform_action
   if should_perfom_action?
      action.perform
   end
end

def should_perform_action?
   # conditions
end

I find that it is more explicit and correct to follow this practice instead:

def perform
   perform_action if should_perform_action?
end

def perform_action
   action.perform
end

def should_perform_action?
   # conditions
end