DEV Community

Cover image for Code Smell 220 - Return Tuple
Maxi Contieri
Maxi Contieri

Posted on • Edited on • Originally published at maximilianocontieri.com

Code Smell 220 - Return Tuple

You need to return more than one object

TL;DR: Don't return multiple values.

Problems

  • Coupling

  • Missing Abstraction

  • Readability

  • Extensibility

Solutions

  1. Create a return object grouping the tuple

  2. Reify it into an object with cohesion and behavior (not a DTO or Dictionary)

  3. Look for the object in the real world using the MAPPER

  4. try to return void and delegate the solution to the modified object avoiding accidental mutations

Context

A function returning multiple values in languages that allow it is a problem.

Developers can use this hack to avoid reifying concepts.

Some languages are: Javascript, Go, Lua, Matlab, PHP, Python, Rust, and Swift

Sample Code

Wrong

func getNameAndAge() -> (String, Int) {
    let name = "John"
    let age = 30
    return (name, age)
}

Enter fullscreen mode Exit fullscreen mode

Right

struct PeopleProfile {
    let name: String
    let age: Int
}

// We reify the PeopleProfile object
func getNameAndAge() -> PeopleProfile {
    let name = "John"
    let age = 30
    let profile = PeopleProfile(name: name, age: age)
    return profile
}
Enter fullscreen mode Exit fullscreen mode

Detection

[X] Automatic

This is a language smell.

We can tell our linters to warn us.

Tags

  • Coupling

Conclusion

This is yet another language feature that hinders clean code and blinds us from seeing missing abstractions in the Bijection.

Relations

More Info

Value Tuples in C#

Named Tuples in Python

Disclaimer

Code Smells are my opinion.

Credits

Photo by Edgar Soto on Unsplash


By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and in effect increases the mental power of the race. Before the introduction of the Arabic notation, multiplication was difficult, and division even of integers called into play the highest mathematical faculties. Our modern power of easy reckoning with decimal fractions is the almost miraculous result of the gradual discovery of a perfect notation.

Alfred North Whitehead


This article is part of the CodeSmell Series.

Top comments (6)

Collapse
 
fyodorio profile image
Fyodor

Using javascript and beginners tags for articles not intended for beginners and tangentially related to JavaScript is a technical writing smell here on dev, smells like a miserable marketing attempt

Collapse
 
mcsee profile image
Maxi Contieri

I think every begginner should know this before starting making this mistakes like many seniors do.

I have removed Javascript tag since it is not solely related to this language, Tough it applies to JS as well

Collapse
 
ervin_szilagyi profile image
Ervin Szilagyi

Golang's return nill, err goes BRRRR...

With all honesty, I don't think this is not a code smell at all. Having to bundle multi-value returns into objects will result in a bunch of "nothing objects" and DTOs. You have to maintain them, depending on the language you have to export them, and obviously you have to name them (I really like meaningless object names such as SomethingResult, SomethingPair or even worse, just simply Res). At some point somebody will take this Res class and dump it in a globally shared utils package. At this point there is difference between using a tuple from a standard library or using the stuff from my utils. Just please use the tuple then....

Coming back to Golang, the idiomatic way of handling errors is to return them. In most cases this will result in multiple returned values. Regardless if we consider this good or bad choice, it does not really matter. But it is certainly not a code smell.

Collapse
 
mcsee profile image
Maxi Contieri

Hi

Dtos are another code smell

They don't need to be anemic.
Just find the cohesive behavior of the DTO

I wasn't aware of golang convention. I will update the article.
Thank you

Collapse
 
cicirello profile image
Vincent A. Cicirello

What are your thoughts on Python's namedtuple that gives names to the components of the tuple?

Collapse
 
mcsee profile image
Maxi Contieri

They are as anemic as DTOS

The key is to find the missing behavior

I will also add them to the article. Thank you