Georgia Reh

I'm a mathematician. I build robots and climb rocks.


November 12, 2014

On Wikipedia, the page for immutable object says “In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created. This is in contrast to a mutable object, which can be modified after it is created.” which is a perfectly accurate description of the concept and meant absolutely nothing to me until after I already understood the concept. Without a clear idea on how to best research this, I asked a friend to explain it and we sat down and drew a lot of boxes. I now understand the concept well enough to look at Wikipedia and say “yes, that is exactly correct”. Now that I kind of understand immutability, here is a metaphor for how I think about it.

You have a closet completely lined with shelves. The shelves have numbers on them and you have two types of boxes: glass boxes and cardboard boxes. Also, you have a goblin who lives in your closet and sometimes yells at you and tells you that you are terrible at organizing a closet and he will just kick you out and arrange things for you. He gets really cranky when you use the wrong box. When you store things in the glass box, the goblin seals the box like a snow globe and puts it away on the shelf, but when you store things in the cardboard box he just folds it closed before putting it away.

If you ask for a cardboard box called halloween decorations, the goblin hands you the box on shelf 12 and you can remove doll heads, draw on them, or add new ones, and when you give the box back to the goblin he will close the box and put it back on shelf 12.

In contrast, when you ask for the glass box called thanksgiving decorations, the goblin gives you the glass box on shelf 15 and says “Do not open this”. You have some new decorative gourds you want to add to your collection, so you tell the goblin, “I am going to open it and add things”. He gets angry at you and refuses, instead he magically creates an identical glass box with all its contents, but it has not been sealed, he hands you the box and says “this is thanksgiving decorations now”. You can add things and remove things and draw on these gourds just like the doll heads in the cardboard box. When you give the new thanksgiving decorations to the closet goblin, he will seal it up just like the old one, and put it on a new shelf.

Every year, when you want to change your Thanksgiving decorations, the goblin makes you a new glass box, and he continues to store all your old boxes and your closet fills up with glass boxes that used to be called thanksgiving decorations, but there is still only one cardboard box called halloween decorations.

There are good things and bad things about the cardboard boxes and the glass boxes. If you want to have a smaller closet that does not have nearly identical sets of gourds in every glass box, then too bad. The goblin will yell at you and kick you out of the closet if you try to consolidate your gourd collection. On the other hand, if you take the cardboard box out of the closet and throw out all your old doll heads, then you can never recover them. Next year, if you change your mind and wish you still had doll heads, the goblin will just say he does not have any, they are gone forever.

This closet is just an array where everything your program needs is stored, the cardboard boxes are mutable variables, the glass boxes are immutable variables, and the goblin is your compiler.

It is possible to keep a closet full of cardboard boxes, and I think a C goblin will let you do that, but some information should not be altered, and that is why the glass boxes exist. For example, your birthday is an immutable variable. You were born on that day, and you can never change that. Your job is a mutable variable; you might decide to change profession and so you will want to be able to change that variable. Essentially the term ‘mutable variable’ means changeable variable and ‘immutable variable’ means unchangeable or constant. Of course you have the option to not change changeable variables, so some goblins will let you keep a closet full of cardboard boxes and it is then up to you to be extremely careful and certain that anything you throw out or alter will never be needed again. The goblin gives you glass boxes to protect you from your own shortsightedness.