This content originally appeared on HackerNoon and was authored by Maximiliano Contieri
Be Imperative!!
TL;DR: Functions with unclear names hide intent and confuse readers. Use descriptive, action-oriented names.
Problems
- Unclear function purpose
- Increased cognitive load
- Misleading context
- Reduced readability
- Difficult collaboration
- Hidden functionality
Solutions
- Use action-oriented verbs
- Make names descriptive
- Reflect the function’s purpose
- Avoid generic terms
- Provide meaningful context
- Express single responsibility clearly
- Match action to outcome
Refactorings
https://hackernoon.com/improving-the-code-one-line-at-a-time
Context
Functions named with generic 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 critical when working with standalone functions, where the class name doesn't provide additional context.
\ This issue directly relates to the Tell, Don’t Ask principle.
\ Instead of exposing ambiguous behaviors that force the caller to infer functionality, imperative names convey the exact action, guiding the reader without needing to inspect the code.
\ When you name functions descriptively, you eliminate unnecessary guesswork and align with this principle.
Sample Code
Wrong
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...");
}
Detection
- [x] Manual
You can detect this smell by reviewing function names that use vague terms like do, run, process, load, etc.
\ Automated linters can flag these patterns or highlight functions with overly generic names.
Tags
- Naming
Level
- [x] Beginner
Why the Bijection Is Important
Function names should create a clear one-to-one correspondence) between their name and functionality.
\ Breaking this Bijection forces developers to examine code details for context, slowing down debugging, reviews, and extensions.
AI Generation
AI tools sometimes generate generic function names without understanding your domain.
\ When using AI, specify that function names must be descriptive and action-oriented.
AI Detection
AI models can help detect ambiguous names by comparing function signatures with predefined naming best practices.
\ Combining AI with manual code review yields the best results.
Try Them!
Remember: AI Assistants make lots of mistakes
| Without Proper Instructions | With Specific Instructions | |----|----| | ChatGPT | ChatGPT | | Claude | Claude | | Perplexity | Perplexity | | Copilot | Copilot | | Gemini | Gemini |
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.
Relations
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-vii-8dk31x0?embedable=true
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxxi?embedable=true
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-viii-8mn3352?embedable=true
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxxv?embedable=true
See also
https://hackernoon.com/what-exactly-is-a-name-the-quest-part-i-fmw3udc?embedable=true
https://hackernoon.com/what-exactly-is-a-name-rehab-part-ii-4st3uph?embedable=true
Disclaimer
Code Smells are my opinion.
Credits
Photo by britishlibrary on Unsplash
A function name should be a verb or a verb phrase, and it needs to be meaningful
Robert C. Martin
https://hackernoon.com/400-thought-provoking-software-engineering-quotes?embedable=true
This article is part of the CodeSmell Series.
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-i-xqz3evd?embedable=true
\
This content originally appeared on HackerNoon and was authored by Maximiliano Contieri
Maximiliano Contieri | Sciencx (2025-01-11T20:00:04+00:00) Code Smell 285 – How to Fix Non-Imperative Functions. Retrieved from https://www.scien.cx/2025/01/11/code-smell-285-how-to-fix-non-imperative-functions/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.