I don't program in C or defend that language, but each time I read someone complain about the need to manage null-termination and such in C, I wonder if they'd have similar complaints about assembly programming. Sure, C is "higher level" than assembly but only barely? 🤔

#programming #reasoning

@smurthys Over and above needing to use assembly at all? 😅

@scottmichaud If people were using assembly instead they wouldn't (shouldn't) be complaining about all the bookkeeping. So, why complain when using C?

Also, no library--even the standard library--is going to do everything we want the way we want. Yes, they can (always) do better but complaining about bookkeeping surprises me.

Something about bull and horns.

@smurthys I've been using C a lot more in recent months. It's like a breath of fresh air to me.

I do wish I started off with lower level languages such as C. I feel as though a lot of programmers can end up spoiled by the treats of their higher level languages.

@smurthys People complaining usually have something specific in mind. For example, for the null-terminated string, people want pointer + length instead. The proliferation of C APIs accepting null-terminated strings means that even if one writes a custom pointer + length representation, it can't be safely interoperated with those APIs (and this impacts the usability of C++ string_view as well).

@lesley @smurthys Nope. It can be. You just need to provide a C++ header for the consumer to compile in that wraps the C API (or, without C++, the pointer be to a C struct of a pointer and a length, instead of the string itself).

It does require effort to design a good, difficult-to-misuse API, though. C will push you in simple, brittle directions by default.

@scottmichaud @smurthys If a C API only accepts null-terminated strings (no pointer/length version) and I have a string_view, I need to convert that string_view to a std::string to pass it to it safely. This negates the advantage of string_view, and I'd just stick with string in the first place (otherwise, in the worst case, std::string -> string_view -> string round-trip will happen)
@lesley @scottmichaud @smurthys This is why I kept having to tell people who wanted to change string APIs in the standard library that pass to C under the covers to string_view "no"

@lesley @smurthys Context: I was referring to the other side, making a C library that overcomes C's limitations for their end-users.

A library that doesn't know or care to do something better will paint their end-users into a corner, yes.

@lesley I get all that, but that's how C works. There was no advertising/promises otherwise.
@smurthys @lesley I don't think "false advertising" is the crux of the issue people are complaining about. :)
@smurthys *gestures at optimizing compilers* No, they aren't very close
@smurthys they are complaining now but just wait until they realize that they can make substrings by simply inserting a null into the middle of the string
@Wearwolf @smurthys next they find out the something called multithreading exists...

@smurthys

Imagine all the cpu cycles wasted running strlen()! C is high level enough that I have used the preprocessor for creating my own custom but quirky string typedefs that have a defined maximum length and will truncate if not careful. Instead of printf format strings I use concatonation that I'm sure most programmers would dislike. I wanted something like Ada strings but it's the best I could do with C rules that only has language-level string concat for static strings. Every operation self terminates, and there is null padding at the end just in case!