4 ms·
If you need new values you just make new things. If you want to do an operation on fooA, you don't mutate fooA. You call fooB = MyFunc(fooA) and use fooB. Th
by jayd16 1y ago
If you need new values you just make new things.
If you want to do an operation on fooA, you don't mutate fooA. You call fooB = MyFunc(fooA) and use fooB.
The nice thing here is you can pass around pointers to fooA and never worry that anything is going to change it underneath you.
You don't need to protect private variables because your internal workings cannot be mutated. Other code can copy it but not disrupt it.
- MetaWhirledPeas 1y ago> If you need new values you just make new things. > If you want to do an operation on fooA, you don't mutate fooA. You call fooB = MyFunc(fooA) and use fooB. The beautiful thing about this is you can stop naming things generically, and can start naming them specifically what they are. Comprehension goes through the roof.
- ErroneousBosh 1y agoYes, but you can do that without having loads of stale copies of incorrect data lying around, presumably.
- MetaWhirledPeas 1y agoWith techniques like method chaining you can avoid using a constant or a variable and just pass return values around.
- ErroneousBosh 1y ago> If you want to do an operation on fooA, you don't mutate fooA. You call fooB = MyFunc(fooA) and use fooB. This is the bit I don't get. Why would I do that? I will never want a fooA and a fooB. I can't see any circumstances where having a correct fooB and an incorrect fooA kicking around would be useful.
- jayd16 1y agoAs Carmack points out, naming the intermediate values aides in debugging. It also helps you write code as you can give a name to every mutation. But also keep in mind that correct and incorrect is not binary. You might want to pass a fooA to another class that does not want the fooB mutation. If you just have foo, you end up with situations where a copy should have happened but didn't and then you get unwanted changes.
- ErroneousBosh 1y agoBut that's just it, why would a copy ever happen? Why would you want a correct and an incorrect version of your variable hanging about?
- kubanczyk 1y agoTaking your point of view: you assigned a value1 to a name. Then you assigned a value2 to the same name. You say that value2 is correct. It logically follows that value1 was incorrect. Why did you assign it then? The names are free, you can just use a correct name every single time.
- zaphirplane 1y agoBecause the account owner withdrew money . The player scored a goal, the month ticked over, the rain started, the car accelerated, a new comment was added to the thread . The world by definition mutates over time.
- kubanczyk 1y agoAh, true. If the var is a part of a long-living state, all good. That's just rarely seen in CRUD apps, more common in games.
- takinola 1y agoIt is about being able to think clearly about your code logic. If your code has many places where a variable can change, then it is hard to go back and understand exactly where it changed if you have unexpected behavior. If the variable can never change then the logical backtrace is much shorter.