Skip to main content


Showing posts from June, 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

Why you should not use field injection

 Greetings! While doing code reviews, I usually come across DI framework (ex: Spring) managed beans with no constructor. I myself have done this many times (and still for POCs). What you can see in these classes is, fields are annotated with @Autowired, @Resource, @Inject, etc. How do they write unit tests then? They will for example use @InjectMocks using mock framework. As I am not a fan of this approach I ask the reasons why are you creating classes like this and use constructors. However, many will not agree with me giving these kinds of reasons. Reasons to use field injection We are using a DI framework (Spring), do not need to worry about this. The unit testing framework we use support field mocking (InjectMocks). We can use reflection in unit tests. Different developers have different practices, you need to accept that fact and respect them. This is how the previous developer coded. This is how our internal framework use. I'm senior than you, do as I say :P. While those can