Add a more generic encoding/decoding util #271
Replies: 2 comments
-
I like the idea of simplifying data encoding/decoding/conversion. I really like how you are phrasing it too ("autocomplete your way to success"). I do fear though, that the APIs you are proposing would essentially eliminate tree-shakability & also likely come with quite a bit of runtime overhead (the chaining apis you are proposing would require intermediary object instantiations of some kind). One could probably get around that to some extend with some clever proxying ( I do like the general idea though. Juggling the different data types back & forth is definitely one of the more error prone & complicated problems, especially for beginners. |
Beta Was this translation helpful? Give feedback.
-
This might be a third party library. I think the usability is totally worth the code splitting tradeoff but given viems place as almost a "metalibrary to build libraries" it might not make sense for viem to make that tradeoff compared to a third party library or the application itself |
Beta Was this translation helpful? Give feedback.
-
Summary
I would like a new encoding util named
encode
that generically encodes from any data type to any other data type.Why
Lower mental overhead an api that autocompletes to success
What
It is any api that has a single entrypoint (encode) and then autocompletes to success. This is different than the feeling I get today of needing to memorize the names of the encoding utils to use them quickly. This is the user journey:
"I want to encode/decode something"
Api
But another alternative that accomplishes same thing could look like this
Making it look a little like a monad is also possible, but my intuition unnecessary and most would prefer the simpler api.
It's also possible to just infer what it's from
Simpler API internally
Internally maintaining types like toString() compared to infering 'string' in fromBytes(..., 'string') does naturally lead to slightly more maintainable types to maintain internally which is a small but nice bonus.
Easy deprecation plan
A pretty simple code mod could handle a breaking change removing the previous fromFoo toHoo and automatically safely converting them to a new more generic api.
Overall collapsing encode to a single method that is easy to remember is a small but noticeable api improvement with very little cost.
Beta Was this translation helpful? Give feedback.
All reactions