Get the latest tech news

Don't return named tuples in new APIs


In my opinion, you should only introduce a named tuple to your code when you're updating a preexisting API that was already returning a tuple or you are wrapping a tuple return value from another API. Let's start with when you should use named tuples. Usually an API that returns

And that might be true for when you define your data structure (and I'll touch on this "simplicity of definition" angle later), but it actually makes your API more complex for both you and your users to use. For you, it doubles the data access API surface for your return type as you have to now support index-based and attribute-based data access forever (or until you choose to break your users and change your return type so it doesn't support both approaches). On top of that, the tuple API allows for things that you probably don't want people doing with your return type, like slicing, iterating over all the items as if they are homogeneous, etc.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of new APIs

new APIs

Photo of tuples

tuples

Related news:

News photo

Typing lists and tuples in Elixir

News photo

Intel Compute Runtime 24.26.30049.6 Provides New APIs & Extensions

News photo

Google lets third-party developers into Home through new APIs