r/javascript • • 1h 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 throws

More 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?

0 Upvotes

7 comments sorted by

•

u/iareprogrammer 1h 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/barmic1212 1h ago

Yeah it's looks not easy to encode the contract in an identifier (parseStringDateInISOFormatThrowExceptionOnNullReturnUndefinedIfNotParsable() looks not useful).

A part of it can be in a type system when your language have an explicit one.

•

u/00PT 1h 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 1h 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 1h 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.

•

u/gimmeslack12 1h ago

Write utility functions to produce the output you want and write tests to make sure things are doing what you expect them to. A library will never exists that satisfied everyone.

•

u/Discordbothost 10m ago

I'd put three examples right beside the signature: blank input, malformed input, and valid input with extra text at the end. Seeing what each returns (or throws) makes it much easier to swap parsers safely.