Skip to content
Yegor Bugayenko edited this page Sep 11, 2013 · 32 revisions

Composite Variable Names

Avoid composite names of variables, like timeOfDay, firstItem, or httpRequest. I mean both class variables and in-method ones. A variable name should be long enough to avoid ambiguity in its scope of visibility, but not longer. A name should be a noun in singular or plural form, or its abbreviation, for example:

final List<String> names;
void sendThroughProxy(File file, Protocol proto);
private File content;
public HttpRequest request;

Sometimes you may have collisions between constructor parameters and in-class properties, when your constructor is saving incoming data into an instantiated object. In this case I recommend to use abbreviate by removing vowels, for example

public final class Message {
  private String recipient;
  public Message(final String rcpt) {
    this.recipient = rcpt;
  }
}

Time Constants

Don't calculate time with constants, like:

final long millis = min * 60 * 1000;

Instead, use TimeUnit:

final long millis = TimeUnit.MINUTES.toMillis(2);
final long sec = TimeUnit.HOURS.toMinutes(1);

Lombok

Don't forget to use @ToString and @EqualsAndHashCode annotations from Lombok. They add toString() and equals() and hashCode() methods to your class during compilation. Why it's important? Because without them you will either 1) have default methods that don't do any good, or 2) will hav to create your own in every class, which is ineffective.

But don't forget to specify a list of private properties for @EqualsAndHashCode, for example:

@ToString
@EqualsAndHashCode(of = { "name", "age" })
public final class User {
  private final transient String name;
  private final transient long age;
}
Clone this wiki locally