Atom Feed SITE FEED   ADD TO GOOGLE READER

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.
Forgot to mention a favourite source of inspiration - Wil Shipley's Presentation:
Less code is better code
I like to use this rule to arbitrate between alternative designs.
* I feel mostly the same way.

* You didn't really mean to remove the entire javadoc block altogether, did you?

* I also tend to avoid named classes until I'm forced into it. Sometimes people feel forced into it just because they need to implement two interfaces at the same time, but it's really easy to define an ad-hoc private interface that extends both of those, and still have your anonymous inner class goodness.

* But other things really do force your hand. For example, a well-behaved comparator should have equals() and hashCode(). But ever tried implementing equals() in an anonymous class? Whoops.

* Also, trying too hard to avoid named classes can lead to things nesting way out of control...

* I also dislike 'public' on interface methods, and go further than that -- I also mercilessly chop out all access specifiers on members of private classes (except when forced by an overridden/implemented method to have them). Members of a private class always have the exact same visibility no matter what you stick on them!
You're right about equals and hashCode - could self types help here?