Live data from Hacker News

John Carmack on mutable variables

twitter.com

551–560 of 663 posts

Re: John Carmack on mutable variables

#551
post #507

Earlier quoted context omitted.

What would those names be in this example?

In a real application meaningful names are nearly always possible, eg: const pi = 3.1415926 const 2pi = 2 * pi const circumference = 2pi * radius

Agree in real life you can come up with meaningful names (and should when the names are used far away from the point of assignment), but it doesn’t make sense for GPs example, where the whole point was to talk about assignments in the abstract.

Re: John Carmack on mutable variables

#554
post #521
post #507

Earlier quoted context omitted.

In a real application meaningful names are nearly always possible, eg: const pi = 3.1415926 const 2pi = 2 * pi const circumference = 2pi * radius

Calling tau 2pi is the most cursed thing I've seen all day. Appropriate for Halloween.

If you call a variable tau in production code then you're being overly cute. I know what it means, because I watch math YouTube for fun, but $future_maintainer in all likelihood won't.

Re: John Carmack on mutable variables

#555
post #29

Yeah, I also wish it was the default. But it's a little too verbose to just sprinkle on every variable in C++. Alas. Rust gets this right, but I'm stuck with C++ at work.

> But it's a little too verbose to just sprinkle on every variable in C++ It's worth it, though. Every variable that isn't mutated should be const. Every parameter not mutated should be const, and so should every method that doesn't mutate any fields. The mutable keyword should be banned.

Const annotations are worth it for methods and APIs, but not most local variables.

Re: John Carmack on mutable variables

#556

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?”

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.

if this guys learns enough, who knows, he may have a future in programming!

Re: John Carmack on mutable variables

#557

After a 2 year Clojure stint I find it very hard to explain the clarity that comes with immutability for programmers used to trigger effects with a mutation. I think it may be one of those things you have to see in order to understand.

I think a lot of this kind of stuff should have language support (like he mentions), even if it is not that functional and is just as a hint.

That said, utopias are not always a great idea. Making all your code functional might be philosophically satisfying, but sometimes there are good reasons to break the rules.

Re: John Carmack on mutable variables

#558
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…

This is far from true in my experience. I'm no Lisp hater (I wrote a self-compiling compiler in Scheme) but different syntaxes have dramatically different degrees of familiarity, like Latin letters and Cyrillic. How long do you think it would take you to learn to read phonetically in Cyrillic as fast as you do in the Latin script? It takes the humans months or years, if they ever arrive. But, also, some notations are more usable than others, even for experts, just as with other kinds of user interfaces. Flat is better than nested, simple is better than complex, complex is better than complicated, Perl and MUMPS are unnecessarily error-prone, etc.

Re: John Carmack on mutable variables

#559
post #188

After a 2 year Clojure stint I find it very hard to explain the clarity that comes with immutability for programmers used to trigger effects with a mutation. I think it may be one of those things you have to see in order to understand.

I think the explanation is: When you mutate variables it implicitly creates an ordering dependency - later uses of the variable rely on previous mutations. However, this is an implicit dependency that isn't modeled by the language so reordering won't cause any errors. With a very basic concrete example: x = 7 x = x + 3 x = x / 2 Vs x = 7 x1 = x + 3 x2 = x1 / 2 Reordering the first will have no error, but you'll get t…

It's funny that converting the first example to the second is a common thing a compiler does, Static single assignment [0], to make various optimizations easier to reason about.

[0] https://en.wikipedia.org/wiki/Static_single-assignment_form

Re: John Carmack on mutable variables

#560

This would require coming up with an order of magnitude more variable names which is just unnecessary cognitive load.

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 reass…

[deleted]
Post reply on HN