When checking for valid input, we could raise an exception to make it more helpful to the developer doing the implementation.
Current code:
def initialize(params)
super
raise ArgumentError, "Must provide results base dir." unless params.has_key?("dir")
@dir = params["dir"]
end
According to Confident Ruby it seems this is a bad pattern. The right pattern, would be the following:
def initialize(params)
super
@dir = params.fetch("dir")
rescue KeyError
raise ArgumentError, "Must provide results base dir."
end
Is this correct? If so, our issue with this is what if something in super raises KeyError?
I think the disconnect here is that I don't think of the
#fetch
--including theraise
--as failure handling code. It is checking input (and possibly generating, but not handling, an exception). So the whole stanza falls under the heading of "handling input" to me.