From: Per-erik Martin Date: 2013-06-12T17:23:43+09:00 Subject: Re: How would I refactor these four `unless` statements? Rafal C. wrote in post #1112088: > Let's say I have the following four `unless` statements in a row. At > first I thought about using `if` and `elsif` statements and negate the > condition but I'm not sure which way is suitable/readable/good-practice. > > unless @params.include?(:js_code) || @params.include?(:code_url) > raise BadParameters, 'You must include at least one...' > end > unless @params.include?(:compilation_level) > raise BadParameters, 'The :compilation_level...' > end > unless @params.include?(:output_format) > raise BadParameters, 'The :output_format...' > end > unless @params.include?(:output_info) > raise BadParameters, 'The :output_info...' > end > > Thanks in advance! I think they're fine. When it comes to logical statements for error handling, keeping it simple, systematic, and easy to understand is a good thing. Building some larger, more complex expressions just to make the code shorter will only obfuscate it and require a reader to do more interpretation in the head to make sense of it. Possibly, you can define a helper method for the simple cases like this: def required(name, message) unless @params.include?(name) raise BadParameters, message end end and turn three of them into: required(:compilation_level, 'The :compilation_level...') required(:output_format, 'The :output_format...') required(:output_info, 'The :output_info...') If the messages are completely uniform (except for the parameter name of course), you can also do it this way: def required(names) names.each do |name| unless @params.include?(name) raise BadParameters, "The #{name} ..." end end end required([:compilation_level, :output_format, :output_info]) -- Posted via http://www.ruby-forum.com/.