Ask HN: Modern Alternatives to C

17 points by leakbang ↗ HN
Recently with a group of friends I decided to start creating a web browser from the ground up using as few libraries as possible and I wanted to do it in C. I persuaded everyone to learn and start using C, because I personally had a sweet spot for C and I've seen many great applications written in C so I wanted to pay homage to the great developers who coded great software in C.

But none of us were familiar with C and graphical libraries such as Xlib and Cairo so it was and is a big learning experience for all of us. However, learning C and Xlib has become a massive hurdle for us. The problem is that C is a remarkably old language and the available learning resources are very old. Most of the information in books have become obsolete as standards and libraries have changed and and updated. The C communities have turned into ghost-towns with the latest posts being from several years ago. And worst of all, an unbelievably large chunk of C code on the internet cannot be compiled due to changes in standards and changes in libraries.

I think while it was a novel attempt to try to do the whole project in C with the bare minimums, it is very unproductive. It took out the confidence and fire in all of us so now I'm looking at some of the modern languages as a replacement for C. I still would like to keep it relatively low leveled and be in control. From my knowledge GO and Rust seem to be a modern take on C.

GO and Rust have caught my attention because they seem to have an active community, both allow the programmer to be in charge, and they perform quite well.

I'm asking you for advice and really like to hear your thoughts. Do you think Rust or GO would be suitable? Do you recommend other languages? What do you think?

I am interested to read your thoughts.

47 comments

[ 1.8 ms ] story [ 36.3 ms ] thread
What about Zig?
I just quickly looked at it. It seems to be nice by a quick glance. I will look at it in depth. Thank you for your suggestion :)
I consider Rust and Go to be the general “modern alternatives to C”.

Go afaik is more focused on simple applications, while Rust is better for large-scale apps or if you need unique performance, memory management, or security.

However Rust is hard to use/learn and verbose. It’s a steep learning curve so you risk getting burnt out. IMO you need to understand low-level systems programming and some type theory to be good at Rust.

If you want other alternatives, you might also be better off just using Kotlin/Java. It’s not as performant but it’s not terrible (e.g. JavaScript), and the good tooling, runtime checks, and reflection come in handy. .NET in C# is another alternative but i’ve never used it.

Or you could use Swift, which seems to be performant like Rust/Go but still easy to learn/use and similar to Kotlin. The only issues are that afaik Swift support on anything not Apple is hard, you basically need Xcode or AppCode, and there isn’t as big of an ecosystem.

ETA: I personally would choose Go but don’t build something too large-scale, try to simplify as much as possible.

Thank you for your sound advice. I'll definitely consider what you said and will think carefully about it.
Take a look at the Crystal Programming Language - “Slick Like Ruby and Fast Like C” is goal of the project.

https://crystal-lang.org/

This looks interesting. I'll look more into it. The only thing that concerns me is the overhead and the automatic garbage collector. But GO has those as well.

Thank you for your recommendation.

Most of what makes a modern browser slow and bloated is the Javascript, which has garbage collection. You're not getting away from that if you want to develop a usable browser.
Does it have good support for writing GUI applications?
Why not consider D?
Just come here to say the same thing. D is a natural successor to C and C++, the only main thing that it lacks at the moment is the adoption and popularity.

Personally I think D is not only suitable for systems programming but it also very much suitable for modern data science programming as I commented recently in the other discussions on C++ Matlab like library topic [1].

[1]https://news.ycombinator.com/item?id=31080759

I never thought of it, mainly because I've never seen it anywhere.

But I did quickly glance and it and it looks very interesting. Although I will take a closer look to see how active the community is, and the documentations too.

Thank you for the recommendation.

Rust and Golang are suitable.

If you’re willing to consider esoteric languages, they Ada. It’s made with security in mind, so might be appropriate for a web browser.

There’s an Ada HTTP server that might be a good starting point. It’s called Ada Web Server, or just AWS for short.

The Ada community is cursed with terrible acronyms (AWS, GPS, SPARK, and Ada).

I'll look at Ada closely. After a quick glance, the activity done in Ada seems concerning but I'll take a good look at it. Thank you for your recommendation.
C++

I don't think Go or Rust has the mature graphics APIs you'd need

You are right. Although I believe almost all of the popular graphics libraries has bindings for Go & Rust.

C++ is definitely powerful for this use-case and I believe it has a very active community as well.

Thanks for the recommendation.

Either Go or Rust would be great. They're both good alternatives to C. D, Zig, Crystal, Nim are other ones you might consider.

An alternative to these that I use for hobby projects, is Ada. It's weird and most people laugh at me for using it. It's super simple (you can learn it in about a month), and it writes like a more strongly-typed and safer, Pascal version of C. It also has built-in concurrency and provides low-level control. I did a presentation at FOSDEM to give an overview of the language since most people don't know much about it (https://fosdem.org/2022/schedule/event/ada_outsiders_guide/).

There's a tool called Alire (https://alire.ada.dev/) that will install the toolchain for you and simplifies building and running. The open source community is super tiny, probably 1/10th or smaller of that of the Rust or Go communities. However, since the language has been around and evolving for decades, it's surprisingly easy to get answers to questions, since there's quite a few very experienced folks around. Though it's easy to make bindings, there's relatively few available. However, since the language's open source scene is new compared to the rest of the language, it's a place you and your friends could make a big impact.

Thanks a lot for your sound recommendation. I will take a close look at Ada, and I will watch your presentation too. It looks very promising.
Another to add to the group is Vlang (https://vlang.io/), as has strong interop. It has the focus of being an alternative to C/C++/Go.
You want to create a web browser from the ground up and C was a massive hurdle for you?

Oh boy.

C itself wasn't the problem but rather learning to use Xlib and Cairo was very difficult. A massive amount of code that I found on the internet was un-compilable. The documentation is extremely old and I was relying a lot on taking already existing code and playing around with it until I learn the intricacies.
Any program written using Xlib will be tied to X Window.

Xlib is very low level.

Changing languages won't solve the problem. E.g. if you have Rust or Go bindings for Xlib, Xlib is still Xlib.

There are a lot of requirements going into a browser. Think about, oh, rendering HTML + CSS: the whole ultra-complicated model. Fonts. Javascript engine. Handling a myriad image formats. Video. TLS security. Javascript engine!

Xlib is the least of your problems.

Rust and C++ are the best alternatives for systems programming languages that have the same performance characteristics as C. Go's performance is similar to Java and C# due to its runtime and garbage collection. Maybe that's fine for your use case, but that places it on a different part of the spectrum than C, C++, and Rust.
If I wanted to create a web browser from the ground up, I would do that in C#.

And I would not use graphical libraries such as Xlib or Cairo, none of them has good hardware support. I think the right way is building on top of 3D GPU APIs, like I did there: https://github.com/const-me/Vrmac#vector-graphics-engine

I agree with you that using a GPU accelerated API has many more benefits. I was trying to learn Xlib and Cairo to learn the 'nuts and bolts' of rendering and also because they didn't require other libraries. I will look both at C# and GPU accelerated APIs. My only concern with C# is the cross compatibility. Thank you for your recommendations.
> to learn the 'nuts and bolts' of rendering

These nuts and bolts are very different between CPU and GPU. CPU-based libraries are painting pixels in bitmaps in system memory. Most GPU-based libraries are uploading indexed triangle meshes, and rendering them with weird shaders.

Worse, there're no good open source implementations of GPU-based ones. Microsoft ships an implementation as a part of OS (Direct2D) but it's not open source. Linux simply doesn't have an equivalent.

At least for initial versions, consider C interop with this https://github.com/memononen/nanovg It cuts a few corners (no cleartype for text, CPU overhead for repeated rendering of same static paths) but it's still good overall, simple, and easy to use.

> My only concern with C# is the cross compatibility

Works well on Linux, Windows and OSX, including ARM CPUs. Not sure about Android and iOS, never tested.

My largest concern with C# would be performance. Technically the language allows to code in any style, but most guides and examples are using OO-heavy one. Less than ideal for performance-critical pieces where you'd want cache-friendly RAM layout i.e. mostly value types, minimize dynamic memory allocations (e.g. lambdas are using managed heap when created), and so on.

My largest concern with the project overall would be the scale. Browsers are incredibly complicated. In C# the standard library contains many important pieces (HTTP stack, WebSockets, XML, etc.), you'll definitely find good free libraries to deal with HTML and CSS, you'll probably find libraries to interpret or JIT-compile JavaScript, but still, the remaining pieces are huge.

> Worse, there're no good open source implementations of GPU-based ones. Microsoft ships an implementation as a part of OS (Direct2D) but it's not open source. Linux simply doesn't have an equivalent.

What about OpenGL? I believe that the Firefox/Servo WebRender (written in Rust) also uses OpenGL. But I do believe that the GPU acceleration implementation is more complete on Windows and Mac OS.

> you'll definitely find good free libraries to deal with HTML and CSS, you'll probably find libraries to interpret or JIT-compile JavaScript, but still, the remaining pieces are huge.

I really want to not use libraries as much as possible and do everything on my own. It won't be good as the libraries but I see a lot of value in doing everything myself and learn it.

> What about OpenGL?

Yeah, GL is good now. Vulkan too, when supported by GPU driver. However, both GL and VK can only render triangle meshes. Browsers don't quite need meshes (except WebGL but that's another story), they need 2D graphics and text.

On Windows, browsers and most other modern apps are using Direct2D and DirectWrite. They are OS-provided libraries which render 2D graphics and text with triangle meshes using Direct3D 11 underneath.

While modern GL is comparable to D3D11 (and VK comparable to D3D12), there's no equivalent libraries on Linux, i.e. you'll need to do that part on your side, instead of consuming OS-provided libraries. These parts are far from being trivial.

> I really want to not use libraries as much as possible and do everything on my own

OK but at least you'll be able to look how other people did that.

Also the standard library is a part of the language runtime, no reason not to use it. In C# it contains quite a few complicated low-level pieces, like network protocols and cryptography.

How is anything in C# "from the ground up"?
I thing the OP wants to implement a browser engine from scratch, without forking Chrome or Firefox.
I think C is the best option which is simple, fast and powerful. You need to introduce a separation layer to decouple your core implementation from the graphics, networking... libraries. This makes porting to other OSes or switching to the other libraries easy.
Zig - most portable modern C alternative, you can consume C/C++ with ease, so if there are C/C++ libraries that'll help you kickstart your idea, you'll just need to include the headers, that's it

Comptime feature is unmatched, it's not something weird as macro, you don't need to learn a new language on top of the language itself, it is zig code

You can crosscompile with EASE to what ever target you want

My 2nd choice would be D

Rust? you'll cry when you'll have to wait 10+ minutes to compile a single line change, and the code becomes quickly unmanageable, the community is sectarian, elitist, and will hate you for criticizing their "god" language

Go? best language for servers, not so good for anything that require low level

> Rust? ... the community is sectarian, elitist, and will hate you for criticizing their "god" language.

That is very unfortunate.

I have been looking into Zig since you and a few other people have suggested it. It is looking more promising the more I look into it. It looks like a pretty robust language with an active community.

There are more out there than Zig. There's Vlang and Odin as well.

D is something to look at, but I'm wondering if the opportunity passed it by and it has become almost as wide/complex as C++. I never quite understood why D didn't go more heavily into application development, like Object Pascal, as a lane it could make more of a name for itself. Hard to be kind of similar to C++, going for same usage, and then to displace it. C++ is just so established and people so invested in it.

In the case of Zig, Vlang, or Odin, these are clearly more about being a step higher than C, but ease of use as well.

Free Pascal with Lazarus. Sounds weird but you can do pretty low level programming (as with C) with Free Pascal including using assembly language, if needed. And you can compile into a single executable.

Also, since it already includes a UI and also a web browser component which you can extend/modify, it would be trivial to get started with developing the nuts and bolts of the Web browser

I really would want to develop everything from scratch and not use preexisting libraries. But Pascal seems interesting. But a concerning thing would be an active community and active open-source projects. I'll take a closer look at pascal.
There are tons of downloadable components and quite a lot of people who still use it. If you need help with getting started, do let me know and I can walk you through an introduction which will save you a lot of time and get you going pronto.
Thank you very much for your kind offer :) but I already made my choice and picked up Zig. It was exactly what I was looking for. A modern C. No hidden preprocessor, no macros, no hidden control flow, and most importantly, it has an active community.
Thank you for the update. Wishing you the best!
People sleep on Object Pascal to the extent it's almost comical. Not sure why more people refuse to get over the begin...end versus {...} bias or hypocrisy (since some will jump all over Python-like or Ruby-like syntax). A lot of what people want their language to become, is what Object Pascal already is.
Does anyone have a good reference for C as it should be used in 2022? It seems all the references out there are backwards facing and/or decades old.
[1]: The C Programming Language (2nd Edition)

[2]: POSIX.1-2017. https://pubs.opengroup.org/onlinepubs/9699919799.2018edition...

[3]: The Linux man-pages project. https://www.kernel.org/doc/man-pages/

[4]: Apple Documentation Archive. https://developer.apple.com/library/archive/navigation/

[5]: Windows API index. https://docs.microsoft.com/en-us/windows/win32/apiindex/wind...

The age of the documentation has absolutely nothing to do with using the software. I highly encourage developers to get rid of this mindset. Generally it comes from younger developers who have less exposure across the board. Senior developers should be comfortable reading standards documents and knowing that they will be years old. They are old for a reason. They stood the test of time.

Read your man pages. Eat your vegetables.

Is Go really suitable…? I love Go, but so far I haven’t seen any desktop applications written in it. I guess that’s for a reason?

BTW, if you want some inspiration, have a look at the browser bundled with SerenityOS. It’s written in C++.

https://github.com/SerenityOS/serenity/tree/master/Userland/...

https://www.youtube.com/watch?v=Gbvhmt9EdfI

> Is Go really suitable…? I love Go, but so far I haven’t seen any desktop applications written in it. I guess that’s for a reason?

I have deviated away from both Go and Rust. Some people in this thread have recommended the Zig programming language and it looks very promising. Right now I'm experimenting with that.

> BTW, if you want some inspiration, have a look at the browser bundled with SerenityOS. It’s written in C++.

This is so awesome, thanks for sharing it. I'm going to love checking out the video and the source code!

One similar thing I know about is a Rust port of the Netsurf browser [1] to Redox OS [2] an OS written in Rust. But this is even better!

[1] https://www.netsurf-browser.org/

[2] https://en.wikipedia.org/wiki/Redox_%28operating_system%29

You could use Go, and there is fyne for cross-platform GUIs, but I think part of the issue is not having a RAD IDE (like say Lazarus or Delphi as in Object Pascal). There is the convenience factor of using Go to build such, and then there is also the direction of where Google takes the language (which isn't about desktop apps).
Another esoteric choice would be the Odin Programming Language [0]. While being very simple it is modern and effective with some real projects built using it.

Performance wise it is at the very top with other low level languages.

[0]: https://github.com/odin-lang/Odin

This seems to be very interesting. I will take an in depth look at it. Thank you for recommending this!