device fragmentation vs clean code

Post on 19-Aug-2014

262 Views

Category:

Engineering

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

Iordanis Giannakakis

@iordanis_g

Device Fragmentation vs Clean Code

Introduction

• Iordanis “Jordan” Giannakakis

• Android Team Lead at Shazam

• @iordanis_g

How Shazam works

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

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

Device fragmentation

Distribution of OS versions (03/14)

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

– 2.5% vs. 1.2%

• Gingerbread still 20% (2010)

Screen variations

Variety of capabilities

Variety of capabilities

Does diversification matter?

• Variety is the power of Android

• Google Play Services updates

• OS features as apps

• Compatibility libraries

Bad code

Bad code

• Big ‘If’ statements, heavily branched

• Unmaintainable

• Duplication

• Mixed concerns

• Untestable

Clean code

Clean code

• Easy to change

• Object model

• Single responsibilities

• Reusable

• Testable

Code quality: Is it a concern really?

• Short-term projects

• Individually developed

• Lean

• A/B testing

Code quality: Is it a concern really?

• Codebase gets bigger

• Need to grow team

• No tests

• Code becomes legacy

Code quality at Shazam

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

Resource System

res/

Some resource types

• Layouts

• Drawables

• Values

– Attributes

– Colours

– Strings

– Styles

Some resource qualifiers

• Locale

• Screen

– Size

– Density

– Orientation

– Ratio

• Platform version

Customise UI

Switchable components

Switchable components

Code example: Resource System

github.com/iordanis/androidCleanCode

What we have gained

• Less code

• Separated presentation to logic

• Flexible UI

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

API change

SharedPreferences.Editor

• commit() – slow, since v1

• apply() – fast, since v9

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”

Compatibility libraries

• Official Libraries

– Support v4, v8, v13

– AppCompat v7

• 3rd party:

– ActionbarSherlock

– Nineoldandroids

Dependency Inject, Yourself (DIY)

• Break hardcoded dependencies

• What vs how

• Behaviour vs implementation

• Swappable dependencies for test and production runtimes

Without Dependency Injection

Client

Feature X Feature X

Dependency Injection

Client

Injector

Feature X Interface

Drive for DIY

• Fast

• Simple

• No magic necessary

• Pure Java!

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

Code example: SharedPreferences

Compatibility

github.com/iordanis/androidCleanCode

What we have gained

• Eliminated ‘ifs’ from production code

• Maintainability

• Testable code

• Reusable code

• More users

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

Capabilities example – Google Play Services

Activity lifecycle

• Complex

• Few callbacks relevant

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

The MVP pattern

Presenter

Model View

The MVP pattern, with Android

Presenter

Model

View

start

stop

Android

Drive for MVP

• Make presentation logic testable

• Avoid Android dependencies

• No need to test the “slave” view

• Avoid Robolectric

Code example: MVP

github.com/iordanis/androidCleanCode

What we have gained

• Separation of concerns

• Testable presentation logic

• Reusable code

• Avoiding need for dependencies when running Android tests on JVM

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

Test requirements

• Expressive

• Flexible

• Fast

• Helpful diagnostics

Testing Android apps

• Unit

• Integration

• System

System tests

• Instrumentation framework

• Robotium & Espresso

• gwen

Behaviour tests

• Given: arrange

• When: act

• Then: assert

Example

Given the server returns a track

When the user taps the Shazam button

Then the user sees the track result

gwen

given(server).returnsATrack()

when(user).tapsTheShazamButton()

then(user).seesTheTrackResult()

https://github.com/shazam/gwen

Phone behaviour

Tablet behaviour

Code example: System Tests

github.com/iordanis/androidCleanCode

Test speed

• All tests on all devices

– not always necessary

• Device clouds

• Gradle

• Maven

Spoon

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

Fork

• Inspired by Spoon

• Infinitely scalable

– 1 test / 3secs

• Pooled execution

– Create pool for any device parameter

Fork

Diagnostics

Flexibility

@SkipOnDevice(

deviceType = TABLET_LARGE)

@SkipOnDevice(

playServices = NOT_AVAILABLE)

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

Notes

@iordanis_g

+IordanisGiannakakis

Code examples: github.com/iordanis/androidCleanCode

Hiring...

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/

Questions?

top related