2
\$\begingroup\$

What the advantages & disadvantages to each of these approaches pertaining to creating file that takes care of the view configuration to reduce a controller's file size.

Main Purposes Are:

  • Memory
  • Performance
  • Testing
  • Usability

This is the simplest, capable of working, example to demonstrate the question, but when many views are present using many methods do any of the above concerns alter when comparing the Extension & ViewModel Class?

Reminder: The ViewModel Class or Extension would be placed in a separate file.

ViewModel Approach:

 class VC: UIViewController {
 lazy var viewModel: ViewModel {
 return (main: self)
 }()
 override viewDidLoad() {
 super.viewDidLoad()
 initializeUI()
 }
 func initializeUI() {
 viewModel.configureView()
 }
 }
 class ViewModel {
 private let main: UIViewController
 init(main: UIViewController) {
 self.main = main
 }
 func configureView() {
 main.view.backgroundColor = UIColor.blue
 }
 }

Extension Approach:

 class VC: UIViewController {
 lazy var viewModel: ViewModel(main: self)
 override viewDidLoad() {
 super.viewDidLoad()
 initializeUI()
 }
 func initializeUI() {
 configureView()
 }
 }
 extension VC {
 func configureView() {
 main.view.backgroundColor = UIColor.blue
 }
 }
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Jul 23, 2017 at 19:38
\$\endgroup\$
1
  • \$\begingroup\$ Please see What to do when someone answers . If someone mentions an issue with your code in an answer, you can't just roll the advice back into the question. \$\endgroup\$ Commented Jul 28, 2017 at 6:45

1 Answer 1

1
\$\begingroup\$

The viewModel property on the Extension example should be removed as it is redundant:

lazy var viewModel: ViewModel(main: self)

Irrespective of this, it seems unnecessary to create a separate ViewModel class solely for the purpose of encapsulating a configureView function. On that basis, the Extension approach seems like a much more sensible and performant way of managing this kind of configuration.

answered Jul 27, 2017 at 5:02
\$\endgroup\$
4
  • 2
    \$\begingroup\$ No, you can't just edit the question like it's a collaborative coding wiki! Please put your suggestions in your own answer. \$\endgroup\$ Commented Jul 27, 2017 at 5:05
  • \$\begingroup\$ Done. The edit was originally made the question because the original form actually slightly obscured the true difference between the two approaches, but happy to understand the convention here. More familiar with SO. \$\endgroup\$ Commented Jul 27, 2017 at 5:13
  • \$\begingroup\$ @Duncan Babbage Ah yes the lazy var was accidentally placed in the extension approach! And now looking back you are 100% correct, I appreciate it. \$\endgroup\$ Commented Jul 27, 2017 at 6:37
  • \$\begingroup\$ @DuncanBabbage the biggest thing with placing code in other files is the removal of private or fileprivate - It is definitely a struggle for me to figure out how to keep file size small while retaining private and fileprivate \$\endgroup\$ Commented Jul 28, 2017 at 3:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.