Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
7  
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 '11 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 '11 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. –  Kiran Ravindranathan Jul 21 '11 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. –  Kiran Ravindranathan Jul 21 '11 at 11:09
    
I totally agree with deadalnix. Dont go to this company. –  Rudy Jul 21 '11 at 11:25
add comment

4 Answers

up vote 30 down vote accepted

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.

share|improve this answer
1  
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). –  Jörg W Mittag Jun 20 '12 at 0:35
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
    
The timeline reference is a nice mention- logically supports the argument. –  Kiran Ravindranathan Jul 21 '11 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. –  Kiran Ravindranathan Jul 21 '11 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 '11 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. –  Péter Török Jul 21 '11 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 '11 at 15:50
show 1 more comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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