Using throw and catch to tidy up our code
Let’s say we want a simple controller action that checks, if a given code is valid.
It should return true and false and, if the code is invalid, give the reason (whether that is because it is unknown or because it has been used already). The action could look like this:
def valid if code = Code.find_by_value(params[:id]) unless code.used? respond_with :valid => true else respond_with :valid => false, :reason => 'used' end else respond_with :valid => false, :reason => 'unknown' end end Now this deep nesting effectively hides the underlying algorithm, a simple one in this case.
Expressing this using throw and catch straightens the code a bit:
def valid failure = catch :fail do code = Code.find_by_value(params[:id]) or throw(:fail, 'unknown') code.unused? or throw(:fail, 'used') nil end respond_with({:valid => !failure}.merge(failure ? {:reason => failure}: {}))endWe are down to one respond_with line but the has merging and the throws at the beginning of the line are not particularly pretty.
Let’s introduce a little Ruby mixin for the Hash class that provides it with a compact method that its friend Array has had all along:
module HashExtensions def compact self.reject{|key, value| value.nil? } endend
class Hash include HashExtensionsend
Now using this to clean up the response hash and moving the throw statements to the end so the steps of the algorithm are visible we have our final version of the action:
def valid failure = catch :fail do code = Code.find_by_value(params[:id]) or throw :fail, 'unknown' code.unused? or throw :fail, 'used' nil end respond_with({:valid => !failure, :reason => failure}.compact)end 