Develop Maintainable Apps - edUiConf

69
Develop Maintainable Apps @brwngrldev

Transcript of Develop Maintainable Apps - edUiConf

Develop Maintainable Apps

@brwngrldev

What’s our job?

A. Make apps

B. Make apps that work

C. Make great apps!

@brwngrldev

What’s our job?

Make great apps!

@brwngrldev

The tools…

Libraries

1

Domain Design

2

Code Quality

3

Testing

4

@brwngrldev

Choosing the Right Library

So many options

What to consider

Amazing App

!

Documentation

Project Stability

Fulfills a Need

@brwngrldev

Example

So you need images…

Volley

!

Fresco

!

Glide

!

Picasso

!

Documentations

Volley

!

Documentations

W Project Stability

Volley

!

Documentations

W Project Stability

p Fulfills a need

Volley

!

@brwngrldev

Rinse & Repeat

Volley

!

Fresco

!

Glide

!

Picasso

!

Review…

• Documentation

• Repository Check-ins

• Fulfills a Need

Libraries

1

@brwngrldev

Domain Design

Doing too much

Seen this?

Clean It Up!

How to fix this

MVP

MVVM

OO

Foo

What’s involved…

Foo Bar

Baz Lol

Omg

Example

Remember this?

We need tests

Extract a Presenter

Create an Interface

Reference the Interface

public void onEvent (ErrorEvent event) {

mainView.showVideoUnavailableMessage(); }

Implement the Interface@Override public void showVideoUnavailableMessage () {

Toast.makeText( this, getString(R.string.video_unavailable), Toast.LENGTH_LONG ) .show();

}

Foo Bar

Baz Lol

Omg

@brwngrldev

Review…

• Keep Code Simple

• Use MV* Pattern

• Functional Tests

• Use the Tools

@brwngrldev

Domain Design

2

Code Quality

Poor Readability

Increase Readability?

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

Source

Files

config.xml

Checkstyle Tool

Modules

Design

Formatting

Code Complexity

apply plugin: ‘checkstyle’

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

<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));

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

Example

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"); }

… warning: Cyclomatic Complexity is 9

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

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

4

@brwngrldev

Review…

• Readability Matters

• Use Checkstyle

• Refactor Gradually

@brwngrldev

Code Quality

3

Unit Testing

Untested Code

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

— Robert Martin

0

9,000

18,000

27,000

36,000

Lines of Code

Production Tests 90%

2,200

90s

FitNesse

JUnit

• Framework for writing repeatable tests

• Assertions for testing expected results

• Features for sharing common test data

• Test suites for organizing tests

Mockito

• Enables mock creation and verification

• Mock concrete classes and interfaces

• Simple annotation support

Example

Test Setup@Mock MainView mainView;

@brwngrldev

Test Setup@Mock MainView mainView;

@Before public void setUp () {

}

@brwngrldev

Test Setup@Mock MainView mainView;

@Before public void setUp () {

initMocks( this );

mainPresenter = new MainPresenter(); mainPresenter.setMainView( mainView );

}

@brwngrldev

Sample Test

@brwngrldev

@Test public void configurationLoaded_shouldHideSplashScreen () {

ConfigurationLoadedEvent event = new ConfigurationLoadedEvent();

mainPresenter.onEvent( event );

}

Sample Test

@brwngrldev

@Test public void configurationLoaded_shouldHideSplashScreen () {

ConfigurationLoadedEvent event = new ConfigurationLoadedEvent();

mainPresenter.onEvent( event );

verify( mainView ).hideSplashScreen(); }

Test Report

@brwngrldev

Continuous Integration

@brwngrldev

Code Coverage

@brwngrldev

Makes You Powerful

Review

• Listen to Robert Martin!

• JUnit + Mockito

• Continuous Integration

• Code CoverageTesting

4

@brwngrldev

Summary

Ensure code quality

Choose the right library

Don’t do too much

Write unit tests

@brwngrldev

Thanks!

@brwngrldev +AnnyceDavis

www.adavis.info

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

@brwngrldev

Photo Credits• Slide 5 - https://www.flickr.com/photos/tshirbert/118250140

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

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

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

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

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

@brwngrldev