Type changes should never force pattern changes

posted on 2014-06-25

Java has a few things that make it a very ugly language. I'm not going into the whole object oriented part here, but I'm going to dive deeper into what I think should be in the lemma of beautiful languages: type changes should never force pattern changes.

Example where you can feel the unbalanced ugliness of Java is when you are using void as a return type:

public class CommandPerformer implements Command {
    public int justPerformAnAction();
}

public class CommandWrapper implements Command {
    private final Command delegate;
    public CommandWrapper(Command toWrap) {
        this.delegate = toWrap;
    }
    public int justPerformAnAction() {
        System.out.println("Wrapping");
        return delegate.justPerformAnAction();
    }
}

Now if you replace the int return type of the Command to void, you are forced to remove the return in the wrapping class.

The same problem holds true for the primitive types, which can't be used in any of the generics you can create. This means there is no way to make a generic version of CommandWrapper where the return type of justPerformAnAction is a generic type.

One solution is to avoid void and all Java primitive types (int, float, etc.), but that's almost not done. The best solution is probably just to skip Java and head over to Haskell, Clojure or Scala.