Home > Web Front-end > JS Tutorial > Code Smell - Non-Imperative Functions Names

Code Smell - Non-Imperative Functions Names

DDD
Release: 2025-01-10 09:49:41
Original
825 people have browsed it

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

  1. Use action-oriented verbs
  2. Make names 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

Code Smell  - Non-Imperative Functions Names

Refactoring 005 - Replace Comment with Function Name

Maxi Contieri ・ Jun 7 '22

#javascript #beginners #refactorit #programming

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...");
}
Copy after login

Right

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

public void loadUserPreferences() {
    System.out.println("Loading user preferences...");
}
Copy after login

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

Code Smell  - Non-Imperative Functions Names

Code Smell 33 - Abbreviations

Maxi Contieri ・ Nov 24 '20

#oop #codenewbie #programming #tutorial
Code Smell  - Non-Imperative Functions Names

Code Smell 153 - Too Long Names

Maxi Contieri ・ Jul 29 '22

#javascript #webdev #beginners #programming
Code Smell  - Non-Imperative Functions Names

Code Smell 38 - Abstract Names

Maxi Contieri ・ Nov 30 '20

#oop #codenewbie #naming #webdev
Code Smell  - Non-Imperative Functions Names

Code Smell 174 - Class Name in Attributes

Maxi Contieri ・ Oct 29 '22

#javascript #webdev #beginners #programming

See also

Code Smell  - Non-Imperative Functions Names

What exactly is a name? — Part I: The Quest

Maxi Contieri ・ Feb 9 '21

#codenewbie #tutorial #webdev #oop
Code Smell  - Non-Imperative Functions Names

What exactly is a name - Part II Rehab

Maxi Contieri ・ May 23 '21

#tutorial #codenewbie #programming #webdev

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

Code Smell  - Non-Imperative Functions Names

Software Engineering Great Quotes

Maxi Contieri ・ Dec 28 '20

#codenewbie #programming #quotes #software

This article is part of the CodeSmell Series.

Code Smell  - Non-Imperative Functions Names

How to Find the Stinky parts of your Code

Maxi Contieri ・ May 21 '21

#codenewbie #tutorial #codequality #beginners

The above is the detailed content of Code Smell - Non-Imperative Functions Names. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template