Live data from Hacker News

Switching to C over 'Modern' Programming Languages

devtails.xyz

61–70 of 170 posts

Re: Switching to C over 'Modern' Programming Languages

#61
post #51

I used to love C for its simplicity. There was just no surprises, and the limited feature set enforced a certain programming style that also happens to run very well on modern CPUs. But the C standard library is just awful. It's so inconsistent and full of quirks you just have to know. Like how some string functions allow you to specify a size, while others don't. And how strtok keeps track of an internal state and b…

> But the C standard library is just awful.

This is very painfully true, but one 'killer feature' of C is that it is useful without ever using stdlib functions (except basics like memset, memcpy, ... which can be considered compiler builtins anyway).

In more recent languages (even C++) there is no such clear distinction between the language and stdlib any more, which IMHO is a real problem (e.g. most of C++'s problems are actually stdlib problems, not language problems).

Re: Switching to C over 'Modern' Programming Languages

#62
post #36
post #16

Is this becoming a thing now (or maybe it was always a thing), where a language, like Rust, has become popular enough that instead of everyone talking about learning it, they now want to talk about how “simple” and “beautiful” C is for no other reason than signaling how different you are from the zeitgeist? Rust exists for a reason, and it solves specific problems. That’s the “magic”, just like any abstraction in any…

I'm a C programmer by day and I disagree with them, C is only simple if you're trying to do simple tasks with it. The very common thing I want to use in C is some sort of variable size string object. But no, I have to dynamically allocate a buffer that I know will be at least the right size for any text I ever put into it, or do I create a buffer that's the correct size for that string but re-alloc if I ever change i…

I have something like this that serves me well:

  struct strbuf { size_t cap, len; char *str; };
  void sb_setf(struct allocator *a, struct strbuf *sb, const char *fmt, ...);
  void sb_appendf(struct allocator *a, struct strbuf *sb, const char *fmt, ...);
  // have other convenience functions for formatting fixed point values like "prefix AAA.BBB suffix" ("voltage: 7.23 V")
  // special helpers for dates, times, etc.
Just keep building that library up and you'll have growable buffer, strings, lists, hashmap (uintptr -> uintptr is all you need in 99% of cases I've found, maybe some helper functions for string key -> void * built ontop of uintptr->uintptr) + replace/rewrite the standard library to operate on these types instead and you're good to go.

Re: Switching to C over 'Modern' Programming Languages

#63

I've been learning Rust lately. Working on a little game. First few thousand lines of code in I was having some doubt. After being a little frustrated with borrows and lifetimes and with my strong tendency to prematurely optimize all the things I was soooo tempted to just switch to C and have full unfettered access to my sweet sweet pointers. I'm glad I haven't switched though. Performance (which I'm measuring a ton…

"Rust forces me to think a little bit more [..]" My reaction to that has always been that it forces us to think about the things we would have had to think anyways if we wanted to create reasonably reliable and secure software. So in the end if saves work, even if it doesn't appear like that at first.

The other side of the medal (especially for game development), is that in the higher level parts of game code you need quick turnaround times for experimentation and tweaking, and most of that code won't even make it into the final product (so all the upfront time you spent thinking about proper architecture and ownership details is wasted up there).

The traditional solution is to have different languages for different code layers (e.g. a compiled language for the low level parts, and an interpreted scripting language for the high level parts), but this comes with its own set of problems.

Re: Switching to C over 'Modern' Programming Languages

#64

The author appears to be quite green behind the ears, which is fine! They’re certainly doing themselves a disservice by being so sure of themselves. There’s a lot here that feels right in the pocket of Dunning-Kruger ignorance. Being able to analyse the benefits of new tech at a distance (which is what the author is doing with their sterile toy projects) is not something that you can “fake until you make it”. It requ…

> They’re certainly doing themselves a disservice by being so sure of themselves

I didn't find the OP to be sure of themselves. See e.g.:

"Admittedly, this would slowly go away as I gained more experience with the language."

"This is likely because I haven’t spent enormous amounts of time in C/C++"

"I will probably come out on the other side with a lot of the same feelings about C as other languages."

"Maybe I will find the complicated syntax and rules of Rust are worth it."

Re: Switching to C over 'Modern' Programming Languages

#65

Earlier quoted context omitted.

The difference is that a CPU does way less magic under the hood than an optimizing compiler for a high level language, and there's no such thing as UB in assembly (outside some exotic cases like 'illegal' instructions on the 6502 which behave unpredictably)

Meltdown/Spectre pulled back the curtain and revealed just how much magic is going on with modern CPUs.

But it only really manifests as timing.

Re: Switching to C over 'Modern' Programming Languages

#66

Earlier quoted context omitted.

This is silly. Assembly is an abstraction too. Do you think the CPU actually runs the code you give it? Of course not. There's microcode, register aliasing, speculative execution, etc. Even assembly itself has symbols and labels, which are themselves abstractions.

Did you really not understand the point the GP was trying to make or is this an attempt to be clever? Of course assembly is an abstraction, but it is the lowest level programming language that ordinary mortals can still write code in, which was the point the GP was making. Exactly nobody writes applications in microcode. Register aliasing and speculative execution have under normal circumstances no effect other than…

If you’re trying to understand the behaviour of the computer then all of the layers of abstraction matter, right down to the logic gates. Spectre and Meltdown proved that software developers can’t just “trust the CPU to do the right thing.”

Besides the issue of security, performance is also a thing. If you’re trying to squeeze every last cycle out of your program then you need to understand CPU cache hierarchies at the very least. That’s at a level far below assembly language, digging down into the physical layout of the machine.

Re: Switching to C over 'Modern' Programming Languages

#67
post #66

Earlier quoted context omitted.

Did you really not understand the point the GP was trying to make or is this an attempt to be clever? Of course assembly is an abstraction, but it is the lowest level programming language that ordinary mortals can still write code in, which was the point the GP was making. Exactly nobody writes applications in microcode. Register aliasing and speculative execution have under normal circumstances no effect other than…

If you’re trying to understand the behaviour of the computer then all of the layers of abstraction matter, right down to the logic gates. Spectre and Meltdown proved that software developers can’t just “trust the CPU to do the right thing.” Besides the issue of security, performance is also a thing. If you’re trying to squeeze every last cycle out of your program then you need to understand CPU cache hierarchies at t…

I'm well aware of all of those levels. But they are not germane to the discussion about programming in general. They are important if you are doing some really specialist work (low latency, extreme performance, operating system design). But that wasn't the context at all.

Re: Switching to C over 'Modern' Programming Languages

#68
post #9

Don’t expect to see immediate benefit from Rust by writing a small Pong application. Rust gives you confidence at scale: the ability to depend on many 3rd party blocks without compromising stability or performance, the ability to grow and maintain the code, collaborate on it, etc. Nothing of this is easily seen on a small self-contained program you can write in C.

I still cannot figure out how to easily write small applications with Rust on low resource computers, like the one I'm typing this from. (Limited storage, CPU, memory.) It seems even the smallest programs require a massive toolchain. The default reliance on network connection for compilation is offputing. It's vastly easier for me to write small programs, offline, with C. The rationale for using Rust over C that I se…

"offputing" for "offputting" is a great typo, as it might mean "computing on a platform that's not on the local host"

Re: Switching to C over 'Modern' Programming Languages

#69
post #16

Is this becoming a thing now (or maybe it was always a thing), where a language, like Rust, has become popular enough that instead of everyone talking about learning it, they now want to talk about how “simple” and “beautiful” C is for no other reason than signaling how different you are from the zeitgeist? Rust exists for a reason, and it solves specific problems. That’s the “magic”, just like any abstraction in any…

>they now want to talk about how “simple” and “beautiful” C is for no other reason than signaling how different you are from the zeitgeist?

Given that this has been an argument for 3 decades or so, e.g. compared to C++ especially, I don't think so.

Even more so since C is not just some trendy language you pick up quickly, but needs quite a lot of time and effort to be profficcient in, to the point of appreciating its simplicity and portability/stability/etc benefits.

>Rust exists for a reason, and it solves specific problems.

Rust exists because its creators had a reason such a language was needed in mind. Doesn't mean others necessarily share it, or if they do, that they see Rust as the solution to the problem behind that reason.

Re: Switching to C over 'Modern' Programming Languages

#70
post #36

Earlier quoted context omitted.

I'm a C programmer by day and I disagree with them, C is only simple if you're trying to do simple tasks with it. The very common thing I want to use in C is some sort of variable size string object. But no, I have to dynamically allocate a buffer that I know will be at least the right size for any text I ever put into it, or do I create a buffer that's the correct size for that string but re-alloc if I ever change i…

I have something like this that serves me well: struct strbuf { size_t cap, len; char *str; }; void sb_setf(struct allocator *a, struct strbuf *sb, const char *fmt, ...); void sb_appendf(struct allocator *a, struct strbuf *sb, const char *fmt, ...); // have other convenience functions for formatting fixed point values like "prefix AAA.BBB suffix" ("voltage: 7.23 V") // special helpers for dates, times, etc. Just keep…

That works until you want to use code from somebody else who also has something like that that serves them well, but is slightly different.

If you’re extremely lucky, things will compile and work.

If you’re just lucky things won’t compile, and you’ll have to write conversion functions (or macros).

If you’re unlucky, there will be subtle differences, likely poorly documented, between the libraries, and code will compile but have subtle bugs.

Of course, other languages have that problem, too, but at the higher level of json parsers or graphics libraries, not at the basic level of strings, lists, or maps.

Post reply on HN