Let's say I have an processing pipeline. It receives a file, converts the file to data, interprets the data, and then persists it.
At every step of this way, I would need to collect statistics, so I can show a comprehensive "Import Report" at the end. Retrieving some of this data is not possible once the algorithm finished, and has to be collected while it is running. Implementing this cleanly shows to be very cubersome, though, because I end up with implementing this cross-cutting concern still in the algorithm itself.
A few approaches:
1: Create an statistical object. Pass it as parameter to every sub-method of the pipeline that has use for it. Fill it appropriatly there. While it keeps the pipeline stateless, it feels super yucky.
2: Create an statistical object. Save it as an private variable inside of the algorithm object itself. The object is now stateful and appropriate care has to be taken in terms of thread safety. A pain in the ass if the algorithm ever needs to be split into multiple objects down the road. But it keeps methods cleaner, I guess...?
3: Instead of passing a statistical object to a method, have it return the result as well as a statistical response object than can then be accessed by the caller. Not quite as yucky, but this just really moves the problem from one place to another, namely the caller.
4: Implement publishing events inside of the algorithm that spits out necessary events when something noteworthy happens inside of it. Decouples the statistic collection from the algorithm itself, still imposes this use case in the structure of the algorithm, seems incredibly overkill for my use case, and brittle to future changes.
Honestly, all of these approaches are incredibly ugly to me. Right now I'm using approach 1. But I'd like to use a cleaner version in the future.