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);
Clone this wiki locally