From "The Pragmatic Programmer"
🎧 Listen to Summary
Free 10-min PreviewNaming Things
Key Insight
Naming in programming is profoundly important, as names chosen for applications, modules, functions, and variables reveal intent and belief. The process of naming forces a developer to pause and consider the role of what is being created, fostering a deeper understanding of its purpose, capabilities, and interactions. This clarification process often exposes flawed assumptions or designs, leading to better code. The brain processes words extremely fast (demonstrated by the Stroop effect), giving names a priority in how humans interpret code, making well-chosen names crucial for immediate comprehension.
Effective naming involves clarity and precision. For instance, using 'customer' or 'buyer' instead of a generic 'user' for an authenticated individual provides constant contextual reminders of their role and actions. Similarly, a method named 'applyDiscount' with a 'Percentage' type parameter is superior to 'deductPercent' with a 'double amount', as it explicitly communicates intent and expected input range. When a module performs specific actions, like calculating Fibonacci numbers, naming functions with clear, context-aware terms such as 'Fib.of(n)' or 'Fib.nth(n)' makes their purpose immediately obvious, rather than a generic 'Fib.fib(n)'. While striving for clarity, it's also important to honor the cultural naming conventions of a specific programming language or environment, such as 'i, j, k' for loop counters in C, or 'camelCase' versus 'snake_case'.
Consistency in naming is vital within a project, establishing a shared vocabulary or 'jargon' with special meaning to the team. This can be fostered through constant communication (e.g., pair programming) or a project glossary. As code evolves, names can become misleading, leading to a 'software entropy' problem if not addressed. Therefore, it's essential to proactively rename things when their meaning or intent shifts, treating renaming as an ongoing activity. Modern IDEs often provide automatic refactoring tools to make renaming easier and safer, particularly when backed by comprehensive regression tests. If a name cannot be changed due to a deeper design flaw, that underlying issue must be fixed first, otherwise, misleading names will propagate confusion and misinformation to new team members.
📚 Continue Your Learning Journey — No Payment Required
Access the complete The Pragmatic Programmer summary with audio narration, key takeaways, and actionable insights from Andrew Hunt, David Thomas.