Skip to main content

Posts

Showing posts from June 28, 2021

Batch processing with Iterator pattern

 Greetings! While peeping on a pull request I came across this 60+ lines long complex looking code. Intention of the code was to fetch data from the legacy database as batches. Source code -  Iterator Pattern   In brief, idea was like this, while there_are_more_records fetch next batch do any processing handle exceptions calculate next batches calculate is there another iteration Though lines count has been reduced by creating private methods later, this has multpiple problems. Doing multiple things in this method. Looks too complex. Difficult to unit tests. Unit tests will not be that much valid. Hard to maintain. Error prone. Cannot resuse the logics. Unfortunately, people think doing this kind of complex codes are the good work. A better code will treated like an easy work. Anyway, we can do better with an Iterator. As Java's collection framework uses this heavily, most of the time, people don't use this pattern as they don't know they can use Ite