Top Banner
Iordanis Giannakakis @iordanis_g Device Fragmentation vs Clean Code
66

Device fragmentation vs clean code

Aug 19, 2014

Download

Engineering

 
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: Device fragmentation vs clean code

Iordanis Giannakakis

@iordanis_g

Device Fragmentation vs Clean Code

Page 2: Device fragmentation vs clean code

Introduction

• Iordanis “Jordan” Giannakakis

• Android Team Lead at Shazam

• @iordanis_g

Page 3: Device fragmentation vs clean code

How Shazam works

Page 4: Device fragmentation vs clean code

Running order

• What is fragmentation & clean code?

• Make the most of the Android platform

• DIY: Compatible cross-version features

• Handle complexity with MVP

• Write tests that cover all devices

• Q & A

Page 5: Device fragmentation vs clean code

Running order

• What is fragmentation & clean code?

• Make the most of the Android platform

• DIY: Compatible cross-version features

• Handle complexity with MVP

• Write tests that cover all devices

• Q & A

Page 6: Device fragmentation vs clean code

Device fragmentation

Page 7: Device fragmentation vs clean code

Distribution of OS versions (03/14)

• KitKat only 2x as big as Froyo (2010)

– 2.5% vs. 1.2%

• Gingerbread still 20% (2010)

Page 8: Device fragmentation vs clean code

Screen variations

Page 9: Device fragmentation vs clean code

Variety of capabilities

Page 10: Device fragmentation vs clean code

Variety of capabilities

Page 11: Device fragmentation vs clean code

Does diversification matter?

• Variety is the power of Android

• Google Play Services updates

• OS features as apps

• Compatibility libraries

Page 12: Device fragmentation vs clean code

Bad code

Page 13: Device fragmentation vs clean code

Bad code

• Big ‘If’ statements, heavily branched

• Unmaintainable

• Duplication

• Mixed concerns

• Untestable

Page 14: Device fragmentation vs clean code

Clean code

Page 15: Device fragmentation vs clean code

Clean code

• Easy to change

• Object model

• Single responsibilities

• Reusable

• Testable

Page 16: Device fragmentation vs clean code

Code quality: Is it a concern really?

• Short-term projects

• Individually developed

• Lean

• A/B testing

Page 17: Device fragmentation vs clean code

Code quality: Is it a concern really?

• Codebase gets bigger

• Need to grow team

• No tests

• Code becomes legacy

Page 18: Device fragmentation vs clean code

Code quality at Shazam

Page 19: Device fragmentation vs clean code

Running order

• What is fragmentation & clean code?

• Make the most of the Android platform

• DIY: Compatible cross-version features

• Handle complexity with MVP

• Write tests that cover all devices

• Q & A

Page 20: Device fragmentation vs clean code

Resource System

res/

Page 21: Device fragmentation vs clean code

Some resource types

• Layouts

• Drawables

• Values

– Attributes

– Colours

– Strings

– Styles

Page 22: Device fragmentation vs clean code

Some resource qualifiers

• Locale

• Screen

– Size

– Density

– Orientation

– Ratio

• Platform version

Page 23: Device fragmentation vs clean code

Customise UI

Page 24: Device fragmentation vs clean code

Switchable components

Page 25: Device fragmentation vs clean code

Switchable components

Page 26: Device fragmentation vs clean code

Code example: Resource System

github.com/iordanis/androidCleanCode

Page 27: Device fragmentation vs clean code

What we have gained

• Less code

• Separated presentation to logic

• Flexible UI

Page 28: Device fragmentation vs clean code

Running order

• What is fragmentation & clean code?

• Make the most of the Android platform

• DIY: Compatible cross-version features

• Handle complexity with MVP

• Write tests that cover all devices

• Q & A

Page 29: Device fragmentation vs clean code

API change

SharedPreferences.Editor

• commit() – slow, since v1

• apply() – fast, since v9

Page 30: Device fragmentation vs clean code

Compatibility libraries

• Backport features to old OS versions

• Version checking and graceful degradation

• Same package structure, different namespace

– android.app

– android.support.v2.app

• “Deprecated by design”

Page 31: Device fragmentation vs clean code

Compatibility libraries

• Official Libraries

– Support v4, v8, v13

– AppCompat v7

• 3rd party:

– ActionbarSherlock

– Nineoldandroids

Page 32: Device fragmentation vs clean code

Dependency Inject, Yourself (DIY)

• Break hardcoded dependencies

• What vs how

• Behaviour vs implementation

• Swappable dependencies for test and production runtimes

Page 33: Device fragmentation vs clean code

Without Dependency Injection

Client

Feature X Feature X

Page 34: Device fragmentation vs clean code

Dependency Injection

Client

Injector

Feature X Interface

Page 35: Device fragmentation vs clean code

Drive for DIY

• Fast

• Simple

• No magic necessary

• Pure Java!

• Run Android code on JVM for tests java.lang.RuntimeException: Stub!

Page 36: Device fragmentation vs clean code

Code example: SharedPreferences

Compatibility

github.com/iordanis/androidCleanCode

Page 37: Device fragmentation vs clean code

What we have gained

• Eliminated ‘ifs’ from production code

• Maintainability

• Testable code

• Reusable code

• More users

Page 38: Device fragmentation vs clean code

Running order

• What is fragmentation & clean code?

• Make the most of the Android platform

• DIY: Compatible cross-version features

• Handle complexity with MVP

• Write tests that cover all devices

• Q & A

Page 39: Device fragmentation vs clean code

Capabilities example – Google Play Services

Page 40: Device fragmentation vs clean code

Activity lifecycle

• Complex

• Few callbacks relevant

Page 41: Device fragmentation vs clean code

Issues with presentation complexity

• Compounds complexity of lifecycle

• Testing presentation logic difficult

• Unfulfilled dependencies when in JVM java.lang.RuntimeException: Stub!

• Robolectric is the only option

Page 42: Device fragmentation vs clean code

The MVP pattern

Presenter

Model View

Page 43: Device fragmentation vs clean code

The MVP pattern, with Android

Presenter

Model

View

start

stop

Android

Page 44: Device fragmentation vs clean code

Drive for MVP

• Make presentation logic testable

• Avoid Android dependencies

• No need to test the “slave” view

• Avoid Robolectric

Page 45: Device fragmentation vs clean code

Code example: MVP

github.com/iordanis/androidCleanCode

Page 46: Device fragmentation vs clean code

What we have gained

• Separation of concerns

• Testable presentation logic

• Reusable code

• Avoiding need for dependencies when running Android tests on JVM

Page 47: Device fragmentation vs clean code

Running order

• What is fragmentation & clean code?

• Make the most of the Android platform

• DIY: Compatible cross-version features

• Handle complexity with MVP

• Write tests that cover all devices

• Q & A

Page 48: Device fragmentation vs clean code

Test requirements

• Expressive

• Flexible

• Fast

• Helpful diagnostics

Page 49: Device fragmentation vs clean code

Testing Android apps

• Unit

• Integration

• System

Page 50: Device fragmentation vs clean code

System tests

• Instrumentation framework

• Robotium & Espresso

• gwen

Page 51: Device fragmentation vs clean code

Behaviour tests

• Given: arrange

• When: act

• Then: assert

Page 52: Device fragmentation vs clean code

Example

Given the server returns a track

When the user taps the Shazam button

Then the user sees the track result

Page 53: Device fragmentation vs clean code

gwen

given(server).returnsATrack()

when(user).tapsTheShazamButton()

then(user).seesTheTrackResult()

https://github.com/shazam/gwen

Page 54: Device fragmentation vs clean code

Phone behaviour

Page 55: Device fragmentation vs clean code

Tablet behaviour

Page 56: Device fragmentation vs clean code

Code example: System Tests

github.com/iordanis/androidCleanCode

Page 57: Device fragmentation vs clean code

Test speed

• All tests on all devices

– not always necessary

• Device clouds

• Gradle

• Maven

Page 58: Device fragmentation vs clean code

Spoon

http://square.github.io/spoon/

Page 59: Device fragmentation vs clean code

Fork

• Inspired by Spoon

• Infinitely scalable

– 1 test / 3secs

• Pooled execution

– Create pool for any device parameter

Page 60: Device fragmentation vs clean code

Fork

Page 61: Device fragmentation vs clean code

Diagnostics

Page 62: Device fragmentation vs clean code

Flexibility

@SkipOnDevice(

deviceType = TABLET_LARGE)

@SkipOnDevice(

playServices = NOT_AVAILABLE)

Page 63: Device fragmentation vs clean code

Summary

• Relationship of fragmentation & clean code

• How to make the most of Android

• How to write compatible code cleanly

• How to apply the MVP pattern

• How to write awesome automation tests

Page 64: Device fragmentation vs clean code

Notes

@iordanis_g

+IordanisGiannakakis

Code examples: github.com/iordanis/androidCleanCode

Hiring...

Page 65: Device fragmentation vs clean code

References

• Responsible Design For Android: https://leanpub.com/ResponsibleDesignAndroid-Part1

• 50 Android Hacks: http://www.manning.com/sessa/

• Google I/O 2012: Multi-Versioning Android User Interfaces: https://www.youtube.com/watch?v=amZM8oZBgfk

• Android Dashboards: https://developer.android.com/about/dashboards/index.html

• Android Fragmentation visualized: http://opensignal.com/reports/fragmentation-2013/

Page 66: Device fragmentation vs clean code

Questions?