CodeSOD: On the Creation
Understanding the Gang of Four design patterns is a valuable bit of knowledge for a programmer. Of course, instead of understanding them, it sometimes seems like most design pattern fans just... use them. Sometimes- often- overuse them. The Java Spring Framework infamously has classes with names like SimpleBeanFactoryAwareAspectInstanceFactory. Whether that's a proper use of patterns and naming conventions is a choice I leave to the reader, but boy do I hate looking at it.
The GoF patterns break down into four major categories: Behavioral, Structural, Concurrency, and Creational patterns. The Creational category, as the name implies, is all about code which can be used to create instances of objects, like that Factory class above. It is a useful collection of patterns for writing reusable, testable, and modular code. Most Dependency Injection/Inversion of Control frameworks are really just applied creational patterns.
It also means that some people decide that "directly invoking constructors is considered harmful". And that's why Emiko found this Java code:
/** * Creates an empty {@link MessageCType}. * @return {@link MessageCType} */public static MessageCType createMessage() {MessageCType retVal = new MessageCType()return retVal;}
This is just a representitive method; the code was littered with piles of these. It'd be potentially forgiveable if they also used a fluent interface with method chaining to intialize the object, buuut... they don't. Literally, this ends up getting used like:
MessageCType msg = MessageCType.createMessage();msg.type = someMessageType;msg.body = ...
Emiko sums up:
[Advertisement] Continuously monitor your servers for configuration changes, and report when there's configuration drift. Get started with Otter today!At work, we apparently pride ourselves in using the most fancyful patterns available.