Top Banner
A Better MVC “You’re holding it wrong.” Dave DeLong – @davedelong A guy who thinks too deeply about stu
29

A Better MVC - Dave DeLong

Feb 20, 2023

Download

Documents

Khang Minh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: A Better MVC - Dave DeLong

A Better MVC“You’re holding it wrong.”

Dave DeLong – @davedelongA guy who thinks too deeply about stuff

Page 2: A Better MVC - Dave DeLong

Massive View Controller

Page 3: A Better MVC - Dave DeLong
Page 4: A Better MVC - Dave DeLong

(This is our own fault)

Page 5: A Better MVC - Dave DeLong

–Literally no one, ever

“I really think we should follow bad programming principles, violate encapsulation, and make things tightly coupled.”

Page 6: A Better MVC - Dave DeLong

Why do we make this mistake?• Apple “tells us” to

• Revert to the default

• External constraints

Page 7: A Better MVC - Dave DeLong

–George Santayana

“…when experience is not retained, … infancy is perpetual. Those who cannot remember the past are condemned to repeat it.”

Page 8: A Better MVC - Dave DeLong

Making some observationsHuh, that’s interesting…

Page 9: A Better MVC - Dave DeLong

#1: MVC is not a Pattern• MVC is a philosophy

• Separate storage from networking from parsing from routing from business logic from rendering from view hierarchy from …

• “Who should care about this logic?”

Page 10: A Better MVC - Dave DeLong

#2: Patterns are Tools• Similar problems → similar solutions → similar patterns

• Different problems → different solutions → different patterns

• Good developers learn patterns

• Great developers learn problems

Page 11: A Better MVC - Dave DeLong

#3: Naming is Hard• Just because it has "Controller" in the name, doesn't mean it's a

Controller

• UIViewController is not a Controller

• It performs view-related things

Page 12: A Better MVC - Dave DeLong

#4: Views don’t fill the screen• Your screen of app UI is not a single UIView

• Why is your screen of app UI a single UIViewController?

• UIViewControllers don’t have to fill the screen

Page 13: A Better MVC - Dave DeLong

Thinking this throughThat’s nice; so what?

Page 14: A Better MVC - Dave DeLong

#1: Decompose UIViewControllers• Decomposition is the fundamental skill of programming

• Break apart your UIViewControllers

• A UIViewController that …

• Only shows an image?

• Only shows a single horizontal line?

• Is a cell in a UITableView or UICollectionView?

Page 15: A Better MVC - Dave DeLong

#2: Compose UI• Build your UI by composing UIViewControllers

• UIViewController.addChild(_:) // added in iOS 5

Page 16: A Better MVC - Dave DeLong

#3: Reuse UIViewControllers• You don’t rewrite UILabel every time you need to show text

• Expect to use build and re-use UIViewControllers

• ContainerViewController

• StackViewController

• ScrollingContentViewController

• Accidental consistency

Page 17: A Better MVC - Dave DeLong

#4: Forget UIView• You'll rarely subclass UIView

• UIView is for rendering or interaction

• Render UI with UILabel and UIImageView

• Handle interaction with UIGestureRecognizer

• Consider composing complex views in a UIViewController

Page 18: A Better MVC - Dave DeLong

Putting this in to practiceYour ideas intrigue me and I wish to subscribe to your newsletter

Page 19: A Better MVC - Dave DeLong

#1: Show Data or Children• Generally, UIViews either compose or render

• Rendering happens with UILabel and UIImageView

• Everything else composes those

• Aim for the same with UIViewControllers

Page 20: A Better MVC - Dave DeLong

#2: Think with generic functions

Some ObjectInput Output

Page 21: A Better MVC - Dave DeLong

ListViewController<T>

Array<T>

Item 1

Item 2

Item 3

Item 4

• • •

shouldSelect(_ item: T) -> Bool

willShow(_ item: T)

perform(_ action: Action<T>)

Page 22: A Better MVC - Dave DeLong

#2: Think with generic functions• (T) → T and (T) → Void

• “What goes in, must come out”

• Input: datasource, parameters, signal/observable…

• Output: delegate, callbacks, signal/observable…

• Violating this violates encapsulation

Page 23: A Better MVC - Dave DeLong

#3: Prefer XIBs over Storyboards• Segues are Problematic™

• Navigation violates the “(T) → T” constraint

• They break reusability

• -prepareForSegue: is called on the wrong object

• XIBs have a 1-to-1 relation between UIViewController and UI

• Can still use outlets and custom initializers

Page 24: A Better MVC - Dave DeLong

#3: Prefer XIBs over Storyboards

Detail ViewDetail ViewMaster List

show(detailViewController, sender: self)

performSegue(withIdentifier: “showDetail”, sender: self)

Shows

Page 25: A Better MVC - Dave DeLong

#3: Prefer XIBs over Storyboards

Detail ViewDetail ViewMaster List

show(detailViewController, sender: self)

performSegue(withIdentifier: “showDetail”, sender: self)

Shows

Page 26: A Better MVC - Dave DeLong

#3: Prefer XIBs over Storyboards

Detail View

Detail View

Master List

Coordinator

Notifies of selection Shows

Page 27: A Better MVC - Dave DeLong

#4: Adopt this piecemeal• Don’t rewrite; refactor!

• Refactor to composition as able

Page 28: A Better MVC - Dave DeLong

Results• Tiny UIViewControllers (usually under 200 lines)

• Rarely subclass UIView

• Extremely reusable UIViewControllers

• Clear separation of concern

• Happy programmers "

Page 29: A Better MVC - Dave DeLong

social media

website

email

[email protected]

Thanks!