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
        }
    }
\$\endgroup\$
  • \$\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\$ – 200_success Jul 28 '17 at 6:45
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.

\$\endgroup\$
  • 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\$ – 200_success Jul 27 '17 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\$ – Duncan Babbage Jul 27 '17 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\$ – lifewithelliott Jul 27 '17 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\$ – lifewithelliott Jul 28 '17 at 3:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.