14

Recently in an interview, one of the questions was 'Why do we use MVC?’ I just answered that it is much closer to how, many of the real world systems are! Explained the benefits it has when it comes to Maintainability, Scalability etc. But they were not convinced and finally told me that MVC is used mainly as it 'enables easy Unit Testing'.

While I know theirs is a valid point, I still doubt if it is the major reason because (i) even if I decide not to write Unit Testcases, MVC is a probable choice (ii) Many GUI systems where Unit Testcases are there does not follow MVC.

So the question is 'Is Unit Testing the primary objective of MVC Pattern?'

EDIT: I assume that they might be mentioning ease of Test Driven Development/writing NUnit Testcases. This is because we canwrite testcases for the Model (Provided the View is exactly reflecting Model's state changes)-please correct me if I am wrong.

5
  • 11
    You didn't passed the interview, did you? If no, lucky you. I won't join a company that have a very wrong mindset from the start. :) Unit Testing Definitely not the primary objective. It may help the unit testing because the concern all separated, but definitely not the primary objective.
    – Rudy
    Jul 21, 2011 at 8:48
  • 4
    Remember that interview works both way. You are probing them as much as they are testing you. You just got a red flag : don't go in this company. They have no clue, but even worse, think they semms to not realise that, so no hope of improvement. If you choose to go in the company, you'll face many kafkaesque situations.
    – deadalnix
    Jul 21, 2011 at 10:37
  • @Rudy No I did not pass :P, it was a leading investment bank’s Dev Centre. Also the guys looked good and very authentic with other questions and that is why I got confused with this.
    – WinW
    Jul 21, 2011 at 10:50
  • @deadalnix, Yeah true..feel the same after I see the answers here. But I was not that sure before posting it here.
    – WinW
    Jul 21, 2011 at 11:09
  • I totally agree with deadalnix. Dont go to this company.
    – Rudy
    Jul 21, 2011 at 11:25

5 Answers 5

33

The primary objective would be "separation of concerns", as the model, the view and the controller all have distinct responsibilities.

The author of the original Xerox PARC paper states that:

The essential purpose of MVC is to bridge the gap between the human user's mental model and the digital model that exists in the computer.

If unit-testing were the primary objective, one would be able to easily unit-test views. A look at the landscape of unit-testing projects/frameworks would reveal that it is quite contrary to the claim made. One would typically be using integration and functional tests to test the view.

1
  • 2
    I would say the primary objectives are enabling the Direct Manipulation Metaphor (that's basically what the quote says) and User Empowerment (it was originally envisioned that only the Models would be written by programmers, the Views and Controllers would be written by end users). Jun 20, 2012 at 0:35
14

In my opinion, the answer is a firm 'no'. Perhaps this was the main benefit that was observed in this specific organisation, but I would not call it the 'primary objective'.

I guess it would not be all that difficult to implement MVC in a way, that's hellishly difficult to unit test (heck - the way I did it for the first time was hardly testable).

On the other hand, one could say that that pretty much any pattern (excluding things like Singleton) facilitates unit testing, since they most often promote decoupling - but is it their 'primary objective'? Hardly.

12

MVC (just as most of the know design patterns) was around way before unit testing became known. The GoF book was published in 1994 - and they were only documenting the patterns which have been in use for years (if not decades) before. (And there is no mention of unit testing in it.) About unit testing, I can't locate an exact time about when it became "public" - I personally read about it in articles related to Extreme Programming, and the first XP book came out in 1999.

So obviously unit testing could not be the main objective of inventing / documenting patterns - while it is only fair to say that patterns, when applied well, facilitate unit testing greatly.

6
  • The timeline reference is a nice mention- logically supports the argument.
    – WinW
    Jul 21, 2011 at 10:31
  • There seem to be samll issue with date. heim.ifi.uio.no/~trygver/themes/mvc/mvc-index.html says "MVC was conceived in 1978 as the design solution to a particular problem". No worries...Still your argument holds good- MVC was there long before unit testing originated.
    – WinW
    Jul 21, 2011 at 11:31
  • Unit testing has been around since at least the 1980's. I was starting my career back then and we had unit tests on some of the projects I worked on (and it didn't seem like a new idea then). We just didn't have the pre-built frameworks we have now.
    – GreenMatt
    Jul 21, 2011 at 15:05
  • 2
    @GreenMatt, I know unit testing was not invented by Kent Beck, just reused :-) But AFAIK it was relatively unknown before XP and Agile started to widely propagate it. Jul 21, 2011 at 15:28
  • @Péter Török: I remember 1) writing my own simple code to test individual functions all the way back to college (early-mid 1980's for me) and that I got the idea from someone else; 2) seeing depictions of and reading papers about the waterfall model in the 80's or 90's with a phase called "Coding and Unit Testing" (vs. just "Coding"). (Sorry, I don't remember where, so can't provide citations.) Thus, unit testing has been around and evolving for quite a while.
    – GreenMatt
    Jul 21, 2011 at 15:50
2

I think no, ease of unit testing is one of the benefits, but its part of a collection of benefits when using MVC along with the reasons you list. To say that there is a single primary reason to use MVC is a mistake. It sounds like the company in question choose MVC to facilitate unit testing, hence they think it is the primary reason. Personally my reasons for using MVC are its simplicity compared to web forms which makes it easier to design for and maintain, but every individual/company will have their own reasons for using any technology.

0

In the ASP.NET MVC world, many improvements to ASP.NET have been included in the framework itself. The main purpose of this design pattern is to isolate business logic from the user interface in order to focus on better maintainability, improved testability, and a cleaner structure to the application.

ASP.NET MVC has certain capabilities that make it the best option to choose if you need one or more of the following:

A high level of control over the generated HTML: Unlike Web Forms, Views in ASP.NET MVC render HTML exactly as you tell them to. Recently, Web Forms have been improved in this area but still don’t have the level of control MVC has.

Easier unit testing: With ASP.NET MVC, it is very easy to follow testing patterns such as test-driven development (TDD). Because of the complex event lifecycle in Web Forms, on top of a control-based framework, TDD is a lot easier with MVC.

Separation of concerns: This refers to having all aspects of the system clearly separated from one another. Because of the pattern it implements, an MVC application is divided into discrete and loosely bound parts (model, views, and controllers), which makes it easy to maintain.

Some of the other benefits are:

•The MVC pattern itself makes it easier to manage complexity by clearly separating the functionality of the application into three core parts, the model, the view, and the controller.

•ASP.NET MVC web applications do not use view state or server-based forms. This makes the MVC framework ideal for developers who want full control over the behavior of an application. View state can become very large, which is a problem for devices like smartphones running over slow networks (transmitting all that information can be very slow). In a Web Forms page, you could only have one per page. This is quite a major restriction. In MVC, there is no such restriction—that is, you can have as many elements as you like.

•ASP.NET MVC provides better support for test-driven development (TDD).

•ASP.NET MVC works well for web applications that are supported by large teams of developers and for web designers who need a high degree of control over the HTML. ASP.NET MVC Request Processing

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.