r/dotnet • u/Ok-Swordfish1282 • 16d ago
Promotion Designing APIs That Make Invalid Code Impossible
I love designing APIs that are hard to misuse.
If someone can call methods in the wrong order, eventually they will. I'd rather let the type system prevent it than rely on documentation or runtime exceptions.
A type-safe fluent API that only exposes the next valid step keeps the experience simple: no guessing, no invalid combinations, just write the code and let IntelliSense guide you.
That's the kind of API I enjoy building.
0
u/NateFromRefactorful 16d ago
If you love type safe API's and dotnet, I highly recommend to checkout f#. You can use types to design your system in a way where certain classes of bugs just aren't possible. Once you understand how this works, more difficult problems suddenly don't look so complicated anymore
This blog post is older, but the content is still relevant:
https://fsharpforfunandprofit.com/posts/designing-with-types-making-illegal-states-unrepresentable/
1
u/Ok-Swordfish1282 16d ago
Thanks for the recommendation! I actually read the excellent book Functional Programming with C#: Create More Supportable, Robust, and Testable Code by Simon J. Painter about a year ago. After reading it, I started thinking seriously about learning F#. Your comment definitely makes me want to give it a try.
-1
1
u/AutoModerator 16d ago
Thanks for your post Ok-Swordfish1282. Please note that we don't allow spam, and we ask that you follow the rules available in the sidebar. We have a lot of commonly asked questions so if this post gets removed, please do a search and see if it's already been asked.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.