Code Smell 220 - Return Tuple
Tags: webdev, beginners, programming
Published: 2023-08-02 23:53:40

*You need to return more than one object*

> TL;DR: Don't return multiple values. 

# Problems

- [Coupling](https://dev.to/mcsee/coupling-the-one-and-only-software-design-problem-2pd7)

- 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](https://dev.to/mcsee/code-smell-40-dtos-4426) or [Dictionary](https://dev.to/mcsee/code-smell-27-associative-arrays-2n7j))

3. Look for the object in the real world using the [MAPPER](https://dev.to/mcsee/what-is-wrong-with-software-5pa)

4. try to return void and delegate the solution to the modified object avoiding [accidental mutations](https://dev.to/mcsee/the-evil-powers-of-mutants-1noo)

# 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

[Gist Url]: # (https://gist.github.com/mcsee/d7cb1ea13ceb86ad1c087e2a52926c89)
```swift
func getNameAndAge() -> (String, Int) {
    let name = "John"
    let age = 30
    return (name, age)
}

```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/e6db8dfa30b45043ff6a187ba14be10a)
```swift
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
}
```

# 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](https://dev.to/mcsee/the-one-and-only-software-design-principle-3086).

# Relations

{% post https://dev.to/mcsee/code-smell-10-too-many-arguments-4o67 %}

{% post https://dev.to/mcsee/code-smell-122-primitive-obsession-4a55 %}

{% post https://dev.to/mcsee/code-smell-40-dtos-4426 %}

{% post https://dev.to/mcsee/code-smell-27-associative-arrays-2n7j %}

# More Info

[Value Tuples in C#](https://learn.microsoft.com/en-us/dotnet/api/system.valuetuple-2?view=net-7.0)

[Named Tuples in Python](https://www.geeksforgeeks.org/namedtuple-in-python/)

# Disclaimer

Code Smells are my [opinion](https://dev.to/mcsee/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned-1n3a).

# Credits

Photo by [Edgar Soto](https://unsplash.com/@edgardo1987) on [Unsplash](https://unsplash.com/photos/1HIKnKtXEU0)
    
* * *

> 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_

{% post https://dev.to/mcsee/software-engineering-great-quotes-26ci %}

* * *

This article is part of the CodeSmell Series.

{% post https://dev.to/mcsee/how-to-find-the-stinky-parts-of-your-code-1dbc %}