October 14th, 2024

On naming things: The tension between naming something for what it is, what it does, or how it is used

There is a tension in the problem of naming things: Do you name something for what it is? Do you name it for what it does? Or do you name it for how it is used?

Previously, we saw std::type_identity, which is named after what it is. One reaction was that the class should have been named something like std::non_deduced, which names it after how it is used: To prevent template type deduction.

template<typename...Args>
void enqueue(
    std::function<void(std::non_deduced_t<Args>...)> const& work,
    Args...args)
{
    enqueue([=] { work(args...); });
}

We have the opposite problem with std::in_place: The in_place types are named after how they are used, rather than what they are. They are tags that are used by some constructors of variant-like types to indicate what they should hold.

  • in_place: Hold the primary thing (as opposed to nothing, or the alternate thing)
  • in_place_type<T>: Hold the thing of type T
  • in_place_index<I>: Hold the thing at index I

But when we used it as a tag type in our example, it was used not to indicate what the class itself should hold, but rather what the class should hold a reference to. Perhaps it could be named after what it is: std::type_tag<T> and std::index_tag<I>.

Though what about std::in_place? Maybe we leave that one alone?

And then we have std::monostate, which is named after what it is, rather than how it is used. In other languages, this type goes by the name unit, which to me feels like a name chosen with category-theory-colored glasses. (Also, the name unit could be misinterpreted as having to do with systems of measurement.)

Bonus chatter: Note that none of the examples are named after what they do, because none of them do anything!

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

13 comments

Discussion is closed. Login to edit/delete existing comments.

  • word merchant

    TabTheTextOutForWimps()
    BozosLiveHere()
    PrestoChangoSelector()

    • Ron Parker

      BearNNN (where NNN is the ordinal of the export from USER)
      BunnyNNN (likewise, for the kernel)

  • Michael Taylor

    Could be worse. They could name things that provide no actual indication of anything useful (what it does, what it represents, etc). Entra comes to mind…

    • Sigge Mannen

      Could be worse still, wasn’t it called Azure Active Directory while having nothing in common (nor possible to interoperate) with “real” AD?

  • Joe Beans

    All I care is that they are named in Pascal Case otherwise it’s not much easier to read than assembly language. That was always the big problem I had with the C/C++ world, their naming conventions are like every byte costs big money and it is always unnecessarily difficult to quickly see what a chunk of code is doing.

    • 紅樓鍮 · Edited

      Actually C++ these days have ridiculously long identifiers spelt in full in its standard library like and , and they're all in snake_case, which costs more bytes than PascalCase.

      The C standard library on the other hand is still stuck with short, abbreviated symbols (), which ironically makes less sense in C than in C++ because C doesn't have namespaces and is much more prone to naming conflicts. The broader C community makes more sense...

      Read more
  • Patrick

    I’ve met many programmers in my life, and each one of them assured me that THEY know how to name things. It’s everybody else who doesn’t know what they’re doing.

    • Neil Rashbrook

      Naming things is one of the two hard problems in computer science.

      • Me Gusta

        Alongside cache invalidation and off by one errors, right?

    • Ivan Kljajic

      container .empty() / .clear() are one of my workmate’s favourite gripes about naming.

      • 紅樓鍮

        libc++ marks its vector‘s empty method as nodiscard, so you’ll get a warning if you mistake it for clear. I haven’t checked other containers or other standard library impls.

      • alan robinson · Edited

        if only we were still using LISP we’d know for sure the function should have been named emptyp, and ….

        caarnilmap ?

        😉

        just kidding mostly, but adding p to all predicate functions was a good, almost Hungarian convention.

      • Simon Farnsworth

        Fun pairing I’ve seen in two parts of a big codebase: one group of collections used

        bool empty() const; void clear();

        as its naming convention, while another group used

        bool is_empty() const; void empty();

        .