building large sustainable apps

37
Building Large Sustainable Apps Buğra Oral © 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential. 1

Upload: bugra-oral

Post on 28-Jul-2015

10 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Building Large Sustainable Apps

Building Large Sustainable Apps

Buğra Oral

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

1

Page 2: Building Large Sustainable Apps

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

2

AGENDA

• Problem

• Project Culture

• Structuring a Sustainable Project

• UI Structuring Dilemmas

• GUI Software Architectures

• State of Art

Page 3: Building Large Sustainable Apps

3

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Problem

Page 4: Building Large Sustainable Apps

4

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Problem

Page 5: Building Large Sustainable Apps

5

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Problem

Page 6: Building Large Sustainable Apps

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

6

Within onCreate

ProblemScaled

Within onCreate Scaled

Page 7: Building Large Sustainable Apps

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

7

Activity as ClickListener

ProblemScaled

Activity as ClickListener Scaled

Page 8: Building Large Sustainable Apps

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

8

ButterKnife Example

ProblemScaled

ButterKnife Scaled

Page 9: Building Large Sustainable Apps

9

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

ProblemMixed

Page 10: Building Large Sustainable Apps

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

10

Problems

• View id’s

• Refactoring

• Confusion

• Inconsistency

• Hand over rate

Page 11: Building Large Sustainable Apps

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

11

Project Culture

• Conventions

• External Dependencies

• Resources

• Names

• Migrations

Page 12: Building Large Sustainable Apps

12

Project CultureConventions

• Main tool of communication is the code.

• Code blocks

• Field / Method / Class names

• Tip : Feature Component Based Names i.e: FlippingContactActivity / FlippingContactFragment

• Tip: Android Support Annotations, ex: View.VISIBLE,GONE

• Don’t ignore exceptions and handle errors gracefully

• Don’t use finalizers, no guarantees.

• Don’t duplicate code

• Have sense of maximum line and method length

• Favor equals methods over static objects

• Avoid nested blocks

• Use checkstyle tools to minimize alienation margin

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Page 13: Building Large Sustainable Apps

13

External Dependencies

• What could be an external dependency?

• Network library – Retrofit, Volley, Picasso, Wasp• Parceable Generators - Parceabler• Factory Method generators - IcePick• View libraries • ORM’s – Greendao• Check out android-arsenal.com

• Select your tools and debate so that there will be no place for two dependency doing similar stuff

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Page 14: Building Large Sustainable Apps

14

RESOURCES

What are the resources?

• Color

• Drawable

• Arrays

• Strings

• Dimensions

• Styles

• Themes

• Layout

• Menu

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Page 15: Building Large Sustainable Apps

15

RESOURCESNaming Conventions

Having a resource convention is very important. Resources can will expand drastically depending on how you structure them. It’s general conception to rely on prefixes.

• Colors: it is better to have the names set by the designer, and reuse them as logically as possible. Material guidelines will also encourage you to use them this way, i.e; colorPrimary, colorAccent. For colors with alphas, it is encouraged to use a $colorname_alpha_percent notation.

• Drawables: same as colors, debate with your designer. It’s helpful to come up with a pattern that uses prefixes as ic_ logo_ bg_ and so on.

• Layouts: Layout naming are very important, because they also effect your way of coding and increase clarity.

• activity_$activity_java_name• fragment_$fragment_java_name• list_item_$what_this_layout_renders• pager_item_$what_this_layout_renders• layout_$what_this_layout_renders

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Page 16: Building Large Sustainable Apps

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

16

• Layout View Id’s. • Duplicate layout id’s are the birth marks of a

chaotic project.• Refactor renaming an id will change every

single layout file as well as Java class.• Id’s are too generic, we don’t know that it does.• From Java file we can’t navigate to actual view

without further investigation.• What happens if there are two views with same

id’s in a view group? What will findViewbyId return?

• Suggestion is to use a id pattern, such as $(layoutfilename)_$viewtype_$whatitdoes

• Also now I can see all the views under some xml file just by writing R.id.$(layout_name)_ yaay!

Same goes for String resources.

RESOURCESNaming Conventions

Back to click listener example

Page 17: Building Large Sustainable Apps

17

RESOURCESStructuring

Android Themes and Styles has strong parenting relationships. Utilize them to diminish line of code and increase their usage. It’s very important not to break a style/theme structuring pattern.

• For Themes, always have base theme that does not vary from configuration to configuration.

• For styles, same, maintain a base themes; for all base views, TextView, EditText, Button and so on.

• Don’t break parenting for different activity themes and view styles.

• How to know you did a correct style/theme structure? If you need very little adding and modification while adding new layouts, that’s when you can assume it works.

When all are done accordance with a scalable logic, which is all your files under /res follow a certain rule, there won’t be much a need to create, duplicate and defect.

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Page 18: Building Large Sustainable Apps

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

18

RESOURCESStructuring

Page 19: Building Large Sustainable Apps

19

Application Structure

• Abstractions

• What should I abstract?

• Almost every Android Component. Application, Activity, Fragment, TextView, Button and so one.• Why? You will or may need application global behavior the first day of development or 6 months later.• But, be aware of the overhead! Every abstraction brings 500 byte of overhead to java classes.• Force your view to use your styles by default in constructer.

• Package structure;

• From experience, in most cases feature based approach works best for Android.• We can use public/private/package private modifier as they are intended.• For large apps, containing multiple features, it is such more easier to navigate and locate classes.• A developer working on a specific feature does not need to go beyond his scope.• Keep java packages and their resources aligned i.e;• Different string resource files for features• Different style/theme parents for feature sets.

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Page 20: Building Large Sustainable Apps

20

Architectural Design

• What are the most common Android Architectural designs?

• What are Activities and Fragments from a architectural point of view?

• Some will say they are class that you do everything in them.• When do I use an Fragment instead of Activity?• Some will say that they are Presenters from MVP pattern.• If they are Presenters, what are their responsibilities?• Is a Fragment simply view which should render given data or a something that has some deciding,

loading, changing data capabilities?• Or are fragments mini activities capable of maintaining their jobs themselves? If so what do I do with

child fragments? • Wait child fragments? If a fragment a child, does it have to report to its parent fragment or the

Activity?• Hey what do I do if I need to use a picker? A whole activity, fragment? How do I return the selected

values?

So the fun and chaos begins…

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Page 21: Building Large Sustainable Apps

21

Architectural Design Organized Utilization of Activity and Fragments

• In a Application consisting of a bunch of features, a better way to organize is to think your individual Activities as features.

• They will most generally do data operations, display fragments, open other activities. So they will become a descriptor of that feature, containing no UI related elements at all.

• All UI stuff should be in Fragments, so that they can be used both in tablets and phones. By replacing your Fragment with a new one with better design, we begin to maintain better by plunging out and in a new peace of Lego.

• To arrange Activity and Fragment communication; general conception is to use activities as callback interfaces, but to further decouple components, we encourage the use of Event Buses.

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Page 22: Building Large Sustainable Apps

22

Event BusSide Explanation

• Event bus is a way of communication between objects. An object subscripts to the bus for certain events.

When the certain event is fired, all that are registered will be notified.

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Page 23: Building Large Sustainable Apps

23

Architectural Design Tip

• It is highly encouraged to escape android components and move to plain old java objects. Clean all codes you can and move to an external java module so that you can run simple unit tests and export your business logic.

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Page 24: Building Large Sustainable Apps

24

Architectural Design Exploring Design Patterns

• What is the general problem we need to attack at? %99 Android projects are

• Hard to test code• Hard to read code• Hard to refactor code• Hard to reuse code• Hard to handle-config changes code• Hard to replicate edge-cases code• Hard to hand-over code

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Page 25: Building Large Sustainable Apps

25

Architectural Design Exploring Design Patterns

• Why is it this then?

• ~99% of the Android examples are written this way• It mostly works• Framework classes and libraries work this way.• People have got used to hearing that Android works this way and accepted it.

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Page 26: Building Large Sustainable Apps

26

Architectural Design MVC

MVC (Model-View-Controller) : The term MVC is in my opinion highly subjective and highly abstract. The pain point seems to be around the definition of Controller. There are genuinely two different interpretations of MVC.

• (less-often) An over-arching term, which is a superset of all MV* architectures that separate some View from some Model and other middle-ish layer than may or may not communicated with the View & Model components directly.

• (more-often) A slightly more concrete notion of a pattern that relates the M V and C components in a triangular relationship which does not allow the user to interact with the View directly but only via a View and / or Controller. View displays the Model and Model is manipulated via the Controller.

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Page 27: Building Large Sustainable Apps

27

Architectural Design MVP

MVP (Model-View-Presenter) : MVP is still to general to understand. Main thing is there is linear relationship between M V and P components.

• Generally Model is accepted as the source of data and View is acknowledge as a rendering component.

• Responsibilities of Presenter on the other hand differ from interpretation to interpretation.

• one hand there is the case where all view logic is left in the view and the presenter doesn’t get involved in deciding how to render the model.

• you can also move all the way to having the presenter do all the manipulation of the widgets

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Page 28: Building Large Sustainable Apps

28

Architectural Design MVVM

MVVM (Model-View-ViewModel) : This is a popular one in the Microsoft (& Xamarin) world. The thing that all MVVM architectures have in common is…data-binding!

In a nutshell the ViewModel can define the binding between Views and data properties and if bound two ways then changes to the view update the model and changes to the model update the view, pretty much for free.

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Page 29: Building Large Sustainable Apps

29

Architectural Design MVA

MVA (Model-View-Adapter) : This one is interesting as its pretty simple. This is like a linear MVC (the same as MVP above), meaning that there is no communication between the View and the Model.

From what I can deduct, this seems to be very similar to MVP, except for the fact that Adapter can not store view state, while the presenter could.

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Page 30: Building Large Sustainable Apps

30

State of ArtDesign Patterns

MVP PASSIVE VIEW

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Page 31: Building Large Sustainable Apps

31

State of ArtMVP Passive View Implementation

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Page 32: Building Large Sustainable Apps

32

State of ArtMVP Passive View Implementation

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Page 33: Building Large Sustainable Apps

33

State of ArtMVP Passive View Implementation

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Page 34: Building Large Sustainable Apps

34

State of ArtMVP Passive View Implementation

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Page 35: Building Large Sustainable Apps

35

State of ArtMVP Passive View Implementation

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Page 36: Building Large Sustainable Apps

36

ConclusionWhat did we gain?

• We have project that maintains a culture

• We have logically structured resources

• We can have objects independent from lifecycle methods

• All presenters can be exported and used with other layouts

• All view’s behavior can be changed by its presenter

• Modularity boost by moving presenters and view interfaces to a library.

• Presenters are JUnit testable

• View objects can be mocked via classes that implements the view interfaces

• Cool stuff like RxJava and Dagger could be leveraged in the presenters, in the business logic where their benefit boosts up.

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

Page 37: Building Large Sustainable Apps

Q&A

BUĞRA ORAL

© 2003–2014 Monitise Group Limited. All Rights Reserved. Confidential.

37