Stannum/blog/

Tuple abuse

2021-11-03 Permalink

Tuple types tend to be heavily misused. The anonymity of their heterogeneous members obfuscates the code in places where a simple struct with named fields would be more appropriate.

Let’s walk through a few examples from the C++ standard library and elsewhere (though the discussion is not limited to C++, as I’ve seen Go, Lua and Python code abusing them just as well).

Just like function parameters have names, so should their return types, or any other aggregate types for that matter. I thus conclude that given a choice between a struct and a tuple, the former should be preferred in the vast majority of circumstances.

Tuples are, in their essence, structs with unnamed fields. In a Fantasy Language™ these two would be conflated.

Footnotes

  1. std::pair in C++ is an old special case of an std::tuple, thus the discussion here applies to it just as well.

  2. Perhaps value should be renamed to mapped. However I’d say that set and map should have been combined by removing the value part altogether, and extracting a key from a user provided value_type.