Top
Best
New

Posted by azhenley 10/31/2025

John Carmack on mutable variables(twitter.com)
515 points | 627 commentspage 5
sgarland 10/31/2025|
Dumb question from a primarily Python programmer who mostly writes (sometimes lengthy) scripts: if you have a function doing multiple API calls - say, to different AWS endpoints with boto3 - would you be expected to have a different variable for each response? Or do you delete the variable after it’s handled, so the next one is “new?”
ForHackernews 10/31/2025||
If they're representing different data from different API calls, yeah, I'd be strongly inclined to give them different names.

    order_data = boto.get_from_dynamodb()
    customer_data = boto.get_from_rds()
    branding_assets = boto.get_from_s3()
    return render_for_user(order_data, customer_data, branding_assets, ...)
TZubiri 10/31/2025|||
I think renaming an old variable is a common and sensible way to free a resource in python. If there are no valid names for a resource it will be garbage collected. Which is different in languages like C++ with manual memory management.

John Carmack is a C++ programmer apparently that still has a lot to learn in python.

sswatson 10/31/2025|||
In the vast majority of cases, developer ergonomics are much more important than freeing memory a little earlier. In other scenarios, e.g., when dealing with large data frames, the memory management argument carries more weight. Though even then there are usually better patterns, like method chaining.

FYI John Carmack is a true legend in the field. Despite his not being a lifelong Python guy, I can assure you he is speaking from a thorough knowledge of the arguments for and against.

TZubiri 10/31/2025||
>developer ergonomics are much more important than freeing memory a little earlier

Preach to the python choir bro, but it should be telling when a python bro considers it's too ergonomic and wasteful.

At some point being clean and efficient about the code is actually ergonomic, no one wants to write sloppy code that overallocates, doesn't free, and does useless work. To quote Steve Jobs, even if no one sees the inside part of a cabinet, the carpenter would know, and that's enough.

tl;dr: Craftmanship is as important as ergonomics.

sswatson 11/1/2025||
In this case, overuse of re-assigning is the sloppy thing to do, and immutability by default is the craftsman's move. Reducing your program's memory footprint by re-assigning variables all the time is a false economy.
TZubiri 11/1/2025||
So if you are preparing a 50Kb webpage, and you do 10 steps of processing, you would have a 500KB memory footprint that might be held during the life of the connection? All the while the footprint and thus capacity of your server could have been 100Kb? Nice craftmanship dude!

We are not even talking about in-place algorithms, just 10 functions that process an html into a new array, maybe:

html = load_template(route) html = formatstring(html,variables) html= localize_paths(html) ...

And you would rather have it:

template = load_template(route) formatted_html = formatstring(template,variables) html_with_localized_paths = localize_paths(html)

And you would rather have the latter? For what gain? I think you wouldn't.

"Only a sith deals in absolutes", you have to recognize that both are valid under different contexts. And I'm merely explaining why inmutable is the default in python, 1: python doesn't do programmer self restrictions like const and private; 2: memory is automatic, so there's no explicit allocation and freeing like in C++, so using a new variable for each thing isn't a zero overhead abstraction.

Even for smaller cases, (not 50kb arrays), it's still the proper thing to do, although you have freedom to choose, it's easier to just follow one style guide and protocol about how to do things, if it's pythonic to reuse the variable name, just reuse the variable name. Don't fall for the meme of coming from another language and writing C++ in python or Java in python, you are not a cute visionary that is going to import greatness into the new language, it's much better to actually learn the language rather than be stubborn.

There's places where you can be super explicit and name things, if it's just an integer then it's a very cheap comment that's paid in runtime memory instead of in LOC. But this is why the default in python is not const, because variable name reuse is a core python tactic, and you are not our saviour if you don't get that, you are just super green into the language.

maleldil 10/31/2025||||
Not "a resource", but memory specifically. If there's a proper resource (e.g. a file), you should ensure it's explicitly released instead of relying on the GC (using with/close/etc.) And if memory usage is really important, you should probably explicitly delete the variable.

Anything else is wishful thinking, trying to rely on the GC for deterministic behaviour.

blueside 10/31/2025||||
if this guys learns enough, who knows, he may have a future in programming!
ForHackernews 10/31/2025|||
But wouldn't you do that inside a function or a loop body?
busfahrer 10/31/2025||
In TFT, he mentions

> [...] outside of true iterative calculations

0xedd 10/31/2025||
[dead]
zahlman 10/31/2025||
> and it avoids problems where you move a block of code and it silently uses a version of the variable that wasn’t what it originally had.

I find that keeping functions short also helps a ton with that.

No, shorter than that. Short enough that the only meaningful place to "move a block of code" is into another function. Often, by itself.

williamdclt 10/31/2025|
It helps with that, but it has other trade-offs: indirection isn't free for readability.
throw10920 10/31/2025|||
I'll go further than that and say that indirection significantly increases cognitive load and hurts readability.
zahlman 10/31/2025||
I have consistently found the opposite to be the case across decades of programming experience, as regards the extraction of helper functions. This is not "indirection" in the same sense as with data structures. It is abstraction of currently-irrelevant detail.

Reading and understanding code is a process of answering "what are the immediate steps of this task?", without thinking about what those steps consist of or entail. It is not a process of answering "where is the variable representing ..., and the code that manipulates this?", especially since this makes assumptions that may prove incorrect.

tengbretson 10/31/2025|||
> indirection isn't free for readability

Yes, but also no. If its a mostly side-effect free function with a good name and well defined input/output types its basically free.

AaronAPU 10/31/2025||
This would require coming up with an order of magnitude more variable names which is just unnecessary cognitive load.
furyofantares 10/31/2025||
An order of magnitude? That sounds like pretty outrageous hyperbole. A variable getting reassigned 10 times sounds extremely rare, the average in my experience has to be less than 1 reassignment. I think the approach requires coming up with maybe 10% more names.

Usually there are good, obvious names for intermediate calculations in my experience.

I'm open though - what kinds of things are you doing that require reassigning variables so much?

AaronAPU 10/31/2025|||
Probably exaggerated a bit with that phrasing (“outrageous” seems similarly hyperbolic ;))

But any variable which I’ve not already marked as const is pretty much by definition going to be modified at least once. So now instead of 1 variable name you need at least two.

So now the average number of variables per non-const variable is >= 2 and will be much more if you’re doing for example DSP related code or other math heavy code.

You can avoid it with long expressions but that in principle is going against the “name every permutation” intention anyway.

furyofantares 10/31/2025||
Fair enough re: "outrageous"!

It's actually math heavy code (or maybe medium heavy?) where I really like naming every intermediate. fov, tan_fov, half_tan_fov, center_x, norm_x

AaronAPU 10/31/2025||
I spent a decade or so working on video codecs with an international team, and there was sort of an unwritten rule that code shouldn’t have comments and variable names shouldn’t be descriptive (english language couldn’t be assumed).

Which sounds really awful, but after a while it forces you to parse the logic itself instead of being guided by possibly-out-of-date comments and variable names.

I now prefer less verbosity so that probably explains why I’m a little out of distribution on this topic.

If you looked at any of my code prior to that job, it was the polar opposite with very “pretty” code and lengthy comments everywhere.

1718627440 11/1/2025||
This is kind of the opposite of LLMs, they seem to derive meaning mostly from the variable names, not from what the code actually does.
AaronAPU 11/1/2025||
I haven’t noticed that, will have to keep an eye out. Could help explain some quality inconsistencies.
chongli 10/31/2025|||
No, not at all. You still have the advantages of scopes, name shadowing, namespaces, and collection types. If your language supports them, you can also use algebraic data types to further reduce the number of names you need to deal with.
jstimpfle 10/31/2025||
Either you don't understand what you're talking about, or you've missed the word "strive" in the tweet.
acdbddh 11/3/2025||
related quote from Carmack:

> "A large fraction of the flaws in software development are due to programmers not fully understanding all the possible states their code may execute in."

https://web.archive.org/web/20201229045320/https://www.gamas...

And variable mutations increase the cognitive load necessary to understand all the possible states.

carabiner 10/31/2025||
Does anyone have any real naming conventions, patterns for doing this in ds programming in notebooks? I've got a bad habit of doing:

  df = pd.read_excel()
  df = df.drop_duplicates.blahblah_other_chained_functions()
  [20 cells later]
  df = df.even_more_fns()
AtNightWeCode 10/31/2025||
I care less and less about things like this. At some point you will write code in some lang that have fancy keywords and stuff gets mutated anyway. Also, what people tends to do if stuff is immutable is that they hide mutation by doing deep copies with changes.
Havoc 10/31/2025||
Good point. Had never occurred to me that keeping steps help debug. Obvious in hindsight
Sammi 10/31/2025|
For the same reason almost all my functions end with this:

  const result = ... ;
  return result;
I know debuggers can show return values, but that doesn't help when you're just doing a quick console log inspection.
mcv 10/31/2025||
I use Javscript mostly. Or Typescript actually, these days. I remember when ES2015 introduced `let` because `var` had weird scoping issues. But ever since, I barely use either of them. Everything is `const` these days, as it should.
andsoitis 10/31/2025||
> I use Javscript mostly. Or Typescript actually, these days. I remember when ES2015 introduced `let` because `var` had weird scoping issues. But ever since, I barely use either of them. Everything is `const` these days, as it should. reply

const prevents reassignment of the variable but it does not make the object the variable points to immutable.

To do the latter, you have to use Object.freeze (prevent modification of an object’s properties, but it is shallow only so for nested objects you need to recurse) and Object.seal (prevent adding or removing properties, but not changing them).

May people use immutable.js or Immer for ergonomic immutable data structures.

mcv 10/31/2025||
That is an excellent point, and indeed a problem when debugging. When I log objects to the console, often they don't get serialized until I actually click on them, which means I don't get to see the object as it was at the time, but after a bunch of later changes.
LogicHound 10/31/2025|||
`var` doesn't have weird scoping issues, it just different than other languages. `var` is function scoped, thus all var declarations are hoisted to the top of the function during execution.

This is why the single var pattern used to be recommended.

rlander 10/31/2025||
Except const is not sufficient. It will prevent the reference from being reassigned but the const can still reference a mutable object.
jdthedisciple 10/31/2025||
This is almost standard in modern languages:

For example in Dart you make everything `final` by default.

croes 10/31/2025|
A real default doesn’t need a keyword.
wodenokoto 10/31/2025|
Mutability was by far the most difficult thing when learning Python and mutating objects by iterating over its items do get confusing, even as a senior.

When I was first learning I thought all methods would mutate. It has a certain logic to it

More comments...