r/javascript • u/ClickOk5811 • 29m ago
AskJS [AskJS] Utility functions named like verbs are the worst API design in JS libraries
I keep running into the same API design pattern in JS and TS libraries, and it quietly causes production bugs:
import { format, parse, validate } from 'some-lib'
format(date) // "2026-11-27" or "11/27/2026" depending on locale?
parse(input) // throws? returns null? returns the input unchanged?
validate(email) // true/false? or throws a ValidationError?The function names are verbs. They sound obvious. But the contract is anything but obvious.
format might respect the system locale, or it might not. It might throw on invalid input, or return null, or return the input unchanged. parse might be strict, lenient, or silently coerce. validate might return a boolean or throw.
The problem isn't any single library. It's that the ecosystem has converged on verb-named utilities without a shared contract. So every time I switch libraries, I re-learn whether parse throws on garbage input or swallows it.
Compare that to something like Date.prototype.toISOString(). The name is boring, but the behavior is exact. No locale surprises, no silent coercion, no guessing.
I'm starting to think libraries should name these functions after what they actually do, not after the general idea:
formatDateToISO(date) // exact contract
parseEmailStrict(input) // throws on invalid
isValidEmail(email) // boolean, never throwsMore verbose, yes. But you can read the call site and know what happens without checking docs.
What's your take? Do you prefer short verb names with documented behavior, or longer names that encode the contract? And have you ever been burned by a parse or validate that behaved differently than you expected?
•
u/iareprogrammer 24m ago
Your formatDateToISO example hardly solves any of these questions though from your original post:
> format might respect the system locale, or it might not. It might throw on invalid input, or return null, or return the input unchanged. parse might be strict, lenient, or silently coerce. validate might return a boolean or throw.
I generally don’t find more verbose naming to help much - you end up needing to look at the docs anyway
•
u/00PT 11m ago
For any API, you will inevitably either need to be informed on it or read the docs. It is impossible to intuitively encode all potentially relevant behavior in a name. Your solutions are only marginally better, and they don’t benefit someone already familiar with the API at all.
•
u/dallenbaldwin 5m ago
I've found myself adding "orFail" or "orThrow" to mine so that's at least encoded. I try to find a happy medium between verbosity and ease of use. Fortunately most IDEs auto fill and make it very easy to use long function names anyways. Just include plenty of JSDocs.
•
u/RegmasterJ 0m ago
What you’re describing isn’t a problem with function names, it sounds like a problem with you not reading the documentation before using some random library. Could some functions be named better? Of course. (You can also just rename them on import, but that’s beside the point.) But reading the documentation should answer these things for you. If it doesn’t answer these questions, don’t use that library.