- Published on
- View count
- 9 views
Function names should say what they do
- Authors
- Name
- Francisco Moretti
- @franmoretti_
Usage
π Guideline
Function names should say what they do: Name functions according to their purpose, indicating expected inputs.
Functions play a crucial role in clean code. One of the key aspects of writing clean code is ensuring that your function names clearly express their intended functionality. A function's name should provide a clear and concise description of what the function does, as well as indicate the expected inputs.
π οΈ How to Apply
- Name Clarity: Use clear and concise names that accurately describe the function's purpose. π·οΈ
- Action Verbs: Include action verbs that indicate what the function does. πββοΈ
- Relevant Details: Include relevant information in the function name, such as the expected inputs or the type of output. π
- Avoid Ambiguity: Avoid using generic or ambiguous names that can cause confusion. β
Pros and Cons
π Pros
- Improved Code Readability: Descriptive function names make it easier for developers to understand the purpose and behavior of the code. π
- Self-Documentation: Well-named functions act as self-documenting code, reducing the need for extensive comments and making the code easier to maintain. π
- Enhanced Collaboration: Clear function names facilitate effective communication among team members, leading to smoother collaboration and reduced misunderstandings. π€
π Cons
- Long Names: Sometimes, descriptive function names can become lengthy, impacting code aesthetics and requiring more effort to type. β°
- Naming Ambiguity: Choosing the right name may be challenging, as it requires a balance between being descriptive and concise. It is important to select a name that accurately represents the function's behavior. β
Examples
β Bad
β Good
References
π Related Principles
- Single Responsibility Principle: Functions with clear and descriptive names align with the principle of having functions focused on a single task. π―
- Use Self-Explanatory Variable Names: Choose variable names that are self-explanatory and reflect their purpose. π
- Naming Conventions: Follow established naming conventions within your programming language or framework. π