A really scary (Java anti-pattern) story

Hello, this is Shrimpman of accounting Saas Japan.

When working in the field of Java applications, the language may be dead enough I think it's not uncommon to maintain legacy Java applications. Then, mysterious code by Java beginners at that time, which is difficult for modern programmers to understand, You'll often come across dangerous code that creates fluff and bugs.

This time, I would like to announce the ones that left a particularly strong impression in the ranking format. It really happened (although most of the cases are outside of our company).

No.10 public static variable

A public static variable is like a global variable in the VM, isn't it? Did you want to get the Connection from different places? It is difficult because it was declared as public static Connection. Of course, when you get a connection, it's already closed.

No.9 private static (not final) logger

Loggers with private static (without the final modifier) declaration were used here and there. This is not a problem in itself, but if this class is the target class of Serialize, Of course Logger couldn't be serialized, so an exception was thrown. It's irresistible because it was flooded here and there.

private static Logger log = Logger.getLogger(this.getClass());

Override No.8 finalize

The finalize method of the Object class no longer has a reference to that Object It is executed when it is collected as a target for destruction by the garbage collector. I was in trouble because I was writing the process by overriding the finalize method. It was supposed that the process would be executed or not, and we wouldn't know when it would be called.

No.7 Create a layered structure and check for nulls

SomeValue value = null;
if (someClass != null
        && someClass.getValueA() != null
        && someClass.getValueA().getValueB() != null
        && someClass.getValueA().getValueB().getValueC() != null
        && someClass.getValueA().getValueB().getValueC().getValueD() != null
        && someClass.getValueA().getValueB().getValueC().getValueD().getValueE() != null
        && someClass.getValueA().getValueB().getValueC().getValueD().getValueE().getValueF() != null
        && someClass.getValueA().getValueB().getValueC().getValueD().getValueE().getValueF().getValueG() != null) {
    value  = someClass.getValueA().getValueB().getValueC().getValueD().getValueE().getValueF().getValueG();


No.6 Mysterious side effect class that is not a singleton

It used to be common in the past, such a class. It's almost gone now.

public class SomeClass {
    private Result result;
    public void init() {
        //Something like reading the master
    public void calculate() {
        //Calculation process
        result = new Result(xxx);
    public Resut getResult() {
        return result;

No.5 Throw all exceptions

Well, I don't see this, but it happens occasionally.

public int extractSomethingUtilityMethod throws IOException, IllegalStateException, NumberFormatException, SomeException.... {
  //Something processing

Many kinds of exceptions have been declared to occur ... It's better than being crushed, but I don't want to call such a method ...

No.4 Nonsense reuse of variables (variable scope is uselessly wide)

SomeElement element = null;
if (valueA != null) {
    someElement = new Element(valueA)
if (valueB != null) {
    someElement = new Element(valueB)
if (valueC != null) {
    someElement = new Element(valueC)

It feels uncomfortable ... This alone doesn't seem to have any adverse effects, It's a little chilling to the mysterious scope.

No.3 Elimination of exceptions

Code that sometimes goes crazy

try {
    //Various processing
} catch (Exception e) {

I'm catching an exception, but ... ?? Don't spit logs! ?? I mean ...

Leave by label from No.2 for loop

Now that Java 8 is the center, there are few appearance scenes for loops, but It was conveniently used everywhere as an efficient looping process that wouldn't get stuck in legacy programs. However, because the for loop is used too conveniently, the for loop is nested in double and triple, and it is possible to leave the loop. A wise man who uses labels has appeared.

loopA: for (int i = 0; i < arrayA.length; ++i) {
    for (int j = 0; j < arrayB.length; ++i) {
        //Something processing
        if (xxx) {
            break loopA;

This is just the beginning, but if you multiplex this, you never know when and how far you got out of the loop.

No.1 else if cannot be used HARD mode that imposes coding rules (self?)

I'd like to say as the title says, but it's a little hard to imagine, so I'll write a simple example below.

if (bigDecimalValue.compareTo(BigDecimal.valueOf(100000L) < 0)) {
} else {
    if (bidDecimalValue.compareTo(BigDecimal.valueOf(200000L) < 0) {
    } else {
        if (bigDecimalValue.compareTo(BigDecimal.valueOf(300000L) < 0 ) {
        } else {
This continues endlessly and disappears to the far right of the screen ...

Those who see this for the first time do not know what this process is doing, but conditional branching I think I'm crazy if I got a job at a company that had to code without else if. By the way, this if statement that I encountered was nested more and more and disappeared to the far right of the screen.

In addition, ASaaS has increased the number of technical lead engineers in the last few years and is also focusing on younger education, so We have a system in place to manufacture high quality products. Creating and maintaining a product that all customers who use ASaaS can use with peace of mind for a long time Please be assured that it is in place.

Recommended Posts

A really scary (Java anti-pattern) story
[Java] Really scary switch statement story
Java static story
Really scary ClassCastException
Java initializer story
Java generic story
A story about Java 11 support for Web services
A story about the JDK in the Java 11 era
A story about trying to operate JAVA File
[Java] Create a filter
java build a triangle
Lombok's Java 9+ support story
A story about developing ROS called rosjava with java
Story of making a task management application with swing, java
[Java] A story about IntelliJ IDEA teaching Map's putIfAbsent method
A story about misunderstanding how to use java scanner (memo)
A note about Java GC
[Java] Aizu Online Judge's story 2
[Java] Aizu Online Judge's story 1
Create a java method [Memo] [java11]
[Java] Create a temporary file
Find a subset in Java
What is a Java collection?
[Java] Make it a constant
[Java] Draw a simple pattern
Java creates a Word document
C # and Java Overrides Story
Make a rhombus using Java
A story stuck with NotSerializableException
A story that I struggled to challenge a competition professional with Java
[Note] A story about changing Java build tools with VS Code
A story about hitting the League Of Legends API with JAVA
A story about having a hard time aligning a testing framework with Java 6
A story that I finally understood Java for statement as a non-engineer