Develop Maintainable Apps

113
Be a Good Citizen: Develop Maintainable Apps

Transcript of Develop Maintainable Apps

Page 1: Develop Maintainable Apps

Be a Good Citizen: !

Develop Maintainable Apps

Page 2: Develop Maintainable Apps

“…a high probability of determining the cause of a problem in a timely manner… and a high

probability of being able to modify the program without causing an error in some other part…”

— IEEE

Page 3: Develop Maintainable Apps

Why Care?

Page 4: Develop Maintainable Apps
Page 5: Develop Maintainable Apps
Page 6: Develop Maintainable Apps
Page 7: Develop Maintainable Apps
Page 8: Develop Maintainable Apps
Page 9: Develop Maintainable Apps

What’s involved…

@brwngrldev

Page 10: Develop Maintainable Apps

What’s involved…

Libraries

1

@brwngrldev

Page 11: Develop Maintainable Apps

What’s involved…

Libraries

1

Domain Design

2

@brwngrldev

Page 12: Develop Maintainable Apps

What’s involved…

Libraries

1

Domain Design

2

Code Quality

3

@brwngrldev

Page 13: Develop Maintainable Apps

What’s involved…

Libraries

1

Domain Design

2

Code Quality

3

Testing

4

@brwngrldev

Page 14: Develop Maintainable Apps

Choosing the Right Library

Page 15: Develop Maintainable Apps

So many options

Page 16: Develop Maintainable Apps

So many options

Page 17: Develop Maintainable Apps

What to consider

@brwngrldev

Page 18: Develop Maintainable Apps

What to consider

Documentation

@brwngrldev

Page 19: Develop Maintainable Apps

What to consider

Documentation

Project Stability

@brwngrldev

Page 20: Develop Maintainable Apps

What to consider

Documentation

Project Stability

Fulfills a Need

@brwngrldev

Page 21: Develop Maintainable Apps

What to consider

Amazing App

!

Documentation

Project Stability

Fulfills a Need

@brwngrldev

Page 22: Develop Maintainable Apps

Example

Page 23: Develop Maintainable Apps

So you need images…

Page 24: Develop Maintainable Apps

So you need images…

Volley

!

Fresco

!

Glide

!

Picasso

!

Page 25: Develop Maintainable Apps

Volley

!

Page 26: Develop Maintainable Apps

Documentation s

Volley

!

Page 27: Develop Maintainable Apps
Page 28: Develop Maintainable Apps
Page 29: Develop Maintainable Apps

Documentation s

Volley

!

Page 30: Develop Maintainable Apps

Documentation s

W Project Stability

Volley

!

Page 31: Develop Maintainable Apps
Page 32: Develop Maintainable Apps

Documentation s

W Project Stability

Volley

!

Page 33: Develop Maintainable Apps

Documentation s

W Project Stability

p Fulfills a need

Volley

!

Page 34: Develop Maintainable Apps

@brwngrldev

Page 35: Develop Maintainable Apps

Rinse & Repeat

Volley

!

Fresco

!

Glide

!

Picasso

!

Page 36: Develop Maintainable Apps

Review…

• Documentation

• Repository Check-ins

• Fulfills a Need

Libraries

1

@brwngrldev

Page 37: Develop Maintainable Apps

Domain Design

Page 38: Develop Maintainable Apps

Doing too much

Page 39: Develop Maintainable Apps

Doing too much

Page 40: Develop Maintainable Apps

Seen this?

Page 41: Develop Maintainable Apps

Seen this?

Page 42: Develop Maintainable Apps

Seen this?

Page 43: Develop Maintainable Apps

Seen this?

Page 44: Develop Maintainable Apps

How to fix this

Page 45: Develop Maintainable Apps

Clean It Up!

How to fix this

Page 46: Develop Maintainable Apps

Clean It Up!

How to fix this

MVP

MVVM

OO

Page 47: Develop Maintainable Apps

Foo

What’s involved…

Page 48: Develop Maintainable Apps

What’s involved…

Foo Bar

Baz Lol

Omg

Page 49: Develop Maintainable Apps

Example

Page 50: Develop Maintainable Apps

Remember this?

Page 51: Develop Maintainable Apps

We need tests

Page 52: Develop Maintainable Apps

We need tests

Page 53: Develop Maintainable Apps

We need tests

Page 54: Develop Maintainable Apps

Extract a Presenter

Page 55: Develop Maintainable Apps

Create an Interface

Page 56: Develop Maintainable Apps

Reference the Interface

Page 57: Develop Maintainable Apps

Implement the Interface

Page 58: Develop Maintainable Apps

Foo Bar

Baz Lol

Omg

@brwngrldev

Page 59: Develop Maintainable Apps

Review…

• Keep Code Simple

• Use MV* Pattern

• Functional Tests

• Use the Tools

@brwngrldev

Domain Design

2

Page 60: Develop Maintainable Apps

Code Quality

Page 61: Develop Maintainable Apps

Poor Readability

Page 62: Develop Maintainable Apps

Poor Readability

Page 63: Develop Maintainable Apps

Increase Readability?

Page 64: Develop Maintainable Apps

Increase Readability?

“Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard.”

Page 65: Develop Maintainable Apps

Source

Files

config.xml

Checkstyle Tool

!

Modules

Design

Formatting

Code Complexity

Page 66: Develop Maintainable Apps

apply plugin: ‘checkstyle’ !

task checkstyle(type: Checkstyle) { description 'Checks if the code passes quality standards' group 'verification' configFile file(‘checkstyle.xml') …}

Page 67: Develop Maintainable Apps

apply plugin: ‘checkstyle’ !

task checkstyle(type: Checkstyle) { description 'Checks if the code passes quality standards' group 'verification' configFile file(‘checkstyle.xml') …}

Page 68: Develop Maintainable Apps

<module name=“MethodLength"> <property name="max" value=“60"/> </module> <module name=“LineLength"> <property name="max" value=“120"/> </module><module name=“CyclomaticComplexity"> <property name="max" value=“8"/> </module> …

Page 69: Develop Maintainable Apps

<module name=“MethodLength"> <property name="max" value=“60"/> </module> <module name=“LineLength"> <property name="max" value=“120"/> </module><module name=“CyclomaticComplexity"> <property name="max" value=“8"/> </module> …

Page 70: Develop Maintainable Apps

<module name=“MethodLength"> <property name="max" value=“60"/> </module> <module name=“LineLength"> <property name="max" value=“120"/> </module><module name=“CyclomaticComplexity"> <property name="max" value=“8"/> </module> …

playerControlConfig.setShowClosedCaptionsButton(a.getBoolean(R.styleable.WapoVideoView_showClosedCaptionsButton, false)); playerControlConfig.setShowClosedCaptionsButton(a.getBoolean(R.styleable.WapoVideoView_showClosedCaptionsButton, false)); !

Page 71: Develop Maintainable Apps

<module name=“MethodLength"> <property name="max" value=“60"/> </module> <module name=“LineLength"> <property name="max" value=“120"/> </module><module name=“CyclomaticComplexity"> <property name="max" value=“8"/> </module> …

playerControlConfig.setShowClosedCaptionsButton(a.getBoolean(R.styleable.WapoVideoView_showClosedCaptionsButton, false)); playerControlConfig.setShowClosedCaptionsButton(a.getBoolean(R.styleable.WapoVideoView_showClosedCaptionsButton, false)); !

Page 72: Develop Maintainable Apps

<module name=“MethodLength"> <property name="max" value=“60"/> </module> <module name=“LineLength"> <property name="max" value=“120"/> </module><module name=“CyclomaticComplexity"> <property name="max" value=“8"/> </module> …

Page 73: Develop Maintainable Apps

<module name=“MethodLength"> <property name="max" value=“60"/> </module> <module name=“LineLength"> <property name="max" value=“120"/> </module><module name=“CyclomaticComplexity"> <property name="max" value=“8"/> </module> …

Page 74: Develop Maintainable Apps

<module name=“CyclomaticComplexity"> <property name="max" value=“8"/> </module> !

Page 75: Develop Maintainable Apps

M = E − N + 2P

@brwngrldev

Page 76: Develop Maintainable Apps

M = E − N + 2P

@brwngrldev

Page 77: Develop Maintainable Apps

Example

Page 78: Develop Maintainable Apps

public void overlyComplexMethod(Video video) { if (video != null && video.getStreamUrl() != null) { switch (video.getCategory()) { case "CAT1" : playVideo(video); if (video.getLargeImageUrl() == null) { video.setLargeImageUrl("http://www.largeImage.png"); } updateMetadata(video); break; case "CAT2" : if (video.getLargeImageUrl() == null) { video.setLargeImageUrl("http://www.smallImage.png"); }

Page 79: Develop Maintainable Apps

case "CAT1" : playVideo(video); if (video.getLargeImageUrl() == null) { video.setLargeImageUrl("http://www.largeImage.png"); } updateMetadata(video); break; case "CAT2" : if (video.getLargeImageUrl() == null) { video.setLargeImageUrl("http://www.smallImage.png"); } updateMetadata(video); break; …

Page 80: Develop Maintainable Apps

… warning: Cyclomatic Complexity is 9

Page 81: Develop Maintainable Apps

public void overlyComplexMethod(Video video) { if (video != null && video.getStreamUrl() != null) { updateVideoBasedOnCategory(video); } } !

private void updateVideoBasedOnCategory(Video video) { switch (video.getCategory()) { case "CAT1" : playVideo(video); if (video.getLargeImageUrl() == null) { video.setLargeImageUrl("http://www.largeImage.png"); } updateMetadata(video); break;

Page 82: Develop Maintainable Apps

public void overlyComplexMethod(Video video) { if (video != null && video.getStreamUrl() != null) { updateVideoBasedOnCategory(video); } } !

private void updateVideoBasedOnCategory(Video video) { switch (video.getCategory()) { case "CAT1" : playVideo(video); if (video.getLargeImageUrl() == null) { video.setLargeImageUrl("http://www.largeImage.png"); } updateMetadata(video); break;

Page 83: Develop Maintainable Apps

public void overlyComplexMethod(Video video) { if (video != null && video.getStreamUrl() != null) { updateVideoBasedOnCategory(video); } } !

private void updateVideoBasedOnCategory(Video video) { switch (video.getCategory()) { case "CAT1" : playVideo(video); if (video.getLargeImageUrl() == null) { video.setLargeImageUrl("http://www.largeImage.png"); } updateMetadata(video); break;

7

Page 84: Develop Maintainable Apps

!

switch (video.getCategory()) { case "CAT1" : playVideo(video); updateMetaDataAndUrl(video, "http://www.largeImage.png"); break; …

@brwngrldev

Page 85: Develop Maintainable Apps

!

switch (video.getCategory()) { case "CAT1" : playVideo(video); updateMetaDataAndUrl(video, "http://www.largeImage.png"); break; …

4

@brwngrldev

Page 86: Develop Maintainable Apps

Review…

• Readability Matters

• Use Checkstyle

• Refactor Gradually

@brwngrldev

Code Quality

3

Page 87: Develop Maintainable Apps

Unit Testing

Page 88: Develop Maintainable Apps

Untested Code

Page 89: Develop Maintainable Apps

Untested Code

Page 90: Develop Maintainable Apps

“Every single line of code that you write should be tested. Period.”

— Robert Martin

Page 91: Develop Maintainable Apps
Page 92: Develop Maintainable Apps

0

9,000

18,000

27,000

36,000

Lines of Code

Production Tests

FitNesse

Page 93: Develop Maintainable Apps

0

9,000

18,000

27,000

36,000

Lines of Code

Production Tests 90%FitNesse

Page 94: Develop Maintainable Apps

0

9,000

18,000

27,000

36,000

Lines of Code

Production Tests 90%

2,200

FitNesse

Page 95: Develop Maintainable Apps

0

9,000

18,000

27,000

36,000

Lines of Code

Production Tests 90%

2,200

90s

FitNesse

Page 96: Develop Maintainable Apps
Page 97: Develop Maintainable Apps
Page 98: Develop Maintainable Apps
Page 99: Develop Maintainable Apps

Example

Page 100: Develop Maintainable Apps

Test Setup

@brwngrldev

Page 101: Develop Maintainable Apps

Sample Test

@brwngrldev

Page 102: Develop Maintainable Apps

Test Report

Page 103: Develop Maintainable Apps

Continuous Integration

@brwngrldev

Page 104: Develop Maintainable Apps

Code Coverage

@brwngrldev

Page 105: Develop Maintainable Apps

Makes You Powerful

Page 106: Develop Maintainable Apps

Review

• Listen to Robert Martin!

• JUnit + Mockito

• Continuous Integration

• Code CoverageTesting

4

@brwngrldev

Page 107: Develop Maintainable Apps

Summary

Page 108: Develop Maintainable Apps

Summary

Choose the right library

Page 109: Develop Maintainable Apps

Summary

Choose the right library

Don’t do too much

Page 110: Develop Maintainable Apps

Summary

Ensure code quality

Choose the right library

Don’t do too much

Page 111: Develop Maintainable Apps

Summary

Ensure code quality

Choose the right library

Don’t do too much

Write unit tests

Page 112: Develop Maintainable Apps

Resources

• Clean Code - http://amzn.to/1DJybxH

• Effective Java - http://amzn.to/1Ku8Xel

• Working Effectively with Legacy Code - http://amzn.to/1Jqe1PA

• Unit Testing Idioms - http://goo.gl/Bx1WbL

• Google Code Style - http://goo.gl/8Pf6J3

• Architecting Android - http://goo.gl/UKvmbq

• Conquering Cyclomatic Complexity - http://goo.gl/lRoPXN

• PostTV App - https://goo.gl/0TRsXU

@brwngrldev

Page 113: Develop Maintainable Apps

Photo Credits• Slide 4 - https://www.flickr.com/photos/nikigunn/8379494330

• Slide 6 - https://www.flickr.com/photos/tracy_olson/61056391

• Slide 7 - https://en.wikipedia.org/wiki/Dominoes

• Slide 10 - https://www.flickr.com/photos/tshirbert/118250140

• Slide 24 - http://uncompromisedmen.com/2015/02/17/top-10-crazy-beards-wish-youd-seen-person/

• Slide 38 - https://www.flickr.com/photos/cast_fish/2888442781

• Slide 52 - https://www.flickr.com/photos/desertbusforhope/8207412726/

• Slide 54 - https://www.flickr.com/photos/sokabs/2668975758

• Slide 63 - https://www.flickr.com/photos/pasukaru76/5268559005

@brwngrldev