Live data from Hacker News

John Carmack on mutable variables

twitter.com

491–500 of 663 posts

Re: John Carmack on mutable variables

#491
post #240

I'm going to maybe out myself as having limited experience here ... I don't mind the idea here, seems good. But I also don't move a block of code often and discover variable assignment related issues. Is the bad outcome more often seen in C/C++ or specific use cases? Granted my coding style doesn't tend to involve a lot of variables being reassigned or used across vast swaths of code either so maybe I'm just doing th…

In Python, no user object is modified by a simple assignment to a name. It just binds it.

It is not about mutable/immutable objects , it is about using a name for a single purpose within given scope.

    a = 1
    b = 2
    a = b
"a" name refers to the "2" object. "1" hasn't changed (ints are immutable in Python). If nothing else references it, it might as well disappear (or not).

Though both "single purpose" and immutability may be [distinct] good ideas.

Re: John Carmack on mutable variables

#492
post #237

Earlier quoted context omitted.

Clojure also makes it very easy, it'd require too much discipline to do such a thing in Python. Even Carmack, who I think still does python mostly by himself instead of a team, is having issues there.

> it'd require too much discipline to do such a thing in Python Is Python that different from JavaScript? Because it's easy in JavaScript. Just stop typing var and let, and start typing const. When that causes a problem, figure out how to deal with it. If all else fails: "Dear AI, how can I do this thing while continuing to use const? I can't figure it out."

I agree that Python is not too different and in general I treat my Python variables as const. One thing, however, where I resort to mutating variables more often than I'd like is when building lists & dictionaries. Lambdas in Python have horrible DX (no multi-line, no type annotations, bad type checker support even in obvious cases), which is why the functional approach to build your list, using map() and filter() is much more cumbersome than in JS. As a result, whenever a list comprehension becomes too long, you end up building your list the old-fashioned way, using a for loop and the_list.append().

Re: John Carmack on mutable variables

#494
post #456

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…

This works in RustRover as well! Super useful.

Rust's type system specifically facilitates more powerful tools: https://github.com/willcrichton/flowistry

Re: John Carmack on mutable variables

#495

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.

Made a similar experience with Scheme. I could tell people whatever I wanted, they wouldn't really realize how much cleaner and easier to test things could be, if we just used functions instead of mutating things around. And since I was the only one who had done projects in an FP language, and they only used non-FP languages like Java, Python, JavaScript and TypeScript before, they would continue to write things base…

JS is much more of a functional language than it was given credit for a long time. It had first-class functions and closures from day one if I'm not mistaken.

Re: John Carmack on mutable variables

#496

Earlier quoted context omitted.

Yet even Rust allows you to shadow variables with another one with the same name. Yes, they are two different variables, but for a human reader they have the same name. I think that Rust made this decision because the x1, x2, x3 style of code is really a pain in the ass to write.

In idiomatic Rust you usually shadow variables with another one of the same name when the type is the only thing meaningfully changing. For example let x = "29" let x = x.parse:: () let x = x.unwrap() These all use the same name, but you still have the same explicit ordering dependency because they are typed differently. The first is a &str, the second a Result , the third an i32, and any reordering of the lines woul…

In a Clojure binding this is perfectly idiomatic, but symbolically shared bindings are not shadowed, they are immutably replaced. Mutability is certainly available, but is explicit. And the type dynamism of Clojure is a breath of fresh air for many applications despite the evangelism of junior developers steeped in laboratory Haskell projects at university. That being said, I have a Clojure project where dynamic typing is throughly exploited at a high level, allows for flexible use of Clojure's rational math mixed with floating point (or one or the other entirely), and for optimization deeper within the architecture a Rust implementation via JVM JNI is utilized for native performance, assuring homogenous unboxed types are computed to make the overall computation tractable. Have your cake and eat it too. Types have their virtues, but not without their excesses.

Re: John Carmack on mutable variables

#497

Earlier quoted context omitted.

My problem with functional languages is there never seems to be any easy way to start using them. Haskell is a great example here. Last time I tried to learn it, going on the IRC channel or looking up books it was nothing but a flood of "Oh, don't do that, that's not a good way to do things." It seemed like nothing was really settled and everything was just a little broken. I mean, Haskell has like what, 2, 3, 4? Maj…

> I mean, Haskell has like what, 2, 3, 4? Major build systems and package repositories? It's a quagmire. Don't know when was the last time you've used Haskell, but the ecosystem is mainly focused on Cabal as the build tool and Hackage as the official package repository. If you've used Rust: - rustup -> ghcup - cargo -> cabal - crates.io -> hackage - rustc -> ghc

It's admittedly been years.

ghcup didn't exist, AFAIK. Cabal was around but I think there was a different ecosystem that was more popular at the time (Started with an S, scaffold? Scratch? I can't find it).

Re: John Carmack on mutable variables

#498
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()

Re: John Carmack on mutable variables

#499
post #12

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

I was part of the Carmack cult but the illusion was broken when I saw him use the same authoratative tone on a subject I'm more knowledgable about.

I don't think gellman amnesia is really a material issue. Carmack is indubitably an expert in his field, but that doesn't mean he's an expert in every field (like aerospace or AI). I'm an expert in some things, but I've probably said some stupid shit in other fields where I dabble such as cooking, playing music, raising cats.

Re: John Carmack on mutable variables

#500
post #336

Earlier quoted context omitted.

result.process() That doesn’t make logical sense. You already have a result. It shouldn't need processing to be a result.

It also doesn't make sense for `process()` to be an attribute of `result`. Why would you instantiate a class and call it result‽

> Why would you instantiate a class and call it result‽

Are you suggesting that the results of calculations should always be some sort of primitive value? It's not clear what you're getting hung up on here.

Post reply on HN