Re: A value object *must be* immutable?

Michael Rempel <[email protected]>
Newsgroups gmane.comp.programming.domain-driven-design
Message-ID <CAN2bj2Aihi9VKoPXWGUasQL1tGz1W7SOeq+CC1WeP+r9QU9upg@mail.gmail.com>
In theory, the theory and the practice always line up. In practice they
dont.

You need to 'box' a value object (make a mutable equivalent) in order to
transmit it. In theory I think of it as 'teleporting' the value object. The
thing is if you use value objects correctly, their immutability is their
strength. You WANT them to be immutable.

A typical use for them is to encapsulate a stereotype. A set of identifying
values, and actions that go together. It gets clearer when you have
millions of objects, with lots of properties, but say you have a subset
with only a few hundred variations on a theme. That subset is huge, and
takes up a lot of memory. So you decide to save some ram, and save some
time by assembling those subsets of those objects into value objects. That
way you reference the value, and wow, magic, all the extra properties are
available whenever you need them and you only stored it once.

Another analogy that works for me is clothing in a clothing store. It comes
in size, color, for tall people, fat people, and possibly a few other
attributes. Those attributes make each clothing item a value object. Do you
have that shoe in a size 11 long? Does it matter if they have 3 pair, or
just one? No, if they have three pair of size 11 long, we know I dont have
to try on all three. Pick any pair, and if it fits and I like it, I will
buy. If not, those other two pair dont matter. Black, white, or blue might
matter but as for fit, they are all the same. I'll take the blue pair!
Thanks.

Now once you point to a value object, and say here is my magic reference
value, you want that magic object to behave exactly the same way, no matter
what. Of course you might have a few environmental variables influencing
the output of the value object, but that doesnt change the value object
itself. Rain might definitely change the way my new shoes feel! And the
environment stuff will act on all the value objects the same way. The value
object itself should never change anything! Immutable doesnt mean
parameterless, or invariant outputs from similar calls into the ojbect.
Immutable means you cant fiddle with the guts of it. The action depends on
the value it has, and that value is set when the object is made.

So when do you use value objects? When the shoe fits; wear it!


On Tue, Apr 30, 2013 at 4:02 PM, Dan Haywood
<[email protected]>wrote:

> **
>
>
> +1.
>
> But you might want to consider introducing a Mutable Companion [1]   (cf
> String and StringBuilder)
>
> Dan
>
> [1]
> http://www.two-sdg.demon.co.uk/curbralan/papers/vikingplop/TheGoodTheBadAndTheKoyaanisqatsi.pdf
>
>
> On 30 April 2013 21:54, Raoul Duke <[email protected]> wrote:
>
>>
>>
>> http://c2.com/cgi/wiki?ValueObjectsShouldBeImmutable
>>
>>
>> On Tue, Apr 30, 2013 at 1:52 PM, Fabrício Cabral <[email protected]>wrote:
>>
>>>
>>>
>>> Hi everybody!
>>>
>>> Conceptually a value object *must be* immutable? Could
>>> I have a mutable value object? It makes senses?
>>>
>>> Thanks in advance.
>>>
>>> --
>>> --fx
>>>
>>>
>>>
>>
>>
>>
>  
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.