4

I have been recently strongly influenced by two lectures: Gary Bernhardt's "Boundaries" (https://www.destroyallsoftware.com/talks/boundaries) and Andy Matuschak's "Controlling Complexity" (https://realm.io/news/andy-matuschak-controlling-complexity/)

I have also been learning about, and working with, the MVVM paradigm which leads to my question. Should the ViewModel be an immutable/value-type? Sometimes I think yes, others no.

I'm struck by the notion that the ViewModel is a link between the imperative shell and the functional core which I think is what is confusing me. Should the link between these two be imperative or functional in nature?

1 Answer 1

4

The main purpose of a view model object is to provide a layer of abstraction between the View and the Model, so that you don't have to write code-behind spaghetti. By putting the View logic into its own object, you decouple it from the actual UI surface and render its logic testable.

What would be the point of an immutable view model object?

Consider what you are getting with immutability: thread safety and the ability to compute an idempotent result from a function composition that is easier to reason about than a mutable object being operated on by several agents.

Are any of these conditions present in a View Model object? No.

UI's are mutable by nature; you're not going to give people one chance, and one chance only, to enter data into a text box, are you?

Further, it is the mutable nature of the real world that requires purely functional languages to interact with the real world through mechanisms like monads.

5
  • In response to the question, "you're not going to give people one chance, and one chance only, to enter data into a text box, are you?" In iOS, the NSString contained in a UITextView (the object that holds the data the user typed,) is immutable. This is, in part, the reason I'm confused about the issue.
    – Daniel T.
    Jun 14, 2015 at 1:14
  • Strings in C# (and many other languages) are immutable as well, for very good reasons. Doesn't mean you have to make the entire ViewModel object immutable... If anything, that strings are immutable makes it less necessary. Jun 14, 2015 at 2:08
  • Hmm... I know I don't have to make the ViewModel immutable. I don't have to make anything immutable. Maybe if you could expand on the notion that immutable model objects render an immutable ViewModel unnecessary? I feel like the answer is in there somewhere. Thanks.
    – Daniel T.
    Jun 14, 2015 at 2:32
  • 1
    Well, simply that there are already some safety guarantees for strings. Did you read the conversation at the link I posted in my previous comment? Compare that with the inconvenience of having to set up a whole new View Model object from scratch if the user simply made a typing error. Then think about what you might gain from having an immutable View Object. Thread safety? How many people are going to be typing into that UI on that one screen simultaneously? Jun 14, 2015 at 2:33
  • So you are saying that Strings are immutable for good reasons, but none of those reasons apply to ViewModels?
    – Daniel T.
    Jun 17, 2015 at 1:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.