Function names should say what they do
Say what the function does with its name. The name should tell you what it expects as inputs as well.
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
// Bad: Function name does not convey its purpose or expected inputs
function addToDate(date, month) {
// ...
}
const date = new Date()
// Usage
// It's hard to tell from the function name what is added
addToDate(date, 1)
β Good
// Good: Function name clearly describes the purpose and expected inputs
function addMonthToDate(month, date) {
// ...
}
// Usage
const date = new Date()
addMonthToDate(1, date)
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. π