snippetjournal

– notes, snippet codes, and my journal

Archive for December 2015

Immutability and Mutability

leave a comment »

Immutable
+ No thread locks
+ Fast read and lookup
– Bad for objects that highly mutable (increase memory usage every time it mutates)

Mutable
+ Fast for highly mutable objects
+ Low memory footprint
– Thread locks

Where you use immutable/mutable depending on the objects and pattern utilized. Functional and Math programming requires immutability on all objects to ensure consistency with parallel execution. Object Oriented apps rarely needs immutability since object is reused over and over with high mutability.

1 very clear example of mutability : Try playing games and imagine if all the objects is immutable, including interaction between objects (player on terrain, player vs player, gravity simulation, etc) and if this immutable, how do you deal with the number of objects generated every time it mutates.

1 example for immutability : Math calculation that can be break down and spread across multiple parallel threads (calculating/simulating)

http://www.kaskus.co.id/show_post/56611221dc06bd54638b4568/4041/-

Advertisements

Written by snippetjournal

December 4, 2015 at 7:47 am

Posted in Uncategorized