- Published on
- View count
- 7 views
Don't add unneeded context
- Authors
- Name
- Francisco Moretti
- @franmoretti_
Usage
๐ Guideline
Don't add unneeded context: Avoid repeating information that is already provided by the surrounding code context.
Avoid using redundant words or phrases in variable or method names that duplicate information already conveyed by the context in which they are used. Trust the naming conventions and let the surrounding code provide the necessary context.
๐ ๏ธ How to Apply
- Avoid repetition: If the class or object name already conveys the context, there's no need to repeat it in the variable name. ๐
- Use descriptive names: Instead of adding redundant context, use names that describe the specific purpose or behavior of the variable. ๐
- Consider the scope: When naming variables, take into account the scope of the code to ensure clarity and avoid confusion. ๐ฏ
Pros and Cons
๐ Pros
- Improved readability: Removing redundant context from variable names makes the code easier to read and understand. ๐๏ธ
- Concise code: Eliminating unnecessary repetition in variable names leads to more compact and cleaner code. ๐งน
- Reduces cognitive load: Clear and concise variable names decrease the cognitive load on developers, making it easier to comprehend and work with the codebase. โก
๐ Cons
- Loss of clarity: If variable names lack context due to the omission of redundant information, it may become harder to understand their purpose. ๐ค
- Potential ambiguity: Overly generic or abbreviated variable names may introduce ambiguity, especially when used in different contexts. โ
Examples
โ Bad
โ Good
References
๐ Related principles
- Avoid redundant comments: Don't add unnecessary context through comments, as it can duplicate information already present in the code. ๐ฌ
- Single Responsibility Principle: By adhering to the single responsibility principle, you can avoid adding unneeded context and keep classes and methods focused on their core purpose. ๐ฏ
- Consistency in naming: Maintaining consistency in naming conventions helps to avoid unnecessary context and promotes clarity and understanding in the codebase. ๐
- Don't Repeat Yourself (DRY): Duplicating code or logic can introduce redundant context, so follow the DRY principle to eliminate unnecessary repetition. ๐ฑ