The Silent Villains of the Coding Universe: A Review of Common Java Anti-Patterns.
Tags: java, design, architecture, testing
Published: 2020-08-11 16:33:59

Anti-Patterns seem harmless but lead to error-prone solutions and make your code unmaintainable in the long-run. 

For those who aren't familiar with the term ‘anti-pattern,’ it may sound like frivolous jargon or a throwaway catchphrase, but for those in the know, it’s a little more sinister than that. 

First coined by famed American programmer and prolific author in the programming literature space – Andrew Koenig – anti-pattern is the antithesis of the popular and positive ‘design pattern,’ created by programmers as a reusable solution that can be deployed to solve recurrent problems. 

Today, anti-patterns represent a solution to a programming issue that seems to work when taken at face value but causes problems when executed. Simply put, anti-patterns are patterns that look sound but are counter-intuitive in practice—they result in unmaintainable and error-prone solutions. 

Usually, anti-patterns emerge over a period of time when new functionality is added incrementally without a focus on continuous refactoring. Maintenance of code by different developers over a period of time often compounds the problem.

Here are some examples of anti-patterns that tend to crop up in Java-based programming: 

God Class: A god class is a class of code that executes multiple functionalities that are typically unrelated to each other. This creates needless complexity in the methods being run and results in a class that becomes too critical to the overall functionality of the program. Because this one class affects so many others, you have to be exceedingly careful when it comes to refactoring this class. Since a god class doesn’t follow one of the most basic tenets of programming whereby a big problem is deconstructed into several smaller problems and then solved individually, it results in a code where any small change could have a domino effect on the entire system. A single class with too many functions, excessive amounts of data, and too many methods is a classic example of a god class. 

Global Breakable: Typically a structural component with an excess amount of outgoing dependencies is referred to as a global breakable. This type of anti-pattern is in constant need of maintenance as any change made to the components it is dependent on will result in a need for a change to this component as well. This is a sign of weak code and a system that is missing modularity.
 
Global Butterfly: On the opposite spectrum of the global breakable is the global butterfly. This structural pattern denotes an object that has a number of dependents that are much higher than is advisable. Whether these dependents come in the form of classes, inner classes, interfaces, or packages, all of these result in a global butterfly situation. The drawback of having a global butterfly as part of your code is that it becomes impossible to change this code outright, and even small alterations to this component become a delicate operation that could have a wider knock-on effect. 

Shotgun Surgery: If you have seen a class or method that has a disproportionately high number of incoming couplings, then that is a classic example of what is known as the shotgun surgery anti-pattern. A single change in this class will affect all the methods that call into it. This means that everything from testing this class to refactoring and maintaining it becomes needlessly complex. This class becomes critical as so many methods are dependent on it, even though it may not have as imperative a role to play in isolation. 

Feature Envy: Logically, it is prudent to package data together with the processes that require it. Feature Envy is an anti-pattern that works against this approach. In Feature Envy, the method is linked with a class outside of the package it is a part of. This results in a situation where the importance of a class suffering from Feature Envy is simply reduced to that of a Data Class. Or worse, the method in question may use data from both its own class and from an external class as well. In this case, its functionality becomes even more complex causing the need for extra care during refactoring. You need to put the data and the code that references it together in one package, without any external links to ensure that you avoid feature envy. 

Dispersed Coupling: If a class features methods that are reliant on operations that are spread across a number of different operations in various different classes, you have what is known as a dispersed coupling on your hands. Because of this, if a change is made in the dispersively coupled method, changes will also be required in all the classes it accesses. 

Global Hub: This is a commonly found component where you see a high number of global dependents attached to it, and the component in question also being globally dependent on an excessive amount of other components. Too many outgoing and incoming dependencies see the same component become both a global butterfly and a global breakable. This results in a situation where the code becomes too complicated to follow, and maintenance becomes a major issue as well. A global hub will also result in a situation where the reusability of a module is broken.   

Brain Method: As is indicated by its name, a brain method is the type of anti-pattern that carries out a majority of the functions in its class. Rather than these functions being distributed across several methods, having them included within a single brain method sees the requirement for too many conditional branches. The brain method is like a scaled-down version of a god class, and as such, suffers from similar issues. The code that makes up a brain method is too long and a little too complex, making it difficult to understand, and almost impossible to reuse. The presence of a deep nesting situation with the high number of conditional branches means that it needs to be tested more thoroughly than usual as well. Furthermore, the presence of multiple functions in one method will result in a situation where the method is reliant on a high number of temporary variables, making it more susceptible to errors. 

These are just a few examples of the many anti-patterns that can throw a wrench in your code, causing further delays as your DevOps team identifies and fixes the issues created as a result of their presence. Anti-patterns are classic examples of how not to do things, and since they often masquerade as working code, sniffing them out can be quite the challenge. You can’t really know you’re doing something wrong until your mistake is pointed out to you, right? This is where static code analysers can help. Advanced static code analysis tools can detect these with ease. Read more: https://docs.embold.io/anti-patterns/#what-are-anti-patterns

Generally, utilising a static code analysis tool will ensure that your programming resources are consumed for the right reasons. You can focus on development, innovation, expansion, and progress rather than on putting out fires, troubleshooting, going back and constantly re-checking code, and fixing all the issues that will crop up thanks to anti-patterns.