DEV Community

Cover image for Code Smell 85 - And Functions
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

1

Code Smell 85 - And Functions

Do not perform more than requested.

TL;DR: Unless you need atomicity, do not perform more than one task.

Problems

  • Coupling
  • Single Responsibility Principle violation
  • Readability
  • Low Cohesion
  • Testability

Solutions

  1. Break the function

Sample Code

Wrong

def fetch_and_display_personnel():
  data = # ...

  for person in data:
    print(person)
Enter fullscreen mode Exit fullscreen mode

Right

def fetch_personnel():
  return # ...

def display_personnel(data):
  for person in data:
    print(person)
Enter fullscreen mode Exit fullscreen mode

Detection

Functions including "and" are candidates. However, we need to check them carefully since there might be false positives.

Tags

  • Readability
  • Naming

Conclusion

We should avoid doing more than needed, and our functions should be both minimal and atomic.

More Info

Credits

Photo by Paul on Unsplash

This smell was inspired by


If it takes more than a sentence to explain what you are doing, it’s almost always a sign that what you are doing is too complicated.

Sam Altman


This article is part of the CodeSmell Series.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay