# Code Smell 285 - Non-Imperative Functions Names

> 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

1. Use action-oriented verbs
2. Make [names](https://maximilianocontieri.com/what-exactly-is-a-name-part-i-the-quest) descriptive
3. Reflect the function’s purpose
4. Avoid generic terms
5. Provide meaningful context
6. Express single responsibility clearly
7. Match action to outcome

# Refactorings

%[https://maximilianocontieri.com/refactoring-005-replace-comment-with-function-name]

# 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](https://maximilianocontieri.com/what-exactly-is-a-name-part-ii-rehab) 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

<!-- [Gist Url](https://gist.github.com/mcsee/2a6f11c6af157a4b5025c7e1ca12b8bc) -->

```javascript
public String dateFormatting(Date date) {
    return new SimpleDateFormat("yyyy-MM-dd").format(date);
}

public void load() {
    System.out.println("Loading...");
}
```

## Right

<!-- [Gist Url](https://gist.github.com/mcsee/2ea10469fbc891b3a2003e1c308cd3dd) -->

```javascript
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]((https://maximilianocontieri.com/what-is-wrong-with-software)) between their name and functionality. 

Breaking this [Bijection](https://maximilianocontieri.com/the-one-and-only-software-design-principle)  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](https://chat.openai.com/?q=Correct+and+explain+this+code%3A+%60%60%60javascript%0D%0Apublic+String+dateFormatting%28Date+date%29+%7B%0D%0A++++return+new+SimpleDateFormat%28%22yyyy-MM-dd%22%29.format%28date%29%3B%0D%0A%7D%0D%0A%0D%0Apublic+void+load%28%29+%7B%0D%0A++++System.out.println%28%22Loading...%22%29%3B%0D%0A%7D%0D%0A%60%60%60) | [ChatGPT](https://chat.openai.com/?q=Convert+it+to+more+declarative+names%3A+%60%60%60javascript%0D%0Apublic+String+dateFormatting%28Date+date%29+%7B%0D%0A++++return+new+SimpleDateFormat%28%22yyyy-MM-dd%22%29.format%28date%29%3B%0D%0A%7D%0D%0A%0D%0Apublic+void+load%28%29+%7B%0D%0A++++System.out.println%28%22Loading...%22%29%3B%0D%0A%7D%0D%0A%60%60%60) |
| [Claude](https://claude.ai/new?q=Correct+and+explain+this+code%3A+%60%60%60javascript%0D%0Apublic+String+dateFormatting%28Date+date%29+%7B%0D%0A++++return+new+SimpleDateFormat%28%22yyyy-MM-dd%22%29.format%28date%29%3B%0D%0A%7D%0D%0A%0D%0Apublic+void+load%28%29+%7B%0D%0A++++System.out.println%28%22Loading...%22%29%3B%0D%0A%7D%0D%0A%60%60%60) | [Claude](https://claude.ai/new?q=Convert+it+to+more+declarative+names%3A+%60%60%60javascript%0D%0Apublic+String+dateFormatting%28Date+date%29+%7B%0D%0A++++return+new+SimpleDateFormat%28%22yyyy-MM-dd%22%29.format%28date%29%3B%0D%0A%7D%0D%0A%0D%0Apublic+void+load%28%29+%7B%0D%0A++++System.out.println%28%22Loading...%22%29%3B%0D%0A%7D%0D%0A%60%60%60) |
| [Perplexity](https://perplexity.ai/?q=Correct+and+explain+this+code%3A+%60%60%60javascript%0D%0Apublic+String+dateFormatting%28Date+date%29+%7B%0D%0A++++return+new+SimpleDateFormat%28%22yyyy-MM-dd%22%29.format%28date%29%3B%0D%0A%7D%0D%0A%0D%0Apublic+void+load%28%29+%7B%0D%0A++++System.out.println%28%22Loading...%22%29%3B%0D%0A%7D%0D%0A%60%60%60) | [Perplexity](https://perplexity.ai/?q=Convert+it+to+more+declarative+names%3A+%60%60%60javascript%0D%0Apublic+String+dateFormatting%28Date+date%29+%7B%0D%0A++++return+new+SimpleDateFormat%28%22yyyy-MM-dd%22%29.format%28date%29%3B%0D%0A%7D%0D%0A%0D%0Apublic+void+load%28%29+%7B%0D%0A++++System.out.println%28%22Loading...%22%29%3B%0D%0A%7D%0D%0A%60%60%60) |
| [Copilot](https://www.bing.com/chat?showconv=1&sendquery=1&q=Correct+and+explain+this+code%3A+%60%60%60javascript%0D%0Apublic+String+dateFormatting%28Date+date%29+%7B%0D%0A++++return+new+SimpleDateFormat%28%22yyyy-MM-dd%22%29.format%28date%29%3B%0D%0A%7D%0D%0A%0D%0Apublic+void+load%28%29+%7B%0D%0A++++System.out.println%28%22Loading...%22%29%3B%0D%0A%7D%0D%0A%60%60%60) | [Copilot](https://www.bing.com/chat?showconv=1&sendquery=1&q=Convert+it+to+more+declarative+names%3A+%60%60%60javascript%0D%0Apublic+String+dateFormatting%28Date+date%29+%7B%0D%0A++++return+new+SimpleDateFormat%28%22yyyy-MM-dd%22%29.format%28date%29%3B%0D%0A%7D%0D%0A%0D%0Apublic+void+load%28%29+%7B%0D%0A++++System.out.println%28%22Loading...%22%29%3B%0D%0A%7D%0D%0A%60%60%60) |
| [Gemini](https://gemini.google.com/?q=Correct+and+explain+this+code%3A+%60%60%60javascript%0D%0Apublic+String+dateFormatting%28Date+date%29+%7B%0D%0A++++return+new+SimpleDateFormat%28%22yyyy-MM-dd%22%29.format%28date%29%3B%0D%0A%7D%0D%0A%0D%0Apublic+void+load%28%29+%7B%0D%0A++++System.out.println%28%22Loading...%22%29%3B%0D%0A%7D%0D%0A%60%60%60) | [Gemini](https://gemini.google.com/?q=Convert+it+to+more+declarative+names%3A+%60%60%60javascript%0D%0Apublic+String+dateFormatting%28Date+date%29+%7B%0D%0A++++return+new+SimpleDateFormat%28%22yyyy-MM-dd%22%29.format%28date%29%3B%0D%0A%7D%0D%0A%0D%0Apublic+void+load%28%29+%7B%0D%0A++++System.out.println%28%22Loading...%22%29%3B%0D%0A%7D%0D%0A%60%60%60) | 

# Conclusion

Function names are not just labels; they are contracts with the reader. 

[Ambiguous names]([https://maximilianocontieri.com/what-exactly-is-a-name-part-ii-rehab) break this contract and lead to confusion. 

Descriptive, action-oriented names simplify communication and make your code easier to maintain and extend.

# Relations

%[https://maximilianocontieri.com/code-smell-33-abbreviations]

%[https://maximilianocontieri.com/code-smell-153-too-long-names]

%[https://maximilianocontieri.com/code-smell-38-abstract-names]

%[https://maximilianocontieri.com/code-smell-174-class-name-in-attributes]

# See also

%[https://maximilianocontieri.com/what-exactly-is-a-name-part-i-the-quest]

%[https://maximilianocontieri.com/what-exactly-is-a-name-part-ii-rehab]

# Disclaimer

Code Smells are my [opinion](https://maximilianocontieri.com/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned).

# Credits    

Photo by [britishlibrary](https://unsplash.com/@britishlibrary) on [Unsplash](https://unsplash.com/photos/grayscale-photo-of-men-standing-beside-houses-toxJVcTa26k)
  
* * *

> A function name should be a verb or a verb phrase, and it needs to be
meaningful

_Robert C. Martin_
 
%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
