r/AskProgramming • u/orgad • Nov 13 '21
Why are C API functions are badly named?
As an example consider the function strstr(). How is one supposed to understand the usage of this function solely by its name? Had I wrote such function my pull request would have been rejected for bad naming of a function.
Why is that so in the C standard library?
41
u/CrimsonWolfSage Nov 13 '21
Back in the 1972, when C was written, compilers would only use the first 8 characters to determine uniqueness of a name.
But, at least C has that much, even older languages are not as pretty to read. Like A and B languages...
8
u/Treyzania Nov 13 '21
6 characters which is a carryover from 36-bit-word PDPs, meaning you could fit the whole symbol in 1 word.
4
u/nemec Nov 13 '21
understand the usage of this function solely by its name
False premise. You aren't. This is back when it was feasible to read and basically memorize the entire documentation for a language, so it was expected that you do that.
14
u/c3534l Nov 13 '21
People used to assume you would look things up and learn them. You weren't supposed to know what it was without reading the documentation. People from that era used to call the names "mnemonics" - strstr is easier to remember than 10100010111010101...
1
u/Zyklonista Nov 14 '21
Every single language has its own fuckups in the API. Next question, please.
1
u/orgad Nov 14 '21
Mention one fuckup in Java API
1
u/Zyklonista Nov 14 '21
Sure. You can start with this wonderful gem - https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Boolean.html#getBoolean(java.lang.String)
1
u/orgad Nov 14 '21
interesting, got some more? :)
2
u/Zyklonista Nov 14 '21
There are plenty, like I said, not just in Java, but in any API that one chooses at random. The point is that sometimes the line between familiarity with the API and genuinely terrible naming/design becomes a bit nebulous.
strstr, for instance, is a terrible name, but to a C developer who's spent decades working with C, it might appear inconsequential. So it becomes difficult to make an objective judgment without taking one's background into consideration. Plus historical baggage as well, of course, as others have pointed out.Another example is the difference between the Erlang and Elixir APIs. Erlang has some APIs where the ordering of arguments is sort of ad-hoc whereas Elixir, building upon Erlang and its mistakes, tries to enforce a saner ordering where the first element is almost always the object/entity at hand. Over time, I'm sure this will somehow get violated as well.
1
u/wrosecrans Nov 14 '21
Reddit has a limit on comment length, so I can't include both the criticism and the name of a badly named Java method.
1
-40
u/Nufflee Nov 13 '21
If I had to hazard a guess, they were written by some rando in his basement in the 1970s on a 80 char limited screen, so, in order to save screen space, they'd shorten the names as much as possible. They probably never expected their code to be used 50 years later. I've also heard that compilers had identifier length limits back then which could've played a part.
41
u/CartmansEvilTwin Nov 13 '21
Some rando who worked for one of the largest research institutes for computers/electronics at the time and worked on (for the time) very capable hardware.
I don't know if Kernighan and Ritchie ever intended C to be used 50 years later, but it certainly wasn't supposed to be a hacked together mess either.
16
u/aioeu Nov 13 '21 edited Nov 13 '21
I've also heard that compilers had identifier length limits back then which could've played a part.
Some early assemblers, such as MACRO-11 used on the PDP-11, required symbols to be unique within the first 6 characters. Other languages for these systems often took on the same constraints.
-8
u/Nufflee Nov 13 '21
Yeah, the 6 character number was in my mind but I was very unsure about it. So that could totally be it
6
u/aioeu Nov 13 '21
I found this in the MACRO-11 manual:
3.2.2 User-Defined and Macro Symbols
[...]
The first six characters of a symbol must be unique. A symbol can be written with more than six legal characters, but the seventh and subsequent characters are checked only for ASCII legality and are not otherwise evaluated or recognized by MACRO-II.
9
u/TuGatoTieneCatarro Nov 13 '21
If i had to hazard a guess, i would at least try to use a modicum of intelligence in the absence of any actual data. But then again, why hazard a guess about the history of the C language, which is widely documented everywhere on the web?
8
u/Ikkepop Nov 13 '21 edited Nov 13 '21
By rando's you mean Dennis Ritchie and Brian Kernighan, and by basement you mean Bell Labs, the research arm of AT&T ?
62
u/Ikkepop Nov 13 '21
because C was invented in a time and on a machine where memory and computing power was scarce and extremely expensive, so short names made sense. Shorter names meant saving on compile time and memory usage. Also input methods were very clunky back then, that also might be one of the reasons.