Passing mutable objects through events

Markus's Avatar

Markus

22 Jul, 2012 01:18 PM

Hi, I'm new to this whole MVC(s) stuff, just migrated from PureMVC + Fabrication, and I must tell You, that it was quite easy!

The code's much cleaner, type-safe, easy to write (no boilerplate code), the injector is awesome, and the framework enforces proper decoupled structure (previously I had logic in mediators, and proxies referencing each other directly) - all of this made me think if passing mutable objects like Vectors from model to view is a good idea.

Simplifying the case a little bit, I have a vector of Message objects, that I pass from Channel model to Chat view. As it's not an immutable value, I'm just passing a pointer to the values, and so the view could change the data of Model directly. I could make a shallow copy of the vector, but still, the Chat will be able to change specific Messages.

I'm not afraid of the scenario to actually happen, but the question remains: Is passing mutable objects a terrible manner for MVC?

Right now I'm thinking of making the Message properties read-only, that could be set only in constructor, and use shallow copies of vectors - is it a right solution, or I shouldn't even bother?

Thanks, Robotlegs team!

  1. Support Staff 1 Posted by Till Schneidere... on 22 Jul, 2012 01:35 PM

    Till Schneidereit's Avatar

    Hey Markus,

    I'm very glad to hear you're enjoying the framework!

    Regarding your question, I agree that in theory, values passed to the
    view should be immutable as far as the model is concerned.

    I also think, however, that in most cases you shouldn't bother,
    really. While passing read-only structures or deep copies around will
    prevent lazy people (like Future You) from taking shortcuts, they're
    pretty likely to find ways around that anyway, if they really try. If
    you pass copies around, for example, they might change the data
    nevertheless, because the effect will be seen in a view that handles
    the data after where they're currently handled.

    What I mean is that even in large systems, you should use a mixture of
    technical enforcement and conventions to get to a clean system.

    If, however, your views are not to be trusted for some reason
    (remember that pest of a coworker who's working on them, Future You:
    he is so goddamn prone to take the most awful shortcuts!), then you
    should take technical precautions to whatever degree seems sensible to
    you.

    Luckily, some of the worst shortcuts you ever encountered where taken
    by that other guy, Past You, and he doesn't work on the project
    anymore.

  2. 2 Posted by Markus on 22 Jul, 2012 03:12 PM

    Markus's Avatar

    Haha, thanks for the great answer! I'm really happy, that after encountering another, random problem in fabrication, I decided to rather switch framework then fix it again.

  3. Support Staff 3 Posted by Till Schneidere... on 22 Jul, 2012 03:40 PM

    Till Schneidereit's Avatar

    Glad to be of help :)

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac