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.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Scale globally with MongoDB Atlas. Try free.

Scale globally with MongoDB Atlas. Try free.

MongoDB Atlas is the global, multi-cloud database for modern apps trusted by developers and enterprises to build, scale, and run cutting-edge applications, with automated scaling, built-in security, and 125+ cloud regions.

Learn More

👋 Kindness is contagious

Explore this insightful piece, celebrated by the caring DEV Community. Programmers from all walks of life are invited to contribute and expand our shared wisdom.

A simple "thank you" can make someone’s day—leave your kudos in the comments below!

On DEV, spreading knowledge paves the way and fortifies our camaraderie. Found this helpful? A brief note of appreciation to the author truly matters.

Let’s Go!