1. 16
  1.  

  2. 8

    My favourite example of confusable methods cost me half a day of debugging. LLVM has two methods on a machine instruction:

    • isBundle
    • isBundled

    The second was the one I wanted, the first was the one that I had. Renaming this to isInBundle would have made it a bit more obvious and a lot harder to accidentally typo, but even then it’s confusing. Oh, and as I recall, neither had a doc comment (checking now, one does).

    There’s one example that isn’t here, which is part-of-speech confusion. The C++ standard library is full of these. The most egregious is .empty() which is a method whose name is both an imperative verb and an adjective. This method, in a sane API, would be called isEmpty to make it clear that it’s predicate that is querying a property of the collection. To make things worse, the classes that have an empty method also have a clear method (another word that is both a verb and an adjective), which does the other one. This is a bit less liable to confusion (I don’t actually know what a clear array would be - one where all of the elements are transparent?) but still not ideal. The OpenStep equivalent of this is called removeAllObjects, which is totally unambiguous. Similarly, C++11’s range-based for loops enforced in the language that both .begin() and .end() have special meanings and must return iterators to the start and end of a collection (rather than begin or end an operation).

    The site actually recommends calling things close in a couple of places. That’s interesting because it’s a homonym (which trips up translators periodically, do you mean the verb or adjective?) but by convention it is almost certainly intended a specific one of the two verb forms. Unless you’re writing a flight simulator or similar, when you may want to instruct one object to close on another or measure how close two objects are…

    1. 7

      There’s one example that isn’t here, which is part-of-speech confusion. The C++ standard library is full of these. The most egregious is .empty() which is a method whose name is both an imperative verb and an adjective. This method, in a sane API, would be called isEmpty to make it clear that it’s predicate that is querying a property of the collection. To make things worse, the classes that have an empty method also have a clear method (another word that is both a verb and an adjective), which does the other one. This is a bit less liable to confusion (I don’t actually know what a clear array would be - one where all of the elements are transparent?) but still not ideal.

      I like what Ruby does: the predicate would be empty? and the clear method would be clear!. I think it originally got this from Smalltalk.

      1. 1

        That’s really neat to know; one of the reasons I’ve preferred the Scheme family of Lisps is because they use pred? over predp or pred-p and indicate destructive / mutating operations like reverse! over nreverse.

        The N stands for non-consing, meaning it doesn’t need to allocate any new cons cells.

      2. 1

        Thank you! Great examples. I’m adding a todo right now to incorporate these; I’d do it right now if I wasn’t exhausted.

      3. 5

        Some of these go away when programming in a language other than English, where imperatives are spelled differently from infinitives, both different from nouns and adjectives.

        I’ve been wondering what a programming language with e.g. Finnish grammar would be like.

        That would also be an interesting base-language for translations, since you have much more information in each word. Many translations mistakes are due to confusable English words/forms too.

        1. 4

          Quick note: seems like the link submitted does not have the https:// specified, even though the website does have it, which is somewhat annoying if the browser is in https-only mode. Would be nice if that was changed.

          1. 1

            https-only mode doesn’t rewrite it?

            1. 1

              At least the one built into Firefox doesn’t.

          2. 4

            I hope they’ll add more of these in the future! I’d consider most on the current list rather obvious, but apparently they still creep in even in widely used code so it’s good to study them anyway so they won’t creep into your code (or your colleagues, if you’re doing code review).

            1. 2

              Field name which is singular but which is actually a collection (e.g.: Vector target)

              I’m assuming the vector represents the position of a target in a coordinate system? How else should that be done?

              1. 4

                Oh, no. The term vector in this context means something like “resizable list.” In particular, see https://cplusplus.com/reference/vector/vector/ or https://docs.oracle.com/javase/8/docs/api/java/util/Vector.html .

                Having learned programming first, I got so confused when I first learned linear algebra and saw “vector” used to mean “point.”

                1. 4

                  In my experience, ‘vector’ in computing usually means a chunk of repeated data structures that’s contiguous in memory. GPU docs tend to assume the structure is some kind of number, which is how they represent points.

                  1. 3

                    Sounds like a synonym antipattern

                    1. 1

                      Sometimes I see people doing this when they use the data structure as a function, especially with dictionaries. What I mean is, they use the math definition of a function (maps from some domain to another domain) and then indexing into the structure is like a function call. It can definitely be confusing at first.

                  2. 1

                    The page is somehow pegging a core on my system. I don’t know enough about web development tools to debug why it is doing that, but it feels really weird for that to happen on a page that shows only plain text. Unfortunately can’t read it with scripts disabled either as they use javascript implementation for the tabbed categories.