Misuse by design
Why it matters Most bugs at an API boundary are not in the API. They are in a caller who used it in a way that looked fine and silently did the wrong thing. You cannot fix every caller; you can make wrong use impossible, or loud.
An assistant writing an API tends to be helpful: if you send before connecting, it connects for you; if you pass a string where bytes were expected, it encodes it; if you forget an option, there is a default. Each kindness hides a mistake until it surfaces somewhere far away.
Fail early, fail loudly: raise an error at the exact call that is wrong, before anything has changed, with a message that says what was expected.
Four tools
- Explicit states: an object that knows whether it is new, open or closed, and refuses calls that do not fit.
- Named options, no booleans:
mode="dry_run"cannot be swapped with another flag by accident;copy(a, b, True, False)can. - No silent defaults for choices that are dangerous to get wrong: make the caller say it.
- Types that carry meaning: money that knows its currency cannot be added to money in another.
In this module, hidden misuse scripts call your API wrongly and expect an error at the wrong call, with nothing changed. Correct-use scripts expect it to just work. Each problem lists the misuses to guard against.
Recap Helpful guesses hide bugs. Refuse wrong use at the call that is wrong, before any effect.
Spotted a problem in this lesson? Report it · Ask about this lesson
This exercise opens soon
Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.
This browser is blocking cookies for learn.floati.life, so lessons you finished here cannot open in order. Allow cookies for this site and reload.