Hello everyone, this is my first post here, yay!!! Wellcome me!
I’ve taken up functional programming recently, and many of the practices there have stuck with me and I like to use them in my Ruby code. A practice I especially am fond of is creating service objects, that is having a class that implements the #call
method then creating instances of it and calling them to run my “function”.
This practice is very popular in the dryrb and hanami community, and also popularized by Arkency.
Lately I’ve been working on a problem where we calculate some score based of metrics for thousands of rows, while sorting them. Given I would use service objects in this calculation for each metric I would be creating many of thousands of objects just to do simple calculations, which would later have to be garbage collected.
The service object design makes much sence to organizing the code, as I want each metric to be independent, and would rather avoid creating master object for each metric and use it’s method call with the different data each time, even though in retrospect it does not look so bad.
Most importanly I’d like to know for educational reasons how costly it actually is to create these sort of service objects, and when would it make sence to reconsider using them, even though one feels they would make a better code design decision.