I've recently been working on simplifying a Ruby method that I didn't write. It's part of a legacy project that a coworker wrote. In general, we did a pretty good job not accumulating code debt, but there are a few methods that need some love.
The method that I've been simplifying was originally a mass of nested if-else blocks. I determined what the method does and simplified the method so there are no longer nested ifs.
Now I want to simplify it even more, and, if possible, I want to eliminate all but one
return false unless condition1 && condition2
@var = SomeClass.getter(foo)
return true unless var.someProperty != 0
@stuff = @var.getsomething id
return false unless @stuff && somethingElse
data = JSON.parse(@stuff)
@stuff.each do |stuff|
return false if data[stuff['something']] != stuff['anotherSomething']
The sample code isn't from something that works because Ruby would refuse to execute that method definition.
This is how I would unravel it, but without sample values for any of the local variables or the definition of
SomeClass there's no way to test it.
def return_bool if (condition1 && condition2) @var = SomeClass.getter(foo) if (var.someProperty == 0) @stuff = @var.getsomething id if (! (@stuff && somethingElse)) data = JSON.parse(@stuff) @stuff.each do |stuff| return false if data[stuff['something']] != stuff['anotherSomething'] end end end end return true end