r/AskProgramming • • 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?

54 Upvotes

26 comments sorted by

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.

6

u/YMK1234 Nov 13 '21

Also input methods were very clunky back then, that also might be one of the reasons.

yeah, imagine writing java code without autocomplete 🤣

3

u/ericanderton Nov 13 '21

We had awful things like the Java Programmer's Almanac and other API books. Basically what Stack Overflow is now, only printed on the cheapest and thinnest paper imaginable with a 3" spine.

https://www.amazon.com/JAVA-TM-DEVELOPERS-ALMANAC-1998/dp/B00GB6T9DQ

And what was worse: these went out of date almost as soon as they went to print.

2

u/[deleted] Nov 14 '21

Or imagine writing Java on paper without any api documentation. I hate university

5

u/Ikkepop Nov 13 '21

Immagine writting java on a punch card...

3

u/lanzaio Nov 14 '21

People also wrote code on typewriters and not computer screens. So typing was also painful when you made mistakes.

1

u/wrosecrans Nov 14 '21

Not even normal typewriters. The early teletypes would momentarily lock the keys when you pressed one, to keep all the timings right because there wasn't anything like 'n key rollover' in those days. So there were physical limits on how fast you could type on early interactive computers, in addition to the practical limits of interactive editing on paper.

0

u/[deleted] Nov 13 '21

[deleted]

4

u/Ikkepop Nov 13 '21

I always love when people talk about how programming was back in the days.

Dude don't get me started :D ...
Ok I don't know much how it really was "back in the day", I started in the 90s and only as kid with a hobby

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...

Stackoverflow: Why are C names shortened

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

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

u/pancakeQueue Nov 14 '21

C functions are documented in the man pages.

man strstr

-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 ?