Top
Best
New

Posted by azhenley 4 days ago

John Carmack on mutable variables(twitter.com)
509 points | 620 commentspage 7
ardit33 4 days ago|
Meh... I agree where he comes from (when working on large projects, muttability can introduce bugs), but many languages have solved it, similar by having two types, 'let' and 'var', or 'const' in front of a variable.

But, there is practicality in the ability of being able to change a var, and not having to create a new object every time you change one of its members.

It models real nature/physics better.

It looks like He is asking that 'const' be the default, and 'var' should be explicit, which makes sense.

mavhc 4 days ago||
Really should invent a new name if you don't want your variables to vary
kybernetyk 4 days ago||
the comments/replies to his tweet remind me why I usually avoid twitter
theodorethomas 4 days ago||
Do as much as you can in a spreadsheet, then start a new spreadsheet.
textlapse 4 days ago||
I wish C++ did some sane things like if I have a const member variable, allow me to initialize it as I wish in my constructor - it's a constructor for crying out loud.

Don't be silly and assume if I assign it multiple times in an if condition it's mutable - it's constructing the object as we speak, so it's still const!!!

C# gets this right among many other things (readonly vs const, init properties, records to allow immutability by default).

And the funny thing is the X thread has lots of genuine comments like 'yeah, just wrap a lambda to ensure const correctness' like that's the okay option here? The language is bad to a point it forces good sane people into seeing weird "clever" patterns all the time in some sort of an highest ELO rating for the cleverest evilest C++ "solution".

I was hoping Carbon was the hail mary for saving C++ from itself. But alas, looks like it might be googlified and reorged to oblivion?

Having said that, I still like C++ as a "constrained C++" language (avoid clever stuff) as it's still pretty good and close to metal.

dinkblam 4 days ago||
// !!!: SWIFTY VAR & LET SUPPORT

#define var __auto_type

#define let const __auto_type

armaoin 4 days ago||
Totally agree. const auto for me is the default.
mtlmtlmtlmtl 4 days ago||
It's fascinating how even when Carmack says something rather obvious and unoriginal, that many people have said before, sometimes decades ago, it still spawns a 400+ comment thread on HN. I really don't get it, it's almost like a cult of personality at this point.
kins 4 days ago|
It always was.
askmrsinh 4 days ago|
Constants are useful for reasoning about code, but anyone who focuses only on making everything an immutable is missing the point. The main goal should be achieving referential transparancy.

It can be perfectly fine to use mutable variables within a block, like a function when absolutely needed - for example, in JavaScript's try catch and switch statements that need to set a variable for later use. As long as these assignments are local to that block, the larger code remains side-effect free and still easy to reason about, refactor and mantain.

https://rockthejvm.com/articles/what-is-referential-transpar...

More comments...