Skip to content

Latest commit

 

History

History
124 lines (71 loc) · 4.62 KB

File metadata and controls

124 lines (71 loc) · 4.62 KB

Code Smell 220 - Return Tuple

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 (neither a DTO nor a 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: C#, 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)
}

Right

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

// You 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.

Relations

Code Smell 10 - Too Many Arguments

Code Smell 122 - Primitive Obsession

Code Smell 40 - DTOs

Code Smell 27 - Associative Arrays

More Info

Value Tuples in C#

Named Tuples in Python

Multiple Return Values in Go

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

Software Engineering Great Quotes


This article is part of the CodeSmell Series.

How to Find the Stinky Parts of your Code