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).
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.
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());
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.
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();
}
Wow····.
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;
}
}
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 ...
SomeElement element = null;
if (valueA != null) {
someElement = new Element(valueA)
someElement.setParent(parentElement);
}
if (valueB != null) {
someElement = new Element(valueB)
someElement.setParent(parentElement);
}
if (valueC != null) {
someElement = new Element(valueC)
someElement.setParent(parentElement);
}
It feels uncomfortable ... This alone doesn't seem to have any adverse effects, It's a little chilling to the mysterious scope.
Code that sometimes goes crazy
try {
//Various processing
} catch (Exception e) {
}
I'm catching an exception, but ... ?? Don't spit logs! ?? I mean ...
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.
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)) {
//processing
} else {
if (bidDecimalValue.compareTo(BigDecimal.valueOf(200000L) < 0) {
//processing
} else {
if (bigDecimalValue.compareTo(BigDecimal.valueOf(300000L) < 0 ) {
//processing
} 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