Video in progress

What rules do you follow when choosing a good name (for a class, method or variable)?

A name should tell why it exists, what it does and how it is used, without a comment. Classes are nouns (InvoiceRepository), methods are verbs (calculateFee), booleans are phrased as questions (isActive, hasDiscount). Avoid abbreviations and single-letter names.

📖 Answer

Before and after:

// before
int d;                          // what? days? distance?
List<int[]> list1;
void proc(User u) { ... }

// after
int daysSinceLastLogin;
List<int[]> seatCoordinates;
void sendWelcomeEmail(User user) { ... }

Useful rules:

  • Use the domain language. If the business says "contract", the code should say Contract, not Agreement. One name per concept.
  • Don't repeat the type name: users, not userList; String nameString is redundant.
  • Match the length to the scope: u is fine inside a 3-line lambda, but not for a class field.
  • A name must not lie: if getUser() writes to the database or sends an email, its name is deceiving.
  • Be wary of empty words like Manager, Data, Info, Helper; they hide a vague responsibility.

If you feel a method needs a comment, first try improving its name. A good name often makes the comment unnecessary.