PUBLIC OBJECT

Spammy Java

Compare these two code snippets, they both do the same thing.

Minimalist

public static <T> Comparator<T> reverse(final Comparator<T> forward) {
  Preconditions.checkNotNull(forward, "forward");
  return new Comparator<T>() {
    public int compare(T a, T b) {
      return forward.compare(b, a);
    }
  };
}

Complete

/**
 * Returns a comparator that's the reverse of
 * {@code forward}.
 *
 * @param forward the forward comparator
 * @return the reversed comparator
 * @throws NullPointerException if {@code forward} is null
 */
public static <T> Comparator<T> reverse(final Comparator<T> forward) {
  return new ReverseComparator<T>(forward);
}

/**
 * A comparator that reverses another comparator.
 *
 * @param <T> the type to compare
 */
private static class ReverseComparator<T> implements Comparator<T> {
  private final Comparator<T> forward;

  public ReverseComparator(final Comparator<T> forward) {
    super();
    Preconditions.checkNotNull(forward, "forward");
    this.forward = forward;
  }

  @Override
  public int compare(final T a, final T b) {
    return forward.compare(b, a);
  }
}

Clearly, some people's spamis other people's ham. For your entertainment, here's my views:

Redundant Javadoc: spam. It decreases the signal-to-noise ratio of your docs, and tends to grow out-of-date. I've ranted about this before.

@throws NullPointerException Javadoc: okay. But I'd prefer to just have package-level or project doc that says "all parameters and return values will not be null unless otherwise specified".

'final' on parameters: spam. I almost never reassign parameter values, so the keyword doesn't say much.

'final' on fields: ham. Immutability is great, and I like to be explicit about it.

Named classes instead of anonymous ones: spam. Named classes require extra code for fields and a constructor. What a waste of code!

@Override: When I override a non-abstract method, I really want to make sure the name doesn't change - ham. But for abstract methods and interface methods, it doesn't pull it's weight for me - spam.

super() with 0 args: spam. Especially when the superclass doesn't have another constructor that takes parameters.

'public' on interface methods: okay, but I don't use it. The problem here is that 'public' implies that some other modifier could be appropriate here.

Disagree? Tell me why in the comments.