Memento Mori

A discussion at work took place about design patterns, and it prompted me to go back and look at something. One of the behavioural patterns is Memento, which (from Wikipedia) “provides the ability to restore an object to its previous state (undo)”. The implementation described uses three objects: a caretaker, an originator and a memento object that the caretaker can use to restore the originator to a previous state.

From a functional programming viewpoint, I find this overly complex. Consider a system in which an object follows the functional principle of immutability. To “change state”, a caller calls a method on that object, but the original object is unchanged: the method returns a new object with a different state. There’s then no need to have the complexity of undo – the previous state is available via the original instance.

Of course, this is already the case with immutable strings in many languages. Consider this C#:

    string s1="This String is Mixed Case";
    string s2=s1.ToLower();
    //Oops - we need to undo the ToLower() operation.
    s2=s1;  //This is effectively an "undo"

The only principle to keep in mind is to save a reference to a previous version of the object if there is a potential need to undo it.

Benefits: huge saving of complexity, and therefore of testing. I find that the testing load is often forgotten when developers assess how much work is involved in an implementation, unless TDD is in the blood of the team.
Downside: potential extra use of memory to keep hold of previous objects. With intelligent sharing of expensive resources between objects, this can be minimal.

Advertisements

Two Nations Divided By A Common Language

Now that was annoying. I see news of an excellent new feature in Google Mail; custom “From” addresses. Now, this is a good thing because it allows me to appear to my contacts via the same address they’ve been used to seeing but also because “googlemail.com” addresses seem to be helping my emails get marked as spam. Hmph. Anyway, off I go to set up a new address… and I find that there’s no way to do it. The “Accounts” tab that the Help so carefully explains doesn’t appear for me. I feel so rejected.
However, a few moment’s thought and Googling shows me the problem; I changed my language to “English (UK)”. Changing it to “English (US)” causes the magical new options to appear in full. Hoorah. Now my email has the right “From:” header… but also a different “Sender:” header, causing yet more spam detectors to scream with suspicion. Hey ho.
Anyway, there you have it; blogged so that others in the same situation might find the solution.