I personally passed the entire conditional statement to a separate method. This may sound very similar to what has already been suggested, but I put all this into the method instead of breaking it.
simple_body if complicated_condition def complicated_condition condition1 and condition2 and condition3 and some_more_complicated_condition_that_cannot_be_written_on_a_single_line and still_some_more_complicated_condition_that_cannot_be_written_on_a_single_line end
I can or cannot break down the condition into more methods depending on the conditions and whether I will use these methods later (too many methods used for one purpose only begin to become code smells).
This makes the code readable (I can hide the code and see what it does) and supports it (I can change the condition if necessary, and I know exactly where it is).
If I put this in a class, I would put the method under private , since there is no reason why the "external" should use it.
EDIT: If conditions require variable values ββduring use in a condition, perhaps consider switching to the binding method.
opt = :mysql simple_body if complicated_condition(binding) opt = :oracle simple_body if complicated_condition(binding) def complicated_condition(b) condition1 and condition2 and condition3 and some_more_complicated_condition_that_cannot_be_written_on_a_single_line and still_some_more_complicated_condition_that_cannot_be_written_on_a_single_line and eval('opt', b) == :mysql end
source share