Live data from Hacker News

John Carmack on mutable variables

twitter.com

351–360 of 663 posts

Re: John Carmack on mutable variables

#351

Agree. After working seriously on a large production Haskell codebase for several years I definitely took it for granted. Now that I’m writing stuff in C again I do think immutability should be the default. const isn’t really it though. It could go further.

Well in C actually you can not mutate something, you can only reassign, as it is always pass-by-value. You need to work around that, by passing a pointer to the object instead. In that sense mutability is kind of a language keyword: '&'. When you want to just get the object, you pass object it, if you need to modify it, you need to pass &object. This is something I hate in C++, that random function invocations can mutate arguments without it being obvious in the call syntax.

Re: John Carmack on mutable variables

#352
post #296

I had enormous fun optimizng C++ via self-modifying assembly to squeeze the utmost performance of some critical algorithms, and now this drive towards immutable everything feels like cutting my hands and legs off and forcing me to do subpar engineering.

Compiler optimizations make up for it, usually. Thats been my experience at least.

An important thing to keep in mind is just how far compilers have come over the least 40 years. Often, with immutable languages, they can do extremely efficient compile only optimizations because of said guaranteed immutability by default.

Its not true in every case, of course, but I think for most situations compilers do a more than good enough job with this.

Re: John Carmack on mutable variables

#353
post #226

Earlier quoted context omitted.

ML superfan here. I don’t mind lisp simplicity either. Erlang is too alien for me, but maybe once I was used to it.

If you start programming in it though, syntax only matters during the first day. Familiarity comes very fast, and if you do five programming exercises, maybe one a day, 'implement a hash map', 'make a small game', etc. you will have no problems whatsoever once the week is done. If you have a course where one day you're supposed to do Haskell and another Erlang, and another LISP, and another Prolog, and there's only o…

I disagree, at least for my own case. I greatly prefer reading ML code than C style syntax.

Re: John Carmack on mutable variables

#354
post #96

Yeah I wish variables were immutable by default and everything was an expression Oh well continues day job as a Clojure programmer that is actively threatened by an obnoxious python take over

You are not a Clojure programmer. You use Clojure to solve problems in a professional context. I'm sorry that there's a political tribal war based on language going on at your workplace.

But especially now that coding agents are radically enabling gains in developer productivity, you don't need to feel excluded by the artificial tribal boundaries.

If you haven't, I recommend reading: https://www.kalzumeus.com/2011/10/28/dont-call-yourself-a-pr...

Re: John Carmack on mutable variables

#356
post #150
post #115

Variable is by definition mutable. Constant is by definition immutable. Why can't people get it through their heads in 2025? (I'm looking at you, Rust)

A constant is a variable that _always_ has the same value on all lifecycles, e.g. const int a{10}; An immutable variable well ... does not void some_function(const int a); Can you tell me what is the value of `a` on both cases?

> A constant is a variable...

No, it is not. A constant is the direct opposite of a variable.

> An immutable variable..

There is no such thing. You can decide not to mutate it but variable is by definition mutable.

If you want to argue mutability, then you have to talk about the data structure or memory footprint of the constant or variable that it points to or represents, not the concept of variable or constant itself.

In other words, we can have var foo = 5 and const bar = 5. foo can be changed by being reassigned another value with simple foo = 6, whereas bar cannot as bar = 6 should cause panic/exception/... On the other hand, we can have var foo = {value: 5} and const bar = {value: 5} and now it depends on the language how it handles complex types like a struct/object as the operation now bypasses the guards on the variable/constant assignment itself. Will it guard against mutation or not? It should, but that is rarely the case. Hence, in most languages, we will be able to do foo.value = 6 and also bar.value = 6, even though we should not. But again, now we are arguing about the mutability of the data type or memory representation and not the variable/constant itself. Most languages don't care about mutability, so we have this flawed thinking where we are simply unable to strictly define what data is actually mutable and what data is not. Rust uses the borrow checker, that is one approach, but generally this should be properly handled by the language spec and compiler itself and we should not even have this conversation where programmers simply cannot make a distinction between variables and constants, let alone comprehend what those terms mean in the first place, as those meanings have been thrown out of the window by the folks designing the languages.

Re: John Carmack on mutable variables

#357

In python its common to see code like this: df = pd.concat(df,other_df) df = df.select(...) ... My eyes hurts, when I see it. It makes me avoid python. I bet the reason for this mutable code is a missing simple pipes syntax. I love pipes. In R can do: df |> rbind(other_df) |> select(...) It feels much better.

My eyes would hurt more if I had to look all day at the vertical misalignment of that |> operator

You could always switch to a better font like Fira Code which has a ligature for this.

Re: John Carmack on mutable variables

#358
post #296

I had enormous fun optimizng C++ via self-modifying assembly to squeeze the utmost performance of some critical algorithms, and now this drive towards immutable everything feels like cutting my hands and legs off and forcing me to do subpar engineering.

Same here, I grew up in a world where you had a handful of registers and a bunch of memory locations, and those were your variables. clc lda value adc #1 sta value lda value+1 adc #0 sta value+1 value .byte 0,0 These constraints are pretty much built into my concept of programming and it would take great effort to break out of it. It feels nice doing: x = 20 x = x + func(y) x = x / func(z) And it would feel weirdly w…

To me this also seem to be wasteful, even if not for the computer, but it wastes my amount of working state I can keep in my head, which is very limited.

Re: John Carmack on mutable variables

#359
post #263

Earlier quoted context omitted.

In my IntelliJ (a recent version), if I write a small Java function like this: private static void blah() { final int abc = 3; for (int def = 7; def The variable 'def' is underlined. Mouse-over hint shows: 'Reassigned local variable'. To be clear, 'abc' is not underlined. When I write Java, I try to use the smallest variable scopes possible with as much final (keyword) as possible. It helps me to write more maintaina…

1st) you use ++def in a loop, don't be weird; 2nd) if 'abc' is to be used in the loop body, define in the loop, e.g. for (int def = 7, abc =3; ...); 3rd) this is an IntelliJ bug - both 'def' and 'abc' in the sample are always defined.

the only thing that is weird is your lack of understanding temporary variables

Re: John Carmack on mutable variables

#360
post #272
post #12

How fast this got to the top, you would think John Carmack just invented nuclear fusion.

People worship this guy, but other than being a good C++ graphics programmer, it isn't clear what he's actually done.

His engines are open source, and graphics are far from the only interesting thing about them. If you don't know what he's done that's on you; it's no secret.
Post reply on HN