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

  • 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. 🚱