There's an old joke among programmers that there are only 2 hard problems in computer science: naming things, concurrency, and off-by-one errors. Just

ok Considered Harmful

submited by
Style Pass
2024-05-10 21:30:07

There's an old joke among programmers that there are only 2 hard problems in computer science: naming things, concurrency, and off-by-one errors.

Just like there's an unwritten law that every error variable in Go must be named err, there's an unwritten law that every map existence variable in Go must be named ok. If you don't do this you'll never be invited to Commander Pike's birthday parties, you'll start attracting snide comments on your pull requests, and the cool kids will treat your part of the code base like a sick raccoon curled up in there and died a few months ago. Naming map existence bools something other than ok just isn't done.

The ok idiom has extended out of strict map existence checks, into other related areas. For example, Dolt's code base has many methods with a signature like this:

The boolean return values is true if a table with the given name exists in the database, false otherwise. Now, separating out an existence check from an error condition is a good thing, actually. You never want to force clients to check for a particular error type in business logic, like this:

Leave a Comment