howdy fellers and felletes:
I recently made a pull request on a client project. They have an existing dev team that I work with.
The pull request was for a class and some component classes totaling several hundred LOC that generates a complex report. I really like making functions to break code up into bits:
def render
rows = big_things.inject([]) { |rows, t| append_little_things(t, rows) }.flatten
handle(rows)
end
def big_things
BigThing.where(...).all
end
def append_little_things(thing, rows)
group = ThingGroup.new(thing)
rows.push(group.to_a)
end
The dev team said my code was too complex, difficult to follow and was contrary to the current pattern for writing report generation code. After changing it and resubmitting the pull request, this was preferred to them:
def render
# Get the things
big_things = BigThing.where(...).all
# Track rows
rows = []
# Function for Building a row
build_row = ->(x, y, z, e, m, c) { [....] }
big_things.each do |t|
# Build a row for each child, otherwise
# render a single row
unless t.children.empty?
t.children.each do |c|
rows << build_row.call(t.somethin, t.yep, t.mhm, c.x, c.y, c.z)
end
else
rows << build_row.call(t.somethin, t.yep, t.mhm)
end
end
handle(rows)
end
Was I wrong in thinking that way when I made the first PR? I'm all about code-hiding, breaking out logic into separate classes, bringing deeply nested statements out into 2-space indented methods and writing many functions with a small LOC /w descriptive names. Is that silly?