Live data from Hacker News

John Carmack on mutable variables

twitter.com

231–240 of 663 posts

Re: John Carmack on mutable variables

#231
post #208
post #121

Earlier quoted context omitted.

That depends on your definition. Programming languages often deviate from mathematics when it comes to definition of variables, functions etc. That is by choice, Haskell tried to be as close to mathematical definition as possible.

You don’t need to make a mathematical argument, “variable” and “constant” have clear meanings in colloquial use which match the definitions of your parent comment.

So does 'parent'.

Re: John Carmack on mutable variables

#232

Earlier quoted context omitted.

In your sample there really is no benefit to using the "is" operator over just checking for null (assuming you haven't overloaded the "!=" operator). However, the "is" operator is a lot more powerful, you can match an expression against a pattern with it. Would you say that these samples show no benefit to using the "is" operator? if (obj is string s) { ... } if (date is { Month: 10, Day: https://learn.microsoft.com/…

The issue is that I dislike the overall mentality of just adding a bunch of language features. Things just seem to be dumped in each release and I think to myself "When I am going to use that?". > Would you say that these samples show no benefit to using the "is" operator? I didn't say no benefit . I said dubious benefit . I didn't really want to get into discussing specific operators, but lets just use your date exa…

I agree that they should not add new stuff lightly, but the "is" operator actually should be looked together with switch expression in the context of pattern matching. How else could you enable powerful and succint pattern matching in c#?

Re: John Carmack on mutable variables

#233
post #124

When I started programming in Haskell, where all variables are immutable, I felt like I was in a straitjacket Then, suddenly, the enlightenment

How does Haskell deal with things like memory-mapped I/O or signal handlers where the value of a variable can be changed by factors outside of the programmer's control?

Side effecting computations that depend on the "real world" go into an IO monad. The game in Haskell is shifting as much of the codebase as possible into pure functions/non-side-effecting code, because it's easier to reason about and prove correct.

Re: John Carmack on mutable variables

#234
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.

100%, life's too short.

Ultra-pedantic const-correctness (vs tasteful const-correctness on e.g. pass-by-reference arguments or static/big objects) catches nearly no bugs in practice and significantly increases the visual noise of your code.

If you have luxury of designing a new language or using one with default mutability then do so, but don't turn C coding styles into C++-envy, or C++ coding styles into Rust-envy.

Re: John Carmack on mutable variables

#235
post #215
post #49

In JavaScript, I really like const and have adopted this approach. There are some annoying situations where it doesn't work though, to do with scoping. Particularly: - if (x) { const y = true } else { const y = false } // y doesn't exist after the block - try { const x = foo } catch (e) { } // x doesn't exist after the try block

JavaScript’s `const` has the bigger issue that while things can’t be reassigned, they can still mutate. For example: const myArray = [1,2,3] myArray.push(4) myArray // [1, 2, 3, 4]

In what way is that an issue?

Re: John Carmack on mutable variables

#236
For performance critical code, you want to reuse L1D cache lines as much as possible. In many cases, allocation of a new immutable object boils down to malloc(). Newly allocated memory is unlikely to be found on L1D cache. OTOH, replacing data in recently accessed memory and reusing the memory is very likely to become L1D cache hit in runtime.

Re: John Carmack on mutable variables

#237

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.

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.

Re: John Carmack on mutable variables

#239

Earlier quoted context omitted.

> If you want a language where const is the default and mutable is a keyword whats the difference between const and mutable?

"const" means something can't be changed. "mutable" means it can be changed. You don't need both a "const" keyword and a "mutable" keyword in a programming language. You only need 1 of the keywords, because the other can be the default. munchler is saying the "const" keyword shouldn't exist, and instead all variables should be constant by default, and we should have a "mutable" keyword to mark variables as mutable. A…

> you don't need both a "const" keyword and a "mutable" keyword

What if the lang has pointers? How express read-only?

Re: John Carmack on mutable variables

#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 this thing and so that's why I don't run into it.

Post reply on HN