Views : ViewControllers, many to one, or one to one?

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?

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?)

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.

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.

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật