Readit News logoReadit News
faxmeyourcode · 2 months ago
This is some of the cleanest, modern looking, beautiful C code I've seen in a while. I know it's not the kernel, and there's probably good reasons for lots of #ifdef conditionals, random underscored types, etc in bigger projects, but this is actually a great learning piece to teach folks the beauty of C.

I've also never seen tests written this way in C. Great work.

C was the first programming language I learned when I was still in middle/high school, raising the family PC out of the grave by installing free software - which I learned was mostly built in C. I never had many options for coursework in compsci until I was in college, where we did data structures and algorithms in C++, so I had a leg up as I'd already understood pointers. :-)

Happy to see C appreciated for what it is, a very clean and nice/simple language if you stay away from some of the nuts and bolts. Of course, the accessibility of the underlying nuts and bolts is one of the reasons for using C, so there's a balance.

citizenpaul · 2 months ago
> I'd already understood pointers.

Ok I hear this all the time. Are pointers really that hard for so many people to understand? I'm not trying to brag it took me I think like 15 minutes to grok them from learning about them the first time. I'm sure it took me longer to be proficient but I don't get this legendary difficulty aura that seems to surround their existance.

Also yes nice project.

Job app complete projected archived and abandoned in 3...2..1... :). I hope not.

ActorNightly · 2 months ago
The issue with pointers is that CS gets taught in a VERY bad way. The way it should be taught is starting with basic assembly on a microprocessor. This trains your brain to think of memory locations and data in memory.

Then when you start using pointers, it makes sense. If variable is a pointer, that means its a memory location. *variable is a way to get that data. Then arrays is just a wrapper around pointer arithmetic.

Whereas with CS, you learn about variables first, which is an abstraction on top of memory, and pointers don't make sense in this regard.

This is why any EE/ECE grads are much better developers than CS grads, because once you understand fundamentals

noufalibrahim · 2 months ago
It's a rabbithole. Pointer to array of structures that have pointer fields. Array of pointers to structures etc. You pass them around and trip over the passing semantics, uninitialised pointers etc etc.
lelanthran · 2 months ago
> Are pointers really that hard for so many people to understand?

Apparently they are; I believe it's the indirection that gets people.

Most learners aren't really taught basics properly - they learn that a variable "contains" a value, when instead they should learn that all values have a type, and some variables hold values.

> I'm not trying to brag it took me I think like 15 minutes to grok them from learning about them the first time.

I can't remember not knowing pointers, so I can't really tell you how long it took for it to click, but I do know that I had done a non-trivial amount of assembly before I used C, so maybe that helped/.

jug · 2 months ago
The worst thing with C pointers was for me that the asterisk is inexplicably used both to declare a pointer and a COMPLETELY different operation of dereferencing a pointer.

I still don't understand this decision. I think it should've been like int^ p = &i; ... or ... int i = *p;

Everything clicked ironically when I went even deeper and studied assembly language. Then following pointers to data vs just reading pointers becomes very clear and explicit.

diegof79 · 2 months ago
Understanding the concept is easy.

The problem arises when you start to mix memory management with more complex structures.

It’s extremely easy to make mistakes, and you must be very careful about ownership and clean up. Those things are not strictly related to pointers, but in C, it’s inevitable to use pointers to handle them. That's why people say pointers are hard.

DanielHB · 2 months ago
> Are pointers really that hard for so many people to understand?

Yes, anyone who has taken algorithms and data structures class in C knows that some people just don't get it.

Also the way people teach it tends to be bad, before teaching pointers you need to teach Stack and Heap at a conceptual level.

codegeek · 2 months ago
"Are pointers really that hard for so many people to understand?"

The * vs & always gets me and not to mention if I ever have to deal with Pointer Math.

faxmeyourcode · 2 months ago
Pointers in and of themselves are not difficult to learn on their own, but when you're learning them alongside your first programming language, it's just adds to the difficulty I think.

I think a lot of noobs learning C struggle with pointers especially because there are no good error messages besides "segmentation fault" :D

copperx · 2 months ago
The concept is straightforward. The syntax isn't. That's why cdecl.org exists.
leptons · 2 months ago
I learned assembly language long before I learned C, so pointers took me about 2 seconds to understand. I suppose it may depend on previous experience.
bsder · 2 months ago
> Are pointers really that hard for so many people to understand?

Yes. Especially pointer to pointer to ...

The big problem is that arrays are conflated with pointers because C doesn't do slices. If C had slices, people would naturally avoid pointers except in the cases where they were genuinely necessary. That would make teaching pointers vastly easier.

wkjagt · 2 months ago
I understood them on a superficial level when first learning about them, but it only really clicked after having done assembly.
dingdingdang · 2 months ago
The legendary status is also enhanced by the absolute nightmare that pointers enable if used with indiscretion or high level proficiency - a triple pointer is a good example for me but there's many many more, and arguably worse, examples out there.
resonious · 2 months ago
As a 16 year old who learned programming by hacking together bad ActionScript3 flash games, it took me way longer than 15 minutes. Once I got it I got it and there was no mystery from then on. But there definitely was a hurdle.
assimpleaspossi · 2 months ago
Same here about pointers. Perhaps it's cause I started life as an electronic engineer and understood memory addressing from the chip level but I, too, don't understand the struggle others seem to have.
ashtonjamesd · 2 months ago
Wow! That really means a lot because I always make a lot of effort to make sure my code is just that :)

Appreciate you saying that!

jacquesm · 2 months ago
You've done a couple of things right: very few dependencies, simple, easy to understand code. C gets hairy when you try to be clever.

I'm busy writing some of the most optimized-but-still-portable code that I've ever written and it is very interesting to see how even a slight difference in how you express something can cause a massive difference in execution speed (especially, obviously, in inner loops). Your code is clearly written from what your comfort zone with C is and I'm really impressed by the restraint on display. At the same time, some of the code feels a bit repetitive and would benefit from more universal mechanisms. But that would require more effort and I'm not even sure if that is productive. One part where I see this is in the argument parsing code as well as in the way you handle strings, it is all coded very explicitly, which substantially increases the chance of making a mistake.

Another limitation is that using AI to help you write the code means you don't actually understand what it does, and this in turn may expose you to side effects that you are not able to eliminate because you did not consider them while writing, it is as if someone else gave you that code and asked you to trust them they did not make any mistakes.

Deleted Comment

codegeek · 2 months ago
People, stop trying to be so serious and nitpick this project. This is a great example of an actual HN worthy share. Someone built a cool project and explored the possibilities with C. This is not something we need to analyze with "oh can it replace PHP" etc.

Good job OP. Now if you can add HTML templating, this may become a complete framework :)

ashtonjamesd · 2 months ago
Thank you, I really appreciate you saying that!

Yes it's on the backlog and will be fun to implement :)

whatamidoingyo · 2 months ago
As someone learning C for fun, I agree. This project is awesome!
hgs3 · 2 months ago
The code is very readable and well organized. My only major critique is that there's very little error checking, e.g. there are many calls to snprintf and malloc without checking the result. There is also an unused loop here [1].

As an aside, I don't see any support for parallelization. That's fine for an initial implementation, but web servers do benefit from threading off requests. If you go that route (pun intended) you might consider using something like libuv [2].

[1] https://github.com/ashtonjamesd/lavandula/blob/51d86a284dc7d...

[2] https://github.com/libuv/libuv

ashtonjamesd · 2 months ago
Thank you for the feedback, it is appreciated!

I did intend to implement parallelization as a later feature so it's good to bring it up.

sroerick · 2 months ago
Hi, I think this is great. I've really enjoyed working with Jetzig, which is sort of similar.

I also love the BSD C CGI Postgres stack. I'm just a CRUDmonkey with mostly python skills, so getting to explore low language and memory concepts is a lot of fun for me.

People will whine and moan about how this is not practical, but as embedded devices become more ubiquitous I think a clear value add may actually emerge.

I've been playing with the pico calc, and if I was building something as a "mobile app" for that I would much rather reach for C for my framework code.

Cheers, great work

lubesGordi · 2 months ago
Well I don't know about others here, but I think its cool. If you can make the setup super readable and get the performance of C then why not? Especially now when you can get claude to write a bunch of the framework for you. Add in whatever you need whenever you need it and you automatically have a platform independent web framework that's no bigger than what you need and likely decently performant.
maybewhenthesun · 2 months ago
Maintainer nightmare checklist:

- Web framework : inherently hard to maintain due to communication over evolving standards. Check.

- AI written code where nobody knows howwhatwhenwhy!? Check.

- Written in C. Check.

bwahahahaha!

edit: semi-joking. As I actually like the simplicity of pure C. But the combination of AI written,network-facing and C makes me shudder.

ashtonjamesd · 2 months ago
Haha, I have used AI in some parts of it - mainly the JSON part because I could not wrap my head around it for the life of me. But I am proud that 90% is self written!
jvanderbot · 2 months ago
I think the old HN ethos that I loved, on full display here, won't survive intact in the AI era. It'll have to change from "It is cool to try making <neat tool> in <non obvious language>". Such a project is now a prompt away, and there's light-years of distance between a carefully hand crafted version and something that is posted aspirationally by an AI.

Every agent I know of or use will always say they built "Production ready, secure, fast package for X" if you ask them to build that, but they rarely actually will. It takes enormous time and effort to actually do that, and any first iteration of "production ready" is definitely aspirational until it actually hits the real world and survives. I'm speaking from experience, fwiw.

levkk · 2 months ago
That's awesome. With macros, you can go far and most modern web frameworks use whatever complex tools their language allows (like metaprogramming in Rails).

Mad props for building this. It's hard and it's fun!

As to other comments in the thread about the "why": why not. For the love of the craft.

ashtonjamesd · 2 months ago
Thank you so much! I appreciate it :) And yes, totally agree.
mistivia · 2 months ago
It's very dangerous to write a http parser from scratch in C. This can be very vulnerable without rigorous testing. To get a useful web framework for production in C, I think it's a better idea to start from libmicrohttpd, libevent_http, or even fastcgi, which are battle-tested.
jacquesm · 2 months ago
I don't think anybody here is going to use this for production, but just in case you're tempted: don't.
wallmountedtv · 2 months ago
I hear this comment warnings, and can easily see this myself being true. But, how could one actually make a reasonably safe http server in C from scratch?

That would honestly sound like an amazing book, just walking through all the ways it's horrible chapter by chapter, and how to structure the code instead, slowly. Like an accelerated history to create such a matured http library.

throwaway2037 · 2 months ago
I like your idea for the book. I hope that Robert Nystrom writes it.
fallingmeat · 2 months ago
wow that’s a lot of HATE for a really well organized project with some great ideas. Killer job Ashton, you just built some skills they can’t take away from you.
ashtonjamesd · 2 months ago
Thank you, that means a lot! :)