Today, I will write about a Map anti-pattern that I have sometimes observed.
I have come across code as follows from time to time:
1 2 3 4 5 |
Map<String, Object> json = new HashMap<>(); //bad don't do this!!! // or Map<String, Object> parameters = new HashMap<>(); //again don't do this |
I suggested to the developer who wrote similar code to use a class instead of a Map. I explained that a Map should be used for mapping problems, such as associating data with key value pairs.
The developer was upset, and argued back that using a class instead of a map is over-engineering. I interpreted this as some sort of logical fallacy “Ad Hominem” attack against classes. I exclaimed “Using a map is under-engineering! At the very least, we can introduce an interface that functions a marker class, and let Jackson perform the transformations?” This developer would not budge, and stated that she did not want to write a class for everything that will become serialized as JSON. I stepped back, gave her the benefit of the doubt, looked further at the code, noted the pervasiveness of this anti-pattern, and now I am writing this.