r/javascript • • 2d 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

11 comments sorted by

10

u/iareprogrammer 2d 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

4

u/barmic1212 2d 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.

2

u/d_russo827 1d ago

A return type can expose `T | null`; TypeScript won't tell callers about thrown exceptions or coercion rules.

5

u/00PT 2d 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.

3

u/dallenbaldwin 2d 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.

2

u/RegmasterJ 2d 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.

2

u/gimmeslack12 2d 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.

2

u/Discordbothost 2d 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.

•

u/acefolio_dev 10h ago

In TypeScript you can push a lot of the contract into the return type instead of the name. Something like zod's safeParse returns a { success, data } or { success, error } result, so the call site has to handle failure and nothing gets thrown or silently coerced. A name like parse can stay short if the type says what happens on bad input.

•

u/steve46317 1h ago

For formatters, make locale explicit; otherwise the same call can produce different strings on different hosts.