Live data from Hacker News

Ask YC: is MVC the best solution?

news.ycombinator.com

1–10 of 20 posts

Ask YC: is MVC the best solution?

#1
Somehow my intuition is always a little bit at odds with MVC (as found in current web frameworks). It feels more natural that the view would request the data it needs, especially if it is putting together a mosaic of different sources.

For example, I guess most web applications have some sort of layout view and include the content into it. But what if there are different kinds of things to include? For example there could be a list of logged in users to be displayed in the sidebar, or any number of widgets. It seems unnatural to have to gather the data for those in every controller call.

My experience is with Java frameworks, where usually the flow is through one controller - how does for example Rails deal with that sort of thing? For Java I guess the proposed solution is Portlets, but I haven't seen their wide adaption yet, and they seem to be another headache (those Java specs should be made shorter and more concise).

What is the best solution?

Re: Ask YC: is MVC the best solution?

#2
I tried using Struts for a project a few years back and after I realised how much easier it was to maintain the codebase with a proper enfored separation, I never looked back.

Though I found myself, in many cases, writing more code than I would have without a framework, I found the maintenance benefit more than compensates.

Things have got more complex in recent years with more and more complex interaction logic embedded in webpages with Javascript and i'm not sure i've seen many frameworks address this.

Admittedly, there are now better frameworks out there than Struts (this was Struts 1) and I think i'll have to do a bit of exploring for my new project's website but we'll see.

Re: Ask YC: is MVC the best solution?

#3
The Best Solution for what? Client side mashups? Greenfield development? Mixed-platform interop? Speed and threading? Flexibility with changing requirements?

Not trying to be a jerk, it's just a complicated question. I guess you mean greenfield web applications? Even then I'd have to ask things like how heavy you were client-side, what languages you knew -- most importantly, where's all the data? If you're delivering data basically from the server to the client and back, does most of the processing take place on the client or the server? Some MVC paradigms are crazy to push into Javascript. Some are crazy to use for simple projects.

Lately for Web 2.0 Applications, I'm using a framework that has a lightweight MVC (very lightweight!) in Javascript and pulls information using JSON from various sources. But I wrote it myself, so I'm biased.

MVC is a paradigm that can be applied a lot of ways, in a lot of situations. Most frameworks emphasize the controller, when the model is really what you're after. Your controller classes can "dumb down" in many scenarios to be so lightweight as to not require a separate class. Hard to do that with a pre-canned framework. I guess I would be extremely careful in confusing the framework with the architectural conceit it uses -- two different concepts.

Re: Ask YC: is MVC the best solution?

#5
Rails has filters that can run before, after, or around some or all of your controller actions. This can be done on a per controller basis, or it can be application wide. In this case you'd probably define a "load_widgets" before_filter in your application controller, then set your view to display whichever widgets it had data for.

Re: Ask YC: is MVC the best solution?

#6
The biggest benefit of MVC, in my view, is the amount of reuse it encourages. When a view has no knowledge of what it has to do, you can reuse it an infinite amount of times. A table view is a table view, it shouldn't need to know what its displaying. A button shouldn't need to know anything about your app, because its just a button. This is what makes Cocoa/IB so powerful. In some instances, you don't even need to write a controller, you can just use a stock controller, like Cocoa's array controller.

I would also say that this is one of the biggest drawbacks of current client side web frameworks. Reusing views is much more difficult than it should be, especially if you want to do anything remotely custom or complex, since subclassing is essentially not possible and delegates are very infrequently used.

Re: Ask YC: is MVC the best solution?

#8

Rails has filters that can run before, after, or around some or all of your controller actions. This can be done on a per controller basis, or it can be application wide. In this case you'd probably define a "load_widgets" before_filter in your application controller, then set your view to display whichever widgets it had data for.

Similarly, Django has context processors for the same purpose.

Re: Ask YC: is MVC the best solution?

#9
I don't think the most tangible benefit of MVC is reuse, but rather code organization. In MVC, if you're looking for something, you know where to find it. That saves a lot of time. It's easy to write an application, but harder to maintain it.

Also, imagine an application with a Web view displaying a data set returned from a controller. Now the client/user wants an OS-native version of the app. That's simple, just build a native (OS-specific) window view and call the same controller for the data set (model). Or maybe an API is needed for external consumption. In that case a web service "view" can be built to return the same data set from the same controller. We're just swapping views here--it's plug-and-play.

It's a clear separation of responsibilities. I believe that used to be called "modular" programming. ;)

Re: Ask YC: is MVC the best solution?

#10
Many people accept the MVC dogma as the word of god. In truth, it is a design trade-off just like anything else. You should really look at SeaSide or UncommonWeb. In SeaSide the view and controller are combined into a component in the way you describe as intuitive. In Rails best practice is to create partials (partial views) that can be re-used across views, put code into the model instead of the controller, and use before_filter to trigger re-usable functions.
Post reply on HN