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.

I've seen a lot of stuff that describes how it's done, but not a lot that tells WHY it's done. Is it just a way to keep the code readable, or is there a better reason?

share|improve this question
add comment

migrated from stackoverflow.com Mar 28 '12 at 9:02

This question came from our site for professional and enthusiast programmers.

3 Answers

up vote 13 down vote accepted

It's about Separation of Concerns. Basically, you want your software components to do one thing, and one thing only. If your components start doing more than one thing, code gets hard to read, things become more complicated than they need to be -- basically an app turns into a big plate of Spagetti Code.

The Models contain the business logic, represent the things in your application. The views present the data to the user. The controllers decide what to do with the various user actions. When you stick to that, the code is easy to read because things are as simple as possible.

MVC is a popular pattern, but it's not the only one out there. MVVM is also popular. As long as you separate your concerns, pick any design architecture that suits you.

share|improve this answer
add comment

Thats a big question I don't have time for that big of an answer, so I'll try to keep it short.

The primary reason is like you said for keeping the code readable and manageable. The separation of these main components model, view and controller help to create more reusable code that is easier to understand because each piece has a distinct purpose. The model representing the objects or data within the system and containing the code for the actions that can be performed on those objects. The view of course encapsulating the layout and design that allows you to view that data. And finally the controller that allows you to navigate from view to view, action to action.

If you have ever dealt with a large system of code that married the three of these together, you will have seen the typical copying of large chunks of redundant code that was not quite what was needed for the next task. These models are built in such a way that they are more generic making them more reusable than it was inline. That is you dont have SQL statements scattered throughout your scripts instead it is consolidated. The same is true of HTML being scattered in code, very undesirable and hard to edit and maintain.

Hope that helps.

share|improve this answer
    
Wow! You started your answer saying 'don't have time for the big answer' and look! your's is the longest one! :D –  Jack_of_All_Trades Mar 27 '12 at 20:17
    
Ah now that is not a big answer compared to the question. –  AaronM Mar 27 '12 at 21:42
add comment

The actual purpose of MVC is to separate your views from your controller and model. In other words, it is a design pattern is a structure for keeping display and data separate to allow each to change without affecting the other. By saying so, it is mostly used for GUI stuffs. Thus it essentially contains three things. The Model contains what is often called business logic, or all the data and information manipulated by your system. The View contains the objects that display the data, and the Controller manages the interaction with the user and mediates between the Model and the View.

In many modern GUI toolkits, view and controllers are somewhat inter-related and thus cannot be fully separated. For example, a button is a controller but it needs to be displayed in the screen.

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.