Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Worst Java practice found in your experience? [closed]

Tags:

java

People also ask

What is the biggest problem with Java?

Carmack concluded in 2005 about Java on cell-phones: "The biggest problem is that Java is really slow. On a pure cpu / memory / display / communications level, most modern cell phones should be considerably better gaming platforms than a Game Boy Advance.


I had to maintain java code, where most of the Exception handling was like:

catch( Exception e ) {}

Once I encountered 'singleton' exception:

class Singletons {
    public static final MyException myException = new MyException();
}

class Test {
    public void doSomething() throws MyException {
        throw Singletons.myException;
    }
}

Same instance of exception was thrown each time ... with exact same stacktrace, which had nothing to do with real code flow :-(


Six really bad examples;

  • Instead of error reporting, just System.exit without warning. e.g.
    if(properties.size()>10000) System.exit(0); buried deep in a library.
  • Using string constants as locks. e.g. synchronized("one") { }.
  • Locking on a mutable field. e.g. synchronized(object) { object = ...; }.
  • Initializing static fields in the constructor.
  • Triggering an exception just to get a stack trace. e.g. try { Integer i = null; i.intValue(); } catch(NullPointerException e) { e.printStackTrace(); }.
  • Pointless object creation e.g. new Integer(text).intValue() or worse new Integer(0).getClass()

if{
 if{
  if{
   if{
    if{
     if{
      if{
       if{
         ....

I hate it when people create interfaces just for hanging a set of constants on:

public interface InterfaceAntiPattern {
  boolean BAD_IDEA = true;
  int THIS_SUCKS = 1;
}

—Interfaces are for specifying behavioural contracts, not a convenience mechanism for including constants.


Not related strictly to Java, but calling an expensive function over and over instead of storing the result, when you know it won't change. Example:

if (expensiveFunction() > aVar)
    aVar = expensiveFunction();
for (int i=0; i < expensiveFunction(); ++i)
    System.out.println(expensiveFunction());

The worst Java practice that encompasses almost all others: Global mutable state.