Don't add unneeded context
Learn how to write clear and concise code by avoiding unnecessary repetition of context in variable names. Enhance code readability and maintainability.
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
// Bad variable names with unnecessary context
class Car {
private carModel: string;
private carMake: string;
constructor(carModel: string, carMake: string) {
this.carModel = carModel;
this.carMake = carMake;
}
public getCarModel(): string {
return this.carModel;
}
}
β Good
// Good variable names without redundant context
class Car {
private model: string;
private make: string;
constructor(model: string, make: string) {
this.model = model;
this.make = make;
}
public getModel(): string {
return this.model;
}
}
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. π±