Write Clear, Concise Variable and Method Names: Practical Naming Rules
This article shares practical guidelines for naming variables, methods, and classes—showing why overly long identifiers hurt readability, how to eliminate redundant words, and offering concrete before‑and‑after code examples to help developers adopt concise, expressive names.
Naming Goals
Good names should be clear —you must know what the identifier refers to—and precise —you must know what it does not refer to. Any characters beyond these two goals are unnecessary.
General Principles
Avoid including the variable or parameter type in the name; the type is already evident from the code context.
For collections, use the plural form to describe the contents (e.g., holidays instead of holidayDateList).
Method names need not repeat the class name or the parameter types; the signature already conveys that information.
Prefer short names when the scope is limited; the smaller the scope, the shorter the name can be.
Remove words that add no meaning (e.g., generic terms like data, manager, object).
Bad vs. Good Examples
// Bad variable names
String nameString;
DockableModelessWindow dockableModelessWindow;
// Improved
String name;
DockableModelessWindow window;
// Bad collection names
List holidayDateList;
Map employeeRoleHashMap;
// Improved
List holidays;
Map employeeRoles;
// Bad method names
mergeTableCells(List cells)
sortEventsUsingComparator(List events, Comparator comparator)
// Improved
merge(List cells)
sort(List events, Comparator comparator)
// Bad class and method names
class AnnualHolidaySale {
int _annualSaleRebate;
void promoteHolidaySale() { … }
}
// Better
class AnnualHolidaySale {
int _rebate;
void promote() { … }
}
// Overly verbose example
class DeliciousBelgianWaffleObject {
void garnishDeliciousBelgianWaffleWithStrawberryList(List strawberryList) { … }
}
// Simplified
class Waffle {
void garnish(List strawberries) { … }
}Applying the Rules
When you encounter a name like recentlyUpdatedAnnualSalesBid, ask whether each word adds distinct meaning. If removing any word does not change the understanding, that word is redundant and should be omitted.
Remember that naming is a balance: overly terse names can be ambiguous, but starting with a concise name makes it easier to refine later if conflicts arise.
Conclusion
Adopting concise, expressive naming improves code readability, maintainability, and reduces cognitive load during code reviews. The guidelines above provide a concrete checklist for cleaning up existing code and preventing future naming bloat.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
