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
Written by Emmanuel Turlay
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#