178
RENÉ CACHEAUX MOBILE ARCHITECT ATLASSIAN @RCACHATX Coding for Humans not computers

Writing Code for Humans, not Computers

Embed Size (px)

Citation preview

Page 1: Writing Code for Humans, not Computers

RENÉ CACHEAUX • MOBILE ARCHITECT • ATLASSIAN • @RCACHATX

Coding for Humansnot computers

Page 2: Writing Code for Humans, not Computers

It’s not easy, we don’t teach it, it’s not natural.

Page 3: Writing Code for Humans, not Computers

What’s the big deal?

Readable = Fun

Code is Hard to Read

Time is MoneyWe can get time back to do all the things we love.

Code reviews, fixing bugs, adding new features all require reading.

Easy to read code is fun to work with and who doesn’t want fun?

Page 4: Writing Code for Humans, not Computers

🤐

Page 5: Writing Code for Humans, not Computers

• Big cool statistic

• 2,569

• Add-Ons in Marketplace

Tabs vs Spaces

Page 6: Writing Code for Humans, not Computers

• Big cool statistic

• 2,569

• Add-Ons in Marketplace

Prototypes

Page 7: Writing Code for Humans, not Computers

• Big cool statistic

• 2,569

• Add-Ons in Marketplace

Page 8: Writing Code for Humans, not Computers

• Big cool statistic

• 2,569

• Add-Ons in Marketplace

Let’s Dive In

Page 9: Writing Code for Humans, not Computers

C O D I N G F O R H U M A N S

S TAT E O F A F FA I R S

P U T T I N G I N TO P R A C T I C E

Agenda

Page 10: Writing Code for Humans, not Computers

C O D I N G F O R H U M A N S

S TAT E O F A F FA I R S

P U T T I N G I N TO P R A C T I C E

Agenda

Page 11: Writing Code for Humans, not Computers

State of Affairs

We get Acclimated

It works! Done 😌

Fast, Faster…We are under constant pressure to deliver fast.

Difference between problem solving and building production code.

After years and years we get used to things and don’t see an issue.

Page 12: Writing Code for Humans, not Computers

State of Affairs

We Read A Lot

Smart CompilersLike, really smart.

When developing, we spend most time reading.

Page 13: Writing Code for Humans, not Computers

Indeed, the ratio of time spent reading versus writing is well over 10 to 1. We are constantly reading old code as part of the effort to write new code. ...[Therefore,] making it easy to read makes it easier to write.

R O B E R T C . M A R T I N , C L E A N C O D E : A H A N D B O O K O F A G I L E S O F T WA R E C R A F T S M A N S H I P

Page 14: Writing Code for Humans, not Computers

Opening a Project

Page 15: Writing Code for Humans, not Computers

Someone, somewhere is

reading your code from 5 years ago…

Page 16: Writing Code for Humans, not Computers

C O D I N G F O R H U M A N S

S TAT E O F A F FA I R S

P U T T I N G I N TO P R A C T I C E

Agenda

Page 17: Writing Code for Humans, not Computers

• Big cool statistic

• 2,569

• Add-Ons in Marketplace

So what can we do about it?

Page 18: Writing Code for Humans, not Computers

Design Great Reading Experiences

Page 19: Writing Code for Humans, not Computers

Humans are complexWe all come from different cultures, different backgrounds, and different skill levels.

Page 20: Writing Code for Humans, not Computers

We ❤ Patterns

Page 21: Writing Code for Humans, not Computers

C O D I N G F O R H U M A N S

S TAT E O F A F FA I R S

P U T T I N G I N TO P R A C T I C E

Agenda

Page 22: Writing Code for Humans, not Computers

C O D I N G F O R H U M A N S

S TAT E O F A F FA I R S

P U T T I N G I N TO P R A C T I C E

Agenda

Page 23: Writing Code for Humans, not Computers

Coding for Humans

Writing Code

Structuring Code

Building ProjectsFinding code quickly and welcoming newcomers.

Dividing code up to make code easy to understand.

Writing code that’s easy to read.

Page 24: Writing Code for Humans, not Computers

Coding for Humans

Writing Code

Structuring Code

Building ProjectsFinding code quickly and welcoming newcomers.

Dividing code up to make code easy to understand.

Writing code that’s easy to read.

Page 25: Writing Code for Humans, not Computers

• Big cool statistic

• 2,569

• Add-Ons in Marketplace

On-boarding

Page 26: Writing Code for Humans, not Computers

README.mdProvide a high level overview of how things are structured and leave the details out.

Page 27: Writing Code for Humans, not Computers

PatternsArchitectureOrganization Vocabulary

What should go in a README?

Page 28: Writing Code for Humans, not Computers

• Big cool statistic

• 2,569

• Add-Ons in Marketplace

Organizing Source

Page 29: Writing Code for Humans, not Computers

Before you can read something you have to

find the code.

Page 30: Writing Code for Humans, not Computers

Tell a Story Mapping View Controllers as Packages

Organizing Source

Page 31: Writing Code for Humans, not Computers

Robert Martin, ‘Uncle Bob’

Page 32: Writing Code for Humans, not Computers

Screaming Architecture

Page 33: Writing Code for Humans, not Computers

Example

Page 34: Writing Code for Humans, not Computers

Welcome Twitter

Sign Up

Log In

User’s Journey

Page 35: Writing Code for Humans, not Computers

Follows user’s journey, topmost being earliest and bottommost being latest.

Page 36: Writing Code for Humans, not Computers

Each view controller has its own group, acts like a package.

Page 37: Writing Code for Humans, not Computers

Callout

App Tab Bar

Page 38: Writing Code for Humans, not Computers
Page 39: Writing Code for Humans, not Computers

Callout

App Tab Bar

Nav Bar

Tabs

Page 40: Writing Code for Humans, not Computers

Follows vertical placement on screen.

Page 41: Writing Code for Humans, not Computers

Callout

App Tab Bar

Nav Bar

Page 42: Writing Code for Humans, not Computers

Search Compose Search Compose Search Compose

Page 43: Writing Code for Humans, not Computers

Global nav bar screens are placed at the root, follows placement left to right.

Page 44: Writing Code for Humans, not Computers

More examples of view controllers as packages.

Page 45: Writing Code for Humans, not Computers

Callout

App Tab Bar

Tabs

Page 46: Writing Code for Humans, not Computers

Follows left to right positioning of tabs.

Page 47: Writing Code for Humans, not Computers

Callout

App Tab Bar

Home Tab

Page 48: Writing Code for Humans, not Computers

Nav Bar

Nav Stack

Home Tab Actions

Page 49: Writing Code for Humans, not Computers

Home tab navigation based group organization.

Page 50: Writing Code for Humans, not Computers

Connect

Nav Bar

Page 51: Writing Code for Humans, not Computers

Home tab’s nav bar connect item.

Page 52: Writing Code for Humans, not Computers

Open Tweet Detail

Nav Stack

Page 53: Writing Code for Humans, not Computers

Home Tab Navigation

Tweet List Tweet Detail

Page 54: Writing Code for Humans, not Computers

Groups organized by nav stack order.

Page 55: Writing Code for Humans, not Computers

Mapping App to Source Organization

Temporal Physical

Page 56: Writing Code for Humans, not Computers

Re-usable Code

Page 57: Writing Code for Humans, not Computers

Re-usable code organized separately.

Page 58: Writing Code for Humans, not Computers

Screaming Architecture

Page 59: Writing Code for Humans, not Computers

What app is this?

Page 60: Writing Code for Humans, not Computers

What app is this?

Page 61: Writing Code for Humans, not Computers

Organize by domain instead of by technology.

Page 62: Writing Code for Humans, not Computers

Why use this?

Page 63: Writing Code for Humans, not Computers

Easier to manageEasy to Find CodeEasy to Read Cleaner Diffs

Why use this?

Page 64: Writing Code for Humans, not Computers

Side TipWe read code outside of Xcode, so make sure to mirror Xcode project group structure with file system.

Page 65: Writing Code for Humans, not Computers

Coding for Humans

Writing Code

Structuring Code

Building ProjectsFinding code quickly and welcoming newcomers.

Dividing code up to make code easy to understand.

Writing code that’s easy to read.

Page 66: Writing Code for Humans, not Computers

6666

UNDERLYING PRINCIPLE

Make it easy for you and your team to find code.

Page 67: Writing Code for Humans, not Computers

Coding for Humans

Writing Code

Structuring Code

Building ProjectsFinding code quickly and welcoming newcomers.

Dividing code up to make code easy to understand.

Writing code that’s easy to read.

Page 68: Writing Code for Humans, not Computers

Coding for Humans

Writing Code

Structuring Code

Building ProjectsFinding code quickly and welcoming newcomers.

Dividing code up to make code easy to understand.

Writing code that’s easy to read.

Page 69: Writing Code for Humans, not Computers

• Big cool statistic

• 2,569

• Add-Ons in Marketplace

View Controllers as Components

Page 70: Writing Code for Humans, not Computers

Component-based software engineering … is a reuse-based approach to defining, implementing and composing loosely coupled independent components into systems.W I K I P E D I A

Page 71: Writing Code for Humans, not Computers

View controllers are the foundation of your app’s internal structure … Because they play such an important role in your app, view controllers are at the center of almost everything you do.V I E W C O N T R O L L E R P R O G R A M M I N G G U I D E F O R I O S , A P P L E

Page 72: Writing Code for Humans, not Computers

Independent Loosely Coupled

Page 73: Writing Code for Humans, not Computers

Life of a Typical View Controller

🙈

Page 74: Writing Code for Humans, not Computers

Instead

Page 75: Writing Code for Humans, not Computers

View Controller Package

Page 76: Writing Code for Humans, not Computers

DependenciesAnything not inside the view controller’s group is an external dependency. Try to inject.

Page 77: Writing Code for Humans, not Computers

Break it Down

Child View Controller

Child View Controller

Page 78: Writing Code for Humans, not Computers

Break it Down

Cell Actions

Cell Actions

Page 79: Writing Code for Humans, not Computers

Break it Down

Each Cell as a View Controller

Page 80: Writing Code for Humans, not Computers

View controller containment is your friend 😍!

Page 81: Writing Code for Humans, not Computers

Why use this?

Page 82: Writing Code for Humans, not Computers

Easy to Read Single Responsibility

Incremental & Parallelize

Why use this?

Page 83: Writing Code for Humans, not Computers

• Big cool statistic

• 2,569

• Add-Ons in Marketplace

Use Cases

Page 84: Writing Code for Humans, not Computers

… a use case is a list of actions or event steps, typically defining the interactions between a role [user] and a system, to achieve a goal.W I K I P E D I A

Page 85: Writing Code for Humans, not Computers

Like TweetRefresh Tweet ListLog In Create Tweet

Twitter Use Cases

Page 86: Writing Code for Humans, not Computers

How can I use this?

Page 87: Writing Code for Humans, not Computers

Use Case + Command Pattern

Page 88: Writing Code for Humans, not Computers

Object-Oriented Software Engineering: A Use Case Driven Approach

Page 89: Writing Code for Humans, not Computers

Robert Martin, ‘Uncle Bob’

Page 90: Writing Code for Humans, not Computers

Use Case + Command Pattern

Page 91: Writing Code for Humans, not Computers

Use Case + NSOperation💑

Page 92: Writing Code for Humans, not Computers

Check out Apple’s ‘Concurrency

Programming Guide’

Page 93: Writing Code for Humans, not Computers

• Big cool statistic

• 2,569

• Add-Ons in Marketplace

Think of them as a recipe

Page 94: Writing Code for Humans, not Computers

Types of NSOperations

Sync (default) Async

Page 95: Writing Code for Humans, not Computers

Like TweetRefresh Tweet ListLog In Create Tweet

Twitter Use Cases

Page 96: Writing Code for Humans, not Computers

Twitter Use Cases

Page 97: Writing Code for Humans, not Computers

What typically goes into a Use Case NSOperation?

Page 98: Writing Code for Humans, not Computers

Notify to ReactNSData -> ModelsHTTP Get Persist Models

Typical Refresh Use Case

Page 99: Writing Code for Humans, not Computers

View controllers create & schedule use cases.

Page 100: Writing Code for Humans, not Computers

Cell View Controller

Cell Actions

Cell Actions

Page 101: Writing Code for Humans, not Computers

Why use this?

Page 102: Writing Code for Humans, not Computers

TestableReusableEasy to Read Flexibility

Why use this?

Page 103: Writing Code for Humans, not Computers

Coding for Humans

Writing Code

Structuring Code

Building ProjectsFinding code quickly and welcoming newcomers.

Dividing code up to make code easy to understand.

Writing code that’s easy to read.

Page 104: Writing Code for Humans, not Computers

104104

UNDERLYING PRINCIPLE

Architecture plays a significant role. Pick one and stick with it.

Page 105: Writing Code for Humans, not Computers

Coding for Humans

Writing Code

Structuring Code

Building ProjectsFinding code quickly and welcoming newcomers.

Dividing code up to make code easy to understand.

Writing code that’s easy to read.

Page 106: Writing Code for Humans, not Computers

Coding for Humans

Writing Code

Structuring Code

Building ProjectsFinding code quickly and welcoming newcomers.

Dividing code up to make code easy to understand.

Writing code that’s easy to read.

Page 107: Writing Code for Humans, not Computers

• Big cool statistic

• 2,569

• Add-Ons in Marketplace

Writing Code

Page 108: Writing Code for Humans, not Computers

Example

Page 109: Writing Code for Humans, not Computers
Page 110: Writing Code for Humans, not Computers

Negated condition.

Page 111: Writing Code for Humans, not Computers

Nested multiple levels.

Page 112: Writing Code for Humans, not Computers

Has to run at the end.

Page 113: Writing Code for Humans, not Computers
Page 114: Writing Code for Humans, not Computers
Page 115: Writing Code for Humans, not Computers

Positive condition & removed one level of nesting.

Page 116: Writing Code for Humans, not Computers

Will always run at the end of the method.

Page 117: Writing Code for Humans, not Computers
Page 118: Writing Code for Humans, not Computers

Silly NSNotification user info access.

Page 119: Writing Code for Humans, not Computers
Page 120: Writing Code for Humans, not Computers
Page 121: Writing Code for Humans, not Computers

New KeyboardAnimation type with failable initializer.

Page 122: Writing Code for Humans, not Computers

New KeyboardAnimation type with failable initializer.

Page 123: Writing Code for Humans, not Computers
Page 124: Writing Code for Humans, not Computers

Animation looks very generic, not specific to this method.

Page 125: Writing Code for Humans, not Computers
Page 126: Writing Code for Humans, not Computers
Page 127: Writing Code for Humans, not Computers

New method added to UIView via extension.

Page 128: Writing Code for Humans, not Computers

New method added to UIView via extension.

Page 129: Writing Code for Humans, not Computers
Page 130: Writing Code for Humans, not Computers

Still too many nested levels! 2 levels deep.

Page 131: Writing Code for Humans, not Computers
Page 132: Writing Code for Humans, not Computers
Page 133: Writing Code for Humans, not Computers
Page 134: Writing Code for Humans, not Computers
Page 135: Writing Code for Humans, not Computers
Page 136: Writing Code for Humans, not Computers

Removed one level with a guard.

Page 137: Writing Code for Humans, not Computers

Removed another level by moving logic into updateComposeFeedbackViewBottomConstraintConstant()

Page 138: Writing Code for Humans, not Computers
Page 139: Writing Code for Humans, not Computers

Two lines look related. Guard is only making sure there’s a window.

Page 140: Writing Code for Humans, not Computers
Page 141: Writing Code for Humans, not Computers
Page 142: Writing Code for Humans, not Computers

Simplified by extending UIWindow.

Page 143: Writing Code for Humans, not Computers

Simplified by extending UIWindow.

Page 144: Writing Code for Humans, not Computers
Page 145: Writing Code for Humans, not Computers

NSNotification is more of a distraction here.

Page 146: Writing Code for Humans, not Computers
Page 147: Writing Code for Humans, not Computers

Moved into new method that takes the keyboard animation.

Page 148: Writing Code for Humans, not Computers

Moved into new method that takes the keyboard animation.

Page 149: Writing Code for Humans, not Computers
Page 150: Writing Code for Humans, not Computers

Oh yea, and one last thing…

Page 151: Writing Code for Humans, not Computers
Page 152: Writing Code for Humans, not Computers

Before

Page 153: Writing Code for Humans, not Computers

After

Page 154: Writing Code for Humans, not Computers

The Art of Readable Code

Page 155: Writing Code for Humans, not Computers

Stay PositiveReturn EarlyAvoid Nesting Create New Types

Best Practices we Used

{ return <T> +

Page 156: Writing Code for Humans, not Computers

Optional ChainingGuardFailable Initializers Defer

Swifty Things we Used

? g { d { .?

Page 157: Writing Code for Humans, not Computers

Tons of other Swifty things…

Page 158: Writing Code for Humans, not Computers

Swift API Design Guidelines

Page 159: Writing Code for Humans, not Computers

Why use this?

Page 160: Writing Code for Humans, not Computers

Easy to Read

Why use this?

Page 161: Writing Code for Humans, not Computers

• Big cool statistic

• 2,569

• Add-Ons in Marketplace

Interface Builder 🙈

Page 162: Writing Code for Humans, not Computers

Coding for Humans

Writing Code

Structuring Code

Building ProjectsFinding code quickly and welcoming newcomers.

Dividing code up to make code easy to understand.

Writing code that’s easy to read.

Page 163: Writing Code for Humans, not Computers

163163

UNDERLYING PRINCIPLE

Writing is hard, spend time researching and experimenting.

Page 164: Writing Code for Humans, not Computers

Coding for Humans

Writing Code

Structuring Code

Building ProjectsFinding code quickly and welcoming newcomers.

Dividing code up to make code easy to understand.

Writing code that’s easy to read.

Page 165: Writing Code for Humans, not Computers

Coding for Humans

Writing Code

Structuring Code

Building ProjectsFinding code quickly and welcoming newcomers.

Dividing code up to make code easy to understand.

Writing code that’s easy to read.

Page 166: Writing Code for Humans, not Computers

C O D I N G F O R H U M A N S

S TAT E O F A F FA I R S

P U T T I N G I N TO P R A C T I C E

Agenda

Page 167: Writing Code for Humans, not Computers

• Big cool statistic

• 2,569

• Add-Ons in Marketplace

So those are just a few tips and tricks, where to go from here?

Page 168: Writing Code for Humans, not Computers

C O D I N G F O R H U M A N S

S TAT E O F A F FA I R S

P U T T I N G I N TO P R A C T I C E

Agenda

Page 169: Writing Code for Humans, not Computers

C O D I N G F O R H U M A N S

S TAT E O F A F FA I R S

P U T T I N G I N TO P R A C T I C E

Agenda

Page 170: Writing Code for Humans, not Computers

Putting into Practice

Page 171: Writing Code for Humans, not Computers

Putting into Practice

Automate

Code Review

Team Buy-inThe entire team needs to row together on this.

Be nit-picky at first, the team will get a rhythm going.

Automate as much as possible using scripts. Help and detection.

Page 172: Writing Code for Humans, not Computers

Re-organizingMake sure to stop development while re-organizing Xcode projects.

Page 173: Writing Code for Humans, not Computers

C O D I N G F O R H U M A N S

S TAT E O F A F FA I R S

P U T T I N G I N TO P R A C T I C E

Agenda

Page 174: Writing Code for Humans, not Computers

C O D I N G F O R H U M A N S

S TAT E O F A F FA I R S

P U T T I N G I N TO P R A C T I C E

That’s It

Page 175: Writing Code for Humans, not Computers

We are all engineersWe want to build 😀 things and we want to do it in a reasonable time so we can enjoy other hobbies even coding.

Page 176: Writing Code for Humans, not Computers

Less BugsFunDeliver FasterSlow at first, lightning fast once established.

It’s fun when it’s easy to read and build.

More team members will understand.

Benefits

Page 177: Writing Code for Humans, not Computers

• Big cool statistic

• 2,569

• Add-Ons in Marketplace

Shoot for the moon!

Page 178: Writing Code for Humans, not Computers

Thank you! 🍻

RENÉ CACHEAUX • MOBILE ARCHITECT • ATLASSIAN • @RCACHATX

Don’t forget to grab a handout.