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 have developed an Android application where, typically, each view (layout.xml) displayed on the screen has it's own corresponding fragment (for the purpose of this question I may refer to this as a ViewController).

These views and Fragments/ViewControllers are appropriately named to reflect what they display. So this has the effect of allowing the programmer to easily pinpoint the files associated with what they see on any given screen.

The above refers to the one to one part of my question.

Please note that with the above there are a few exceptions where very similar is displayed on two views so the ViewController is used for two views. (Using a simple switch (type) to determine what layout.xml file to load)

On the flip side. I am currently working on the iOS version of the same app, which I didn't develop. It seems that they are adopting more of a one-to-many (ViewController:View) approach.

There appears to be one ViewController that handles the display logic for many different types of views. In the ViewController are an assortment of boolean flags and arrays of data (to be displayed) that are used to determine what view to load and how to display it.

This seems very cumbersome to me and coupled with no comments/ambiguous variable names I am finding it very difficult to implement changes into the project.

What do you guys think of the two approaches? Which one would you prefer? I'm really considering putting in some extra time at work to refactor the iOS into a more 1:1 oriented approach.

My reasoning for 1:1 over M:1 is that of modularity and legibility. After all, don't some people measure the quality of code based on how easy it is for another developer to pick up the reigns or how easy it is to pull a piece of code and use it somewhere else?

share|improve this question

4 Answers 4

First of all Android and iOS are different platforms, what is very efficient in one, may be not so good in the other. But honestly, I have no idea, because I haven't read into them. I use the 1:1 approach because its easy, and I never got performance issues that led me to that area.

As to the quality of code, you are right. Readability, and "how easy it is to pull a piece of code" is defiantly a measurement. Sometimes I leave some inefficient code with a comment just so I can later understand what the hell I did there.

To your problem, well its up to you to decide if you want to be a nice guy, and make it easy on the next programmer or just get the job done. (consider: will the next programmer be you?)

share|improve this answer

There appears to be one ViewController that handles the display logic for many different types of views. In the ViewController are an assortment of boolean flags and arrays of data (to be displayed) that are used to determine what view to load and how to display it.

It's typical in iOS apps to have one view controller instance for each "screenful" of content, i.e. a view and its subviews, similar to what you'd get from a layout in Android. If the app you're working on uses a single view controller object, or even separate instances of the very same view controller class, to manage a number of different screens then you're probably right that the code should be refactored. Before you jump into that project, though, read Apple's View Controller Programming Guide so that you have a solid understanding of how view controllers work together in iOS.

share|improve this answer

I would not prefer either, I would prefer the one that has least ifs, switches, auxiliary state variables and code duplication.

Usually it's 1:1 with auxiliary classes that take care of displaying information that appears in multiple places, but that's not the rule. The rule is whatever has fewer branches without causing code duplication in that particular situation.

share|improve this answer

Disclosure - my approach may or may not align with strict interpretations of MVC, MVP, or MVVM. But it is based upon my experience in working with those approaches.

In general, a VC should only have one View, but that's primarily driven by the fact that you won't have much overlap between Views. I think the more important aspect is an indifference upon the VC's part about what the View actually is. So if you need if and switch statements to couple your VC to your View then I think something is wrong.

The VC is there to drive data to the View but it doesn't care how the View presents the data as that's the View's job. Likewise, the View doesn't care how the VC gets the data, it just knows that it will. It's really the separation of responsibilities that's important.

"Well that's great" (~Denis Leary) but it doesn't address issues where you have duplication across the Views. This is where I would take a slightly bastardized approach and allow a VC to be composed of other VCs but maintain a relationship of 1 View to 1 VC. The "containing" VCs will bind to a View but the "contained" (child, members of containing VC, whatever you want to call it) won't necessarily have a View that they are bound to. This provides a degree of re-use within your code without overly cluttering things. It does fly in the face of some of the paradigms, so I would comment what you're doing.

Ultimately, you'll have one VC that handles retrieving | manipulating the data that is displayed on multiple Views. But you'll need an additional "layer" of VCs to keep the code structure clean.

Alternatively, just duplicate the code at the VC layer and re-use calls to the Model instead. I'm not a big fan of this as I like to think in terms of re-usable widgets when putting together my Views and ViewControllers.

share|improve this answer

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.