Tuesday, April 21, 2020

ReadOnly vs Immutable collection


ReadOnly Gotcha's

If we declared a collection readonly then it is just a wrapper around the source collection. Here is a catch when I say readOnly collection is just a wrapper I mean it refer source as a reference. There is a point when external library refer readonly collection at a given instance it holds say 10 values. Later stage in the code base if the source reference collection is updated with 11 values in it and we don't want the external library to consume that , in such case readOnly library introduces a complication. For our rescue we should use Immutable only when we are certain readOnly refers to a source that can change due to some conditional logic.

External Library- Refer ReadOnly -R


  • R- refer source collection S holds 10 values
  • S- Later in the code we add 11 values which External library shouldn't refer to..
  • R- refer source collection S holds 11 values





No comments :