Pattern Antipattern
Occasionally as a programmer you run into some strange, strange code. The folks that wrote this are no longer on the team (or with the company for the most part) and you have to guess what was going through their head.The only thing I can guess is this is a pattern that someone wanted to check off their bucket list and this code was where that was going to happen.I ran into such a thing today.Here it is boiled down to the essence. (I took out comments, no code was omitted)
public class ActionPayload<T> {private T payload;public ActionPayload withPayload(final T data) {this.payload = data;return this;}public T getPayload() {return this.payload;}}
This was both the argument to a function and the return of the function.The first thing the function did was unwrap the contents by calling getPayload. The return looked like return new "ActionPayload().withPayload(foo);" Since this was in an implementation of an interface none of the T were defined.Take a look at that again.If you have casts coming and going... isn't this the same as passing in the fucking Object???The absurdity of this pattern continued for a few more layers as well, but I'm not going into that right now.The takeaway is that indirection simply for the sake of indirection does you no good. In fact it's actively bad. Every time you use indirection there's a bit of brain work that needs to happen. If what you get from the indirection offsets the extra effort (most times honestly) then please use it.But when you're hiding the fact that you're passing an Object back and forth with this linguistic trickery, not only are you adding the indirection with no gain, you're hiding the fact that you're unsafely passing the Object back and forth.Two wrongs, in this case, do not make a right.If you read a book or web page or whatever about patterns please understand that this is to come up with a common set of nouns to call things. It's not a todo list. It's not a prescription for how to code. It's simply a single example of how something might be coded.