I’m having trouble writing a function without what looks like a messy if/else flow.
I have two sets of news stories (organization and parent), and I need to display ten in total. If both sources have at least five stories, I show five of each. If one source has fewer than five, I can take stories from the other source to make up the difference. Right now I have two functions that both look like this:
if organization_rss_item_count < 5 organization_rss_item_count elsif parent_rss_item_count < 5 10 - parent_rss_item_count else 5 end
In addition to using if/elsif/else here, the #parent_rss_item_count_to_include method is exactly the same code as the above method except the organization and parent roles are flipped. I feel like there must be a better way to do this without two mirror-image methods, both of which have this kind of control flow. Does anyone have an idea of how I can refactor this into something more elegant?