I'm going to be engaging in microblogging lately, and no, not by a definition Jack Dorsey would approve of. I literally mean, I should make more blog entries, smaller in individual scope, and not from some sense of obligation to having an active blog, but because I keep having repetitive conversations online, all in the same shape: "this constant exhaustion of explaining my principles coulda been a blog post." That's all the more reason to keep these narrow in scope!

First up: the people who pounce on C as "a bad language" when there's some kind of CVE floating around.

Now, I can find some degree of common ground with such people. We have other good high-level compiled languages these days[^1], and quite a bit of software has been written in C that probably ought to be in one of those other languages. C can be a bit dangerous - it has to be, to be good at the stuff in its purview, but we'll save that for another day. And certainly, it's an environment that makes you _feel_ design defects, as they immediately degrade your ability to organize code at scales larger than your working memory - something I  like, as valuable feedback, but other people might reasonably not.

The closest I've come, perhaps, to agreeing with a C skeptic, is the idea that [C promotes bad cultural norms](https://sunshowers.io/posts/sshole/). But does it? Well, that depends what you mean by "C."
## The stdlib from hell

A funny trend I've found is that when you get people together who self-describe as loving C, you will hear just about all of them absolutely dogging on the C standard library. `errno` is a global. Locales are also global state, and affect a cornucopia of functions in ways that break your expectations. Then there's `pthread` cancellation points...

While I do think C is too high-level[^2], it's low *enough* that if you have good taste, you have a lot of ability to opt out of the standard library and do your own thing, or interact with `libc` only in very deliberate ways.

So, yes. A lot of existing C code perpetuates bad conventions about in-band signalling. But you don't have to write C like that.
## Doing better

What I'm going to show you is the technique I use, which I'm planning to write a library for. You don't have to use this exact one, and it might seem overkill at first, but bear with me. Each piece is doing a job.

```c
int main(void) {
  errdata e = {0};
  struct foo_s foo; // Will need to be initialized
  char addr[1024];
  char msg[] = "Hello, world!";

  bool ok
    =  foo_init(&foo, 5, &e)
    && foo_scan(&foo, &addr, sizeof(addr), 60*1000, &e)
    && foo_connect(&foo, &addr, &e)
    && foo_send(&foo, &msg, sizeof(msg), &e)
    && foo_disconnect(&foo, &e)
    && foo_close(&foo, &e)
    || fail(
      print_clear_err(&e)
      && foo_close(&foo, &e)
      || print_clear_err(&e)
    );
  return ok ? 0 : 1;
}
```

So what's going on here? Well, first of all, we have a bunch of functions that return booleans (`true == success`), which means that we can use the short-circuiting logic operators to cut execution short in the event of something failing. If error handling requires checking every single step of the way, people won't do it. But if you can consolidate a sequence of things into one failure check, people will naturally check for failures with the concise idiom. And you can actually look at the code and reason about the failure logic.

This is just a boolean *for good reason.* In order to have the concise chaining idiom, it's not good to put complex information in the return value where it might get lost, or be awkward to save. This means resisting the urge to put full error details in here, and certainly not any important output data. It is doing one job - control flow - well.

Next, errors happen, and we need a place to report the details. We have stack-allocated error data, which then gets populated by called functions if something goes wrong. This is great for a couple reasons.

1. Room for greater detail. You can say which function failed, provide some parameters for context, maybe even hold a small stack trace. Very awkward to do this with a return value, and gross to do it with globals.
2. It's allocated early on, local to your callstack, so if you get an OOM error, there's very little that could go wrong in reporting the problem.
3. You can thread the errdata pointer through the callstack, preventing a chain of copies in the failure case (and often you get similar ergonomics with return values too):

```c
bool foo_scan(
  struct foo_s *f,
  void *addr, size_t asize,
  int timeout, errdata *e
) {
  foo_iface_t ifaces[40];
  return
    foo_open_interfaces(f, &ifaces, sizeof(ifaces), e)
    && foo_poll_loop(f, &ifaces, timeout, e)
    && foo_save_addr(f, addr, asize, e)
    && foo_close_interfaces(f, &ifaces, e)
    || fail(
      foo_close_interfaces(f, NULL, e)
    );
  }
```

It's time to understand the `fail` function, which takes a boolean, and always returns false. This means that your failure handling code can *succeed at failure handling* while still presenting an overall failure as the final result. There might be other useful functions like this, later on, which help with reporting failure *in* failure handlers.

What's cool about this is that all the data lives in stack memory that you own, with simple lifetimes. A lot of `malloc` and copy overhead falls away. Something is needed by a caller 5 frames up (very common for errdata, but sometimes for success results too)? No problem, put it where it belongs.

This can probably be more efficient than Rust ADTs, but that's not the point. It's also, in some cases, less efficient than traditionalist C code, which is a worthy concession. The point is that by being so granular - separation between failure signaling, failure content, and successful result content - you prevent out-of-band signalling problems in ergonomic C.

I plan to write a header-only library for this coding style (Successful Programming, a jokey name based on _returning success booleans_) called Sphere. This means that you can wrap the parts of the stdlib, or other libraries, in these calling conventions.
## Why bother?

Part of the political project of maligning C is to consolidate power in the Rust core developer team, which is not entirely composed of LLM boosters, but contains enough of them that they have become tricky for the team to navigate around, and which led to the infamous "social issues are out of scope" rule when deciding for the entire community whether using `rustc` would be crossing the sociopolitical picket line of using LLM-generated tools.

Rust has support for a limited set of hardware platforms, which is causing issues for people who care about permacomputing, right as people start to care about permacomputing.

If your idea of safety for the future of computing is so narrow to only cover memory safety issues, you will ignore issues like:

 - The lack of viable alternative compilers.
 - The ecosystem norms of using giant piles of third-party code.
 - Logical errors that have the same scope of impact as memory safety issues.
 - Making other projects unportable to their previous platform list.
 - The pre-LLM trend of cognitive surrender to the borrow checker that led to LLM boosters loving the language so much. Just ask DHH!
 - For many competent programmers, Rust is deeply unpleasant and means people will leave rather than deal with it.
 - Loss of relevant battle-hardness in rewrite projects.

We've seen several rewrite projects actually *cause* security problems, because in old well-reviewed C code, memory safety issues are the least of your worries, and cognitive surrender will leave you poorly-equipped to deal with the stuff that actually matters. You are interacting with the operating system or other non-local mutable state over an API. Doing that correctly requires domain knowledge, no matter the language.

Thus, while using Rust is not automatically a bad thing, the people who demand everything be rewritten in Rust, and so urgently that only LLMs can do it, are going to cause more problems than they solve, and use a public safety panic to do it. They are a drain on our communities, by constantly demanding other people's attention to argue with them. I do not intend to give them that attention going forward. I will link them to this page instead.

You can write good, safe code in C, with careful engineering norms. Rust (popular) culture has little to offer the safety discourse, being primarily driven by religious rhetoric and memetic shibboleths.

[^1]: And if the good ones don't tickle your fancy, we also have Rust.

[^2]: More future microblogging material!