6

I'm a beginning coder and I've been working on a WPF app for several months now and struggled to implement anything in MVVM due to time constraits to learn so I coded my program the only way to knew how: with code behind, tight coupling, and a whole bunch of spaghetti code.

My program has grown into 20,000+ lines of messy (but working) code that is mostly event driven. The app I am building will probably be used and continue to be developed for years to come, but I will be the only one working on the code.

However, now that I have time to start learning MVVM, I'm wondering how useful it really is to follow the MVVM pattern. I'm considering refactoring my code into the MVVM pattern but everything seems 100x difficult to do with little documentation. With using code behind and tight coupling, I can make the UI do exactly what I want it to do and everything seems like it can be coded so much faster through the event-driven methods.

How important is it to follow the MVVM design pattern for WPF apps, especially in the long run? Do you think it's a good idea for me to invest my time to redesigning this app and following an MVVM pattern?

2
  • 1
    What you are describing sounds scary. Creating a mess of spaghetti code and delivering it to the client is generally a recipe for disaster. The main reasons I have heard for using MVVM is that the views are re-usable for any screen and you don't have to test the view because it doesn't contain any logic. You can simply test the ViewModel and Model. I hope you are using some form of automated testing in your current code to ensure quality. Aug 28, 2014 at 21:50
  • 2
    Yes, please, try to implement your application now using MVVM. You will thank yourself later. It's not easy, but well worth the effort.
    – Bernard
    Aug 29, 2014 at 1:58

1 Answer 1

13

It looks like you've found yourself in perfect learning situation. Currently your application works, and I'm sure you're very familiar with all of the 20k lines of code. Making changes isn't that difficult, and, so far, there doesn't seem to be any reason to change your development approach.

You are a cowboy! And being a cowboy is a lot of fun!

But being a cowboy is a lot of work, and even the best cowboys run out of steam after a while (sorry about the mixed metaphors).

In the question you mentioned that the app will be used and maintained for years to come. Those 20k lines will surely increase, and with every new feature you add, and every new bug you squish, you'll notice that it gets harder and harder to write code without breaking existing functionality. This is tightly coupled spaghetti hell!

MVVM as a pattern encourages developers to loosely couple their code. In the MVVM world you keep your Model separate from your view state (View Model) and display logic (View). This does require a bit more up-front work, but the downstream benefits are many:

  • Unit testing your Model is easier. No View baloney to worry about.
  • Integration testing your View Model is a breeze
  • Because your M, VM, and V are distinct sections of code, modifying one has a near 0% chance of causing regressions in the others.
  • And there are at least 7 other reasons.

But the most important reason to switch to MVVM over CBV (Cowboy View) is this: You're human. While humans are actually pretty good at creating mental models of complex things like software, doing so is a very draining endeavour. And keeping track of frequently changing and ever growing software is a recipe for mental illness.

We are flawed sacks of flesh. But fret not, for we are also organised flawed sacks of flesh.

By organising your code into classes with single responsibilities (eg, a ViewModel) you can now work on subsets of your application without having to load up your mental model of the entire application. Your brain will thank you for this!

But don't take my word for it. Like I said at the beginning of this pseudo-rant: You're in a perfect learning situation. You've made a classic mistake (there's no shame in that) and you're going to be around long enough for it to catch up to you.

Switch to MVVM now if you want. Or don't. Either way, you're going to eventually learn why it's a useful pattern.

My recommendation: try to maintain your cowboy spaghetti for as long as you can. The best way to learn is by making mistakes. So make your big ones early and often!

Have fun!

2
  • 3
    I like to add that one: It can be very frustrating to learn the MVVM pattern, specially when you don't have a coding partner or mentor who has some experience with it. I learned it after I've worked a lot with WinForms (and event based) and I struggled a lot cause it's a totaly different approach. But it is absolutly worth the pain. Although you have to understand in detail what is happening in MVVM, there are some libraries helping you: for example CaliburnMicro or MVVM Light Aug 29, 2014 at 12:20
  • 1
    Thanks all. I'm starting to get the hang of the pattern after spending a week looking up samples and coding my own mini-projects. Now I'm beginning to see the power of the design pattern and it's much better than what I had before. However, there's still a lot I don't know and will definitely take effort to learn! Sep 4, 2014 at 18:17

Your Answer

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

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