What is immutability and why should I worry about it?

前端 未结 15 1050
无人共我
无人共我 2020-12-07 09:51

I\'ve read a couple of articles on immutability but still don\'t follow the concept very well.

I made a thread on here recently which mentioned immutability, but as

15条回答
  •  北海茫月
    2020-12-07 10:47

    Good question.

    Multi-threading. If all types are immutable then race conditions don't exist and you're safe to throw as many threads at the code as you wish.

    Obviously you can't accomplish that much without mutability save complex calculations so you usually need some mutability to create functional business software. However it is worth recognising where immutability should lie such as anything transactional.

    Look up functional programming and the concept of purity for more information on the philosophy. The more you store on the call stack (the params you're passing to methods) as opposed to making them available via references such as collections or statically available objects the more pure your program is and the less prone to race conditions you will be. With more multi-cores these days this topic is more important.

    Also immutability reduces the amount of possibilities in the program which reduces potential complexity and potential for bugs.

提交回复
热议问题