Key takeaways:
- Avoid single-letter variable names and abbreviations for clearer code.
- Do not include types in variable names; rely on static typing for clarity.
- Use meaningful units in variable names and leverage language-specific types to abstract units.
- Avoid redundant naming patterns, such as prefixing interfaces with 'I' in C#.
- Refactor class hierarchies for clear naming, avoiding 'base' or 'abstract' in class names.
- Replace generic 'utils' or 'helper' classes with more descriptive module names or integrate functions into relevant classes.
# Introduction to Naming Conventions
- Naming is a fundamental aspect of writing readable and maintainable code.
- Bad naming practices can lead to confusion and errors, while good practices can improve code clarity.
# Avoid Single-Letter Variable Names
- Single-letter names lack context and make code difficult to understand.
- Opt for full words or phrases that describe the variable's purpose.
# Shun Abbreviations
- Abbreviations can be ambiguous and rely on context that may not be present.
- With modern IDEs and screen resolutions, the benefits of abbreviations are negligible.
# Do Not Include Types in Variable Names
- Hungarian notation is outdated, especially with modern statically typed languages.
- The variable's type itself should indicate what it represents.
# Use Units in Variable Names When Necessary
- Include units in variable names to clarify the expected input, e.g., .
- Ideally, use language-specific types that abstract units, like in C# or in C++.
# Avoid Redundant Prefixes for Interfaces
- Prefixing interfaces with 'I' in C# is unnecessary and can be confusing.
- Focus on the functionality the interface provides rather than its implementation detail.
# Refactor Class Hierarchies for Clearer Naming
- Avoid naming parent classes with 'base' or 'abstract'.
- Rename child classes to be more specific, and consider renaming parent classes to reflect their broader role.
# Eliminate 'Utils' and 'Helper' Classes
- Instead of generic 'utils' or 'helper' modules, integrate functions into relevant classes or create descriptive modules.
- Utils classes often become a dumping ground for unrelated functions.
# Conclusion
- Good naming practices lead to code that is self-documenting and easier to maintain.
- Refactoring code to follow these practices can greatly improve the readability and usability of the codebase.
By following these best practices, developers can ensure that their code communicates its intent clearly, making it easier for others (and themselves) to understand and modify in the future. Remember, the goal is to write code that is as clear as possible, reducing the cognitive load required to work with it.
Summary for: Youtube