Say I have a customer class
class Customer(name, age)
somewhere in my code, I have a list of customers
List[Customers]
at some point, I need to transform this list of customers into a Map with Key = age
, and value = List[Customer]
like below
Map
15 -> List[Customer]
21 -> List[Customer]
33 -> List[Customer]
Though this is a bit simplified. In my actual code base I am performing some more complex aggregation. In that regard, I have 1 "transformer" function that is 50ish lines long that does some aggregating on the Customer
class and adds it into a map like shown above. This function then returns that map. Unfortunatly I see this function as being somewhat complex and unreadable so I'd like to find a good way to design a better solution.
My question is, is there some form of pattern/principle/best practice I should follow when performing these types of operations? I seem to find myself doing these a lot even though each "transformer" function does some output. The thing is this "transformer" function has an awkward place to live. Does it make sense for me to create a class called "CustomerToAgeMapTransformer" and break up my functions into smaller functions inside CustomerToAgeMapTransformer? This class also seems poorly named and I feel I am heading down a bad road here. Any direction would be really greatly appreciated!