Smell Code 285 – How to Fix Unnecessary Functions
Be sure!!
TL;DR: Functions with unclear names hide intent and confuse readers. Use descriptive and action-oriented names.
problems
- Functional purpose is unclear
- Increased cognitive load
- Misleading context
- Decreased readability
- Difficult cooperation
- Hidden functions
Solutions
- Use action-oriented verbs
- Make nouns descriptive
- Reflect the purpose of the job
- Avoid general terms
- Provide meaningful context
- Clearly express individual responsibility
- Matching the action with the result
Reconstruction
Context
Functions named in layman’s terms force readers to dive into the implementation to understand their behavior.
This wastes time and increases the chance of errors.
Naming becomes even more important when working with standalone functions, where the class name does not provide additional context.
This issue relates directly to Tell, don’t ask principle.
Instead of revealing ambiguous behaviors that force the caller to infer function, imperative names convey the exact action, directing the reader without having to examine the code.
When you name jobs descriptively, you eliminate unnecessary guesswork and conform to this principle.
Sample code
mistake
public String dateFormatting(Date date) {
return new SimpleDateFormat("yyyy-MM-dd").format(date);
}
public void load() {
System.out.println("Loading...");
}
right
public String formatDate(Date date) {
return new SimpleDateFormat("yyyy-MM-dd").format(date);
}
public void loadUserPreferences() {
System.out.println("Loading user preferences...");
}
a statement
You can detect this smell by reviewing function names that use ambiguous terms such as He does, Being, practical, loadetc.
Automated scanners can flag these patterns or label functions with very general names.
level
Why is the objection important?
Job titles should create a clear fit between their name and their function.
Breaking this denial forces developers to examine code details for context, slowing down the process of debugging, revisions, and additions.
Artificial intelligence generation
AI tools sometimes generate generic function names without understanding your domain.
When using AI, specify that job titles should be descriptive and action-oriented.
Artificial intelligence detection
AI models can help detect ambiguous names by comparing functional signatures with previously defined naming best practices.
Combining AI with manual code review yields the best results.
Try them!
Remember: AI assistants make a lot of mistakes
conclusion
Function names are not just labels; They are contracts with the reader.
Ambiguous names break this contract and lead to confusion.
Descriptive, action-oriented names simplify communication and make your code easier to maintain and extend.
Relationships
See also
Disclaimer
Code smells are my opinion.
Credits
Image from the British Library on Unsplash
The function name must be a verb or verbal phrase, and it must be meaningful
Robert C. Martin
This article is part of the CodeSmell series.