Keep calm and write
reusable code in Android
Juan Camilo Sacanamboy
Co-founder and mobile developer at Peewah
http://peewah.co
Content
1. Code reusability
2. The most used technique
3. Code reusability in Android
Code reusability
History
● 1968: Douglas Mcllroy of Bell Laboratories proposed basing the software
industry on reusable components
● Write once and use it in many projects
● Not only reuse the code but also test suites, designs, documentation, and so
on.
● Improve the code duplication caused by copy & paste programming
Types of reuse
Concerning motivation and driving factors, reuse can be:
● Opportunistic
● Planned
Reuse can be categorized further:
● Internal reuse
● External reuse
Concerning form or structure of reuse, code can be:
● Referenced
● Forked
Examples
● Software libraries
● Design patterns
● Higher-order function
● Retrocomputing
● Components
● Outside of computers
Reusable software? Just don’t write generic code
● “It’s a common mistake to make software too generic”
● “Maximizing genericity complicates use”
● “Reusable software requires loose coupling and strong cohesion”
● Pitfalls
○ Abstraction layers everywhere
○ Excessive use of inheritance
Reference: http://josdejong.com/blog/2015/01/06/code-reuse/
2. The most used technique
Yes! It’s the copy & paste programming
The Last Line Effect
● 84 code fragments
○ 41 contain mistakes somewhere in the middle
○ 43 contain mistakes in the last copied code block
Reference: http://www.viva64.com/en/b/0260/ - Andrey Karpov
Figure 1. A rough diagram of mistake distribution in five
homogeneous code blocks.
The probability of making
a mistake in the last
pasted block of code is 4
times higher than in any
other block.
Examples
Reference: http://www.viva64.com/en/b/0260/ - Andrey Karpov
Source Engine SDK
Chromium
Examples
Reference: http://www.viva64.com/en/b/0260/ - Andrey Karpov
ReactOS
Multi Theft Auto
Examples
Reference: http://www.viva64.com/en/b/0260/ - Andrey Karpov
Source Engine SDK
Code reusability in Android
Activities
“An application component that provides
a screen with which users can interact
in order to do something…each activity
is given a window in which to draw its
user interface.”
Reference: http://developer.android.com/guide/components/activities.html
Fragments
Added in API level 11 (Android 3.0)
“A modular section of an activity,
which has its own lifecycle, receives its
own input events, and which you can
add or remove while the activity is
running”
Reference: http://developer.android.com/guide/components/activities.html
Java Archive (jar) vs Android Archive (aar)
● jar: Java classes
● aar: drawables, layouts, assets, styles, Android Manifest, libs, and so on
In Android Studio (Gradle build) you can use library in 3 ways
1. Library is a separate module in a project (multi-module project)
2. Library can be stored locally in the project (typically in libs folder)
3. Library can be deployed into a repository (Maven, Ivy Repository, JCenter)
Reference: http://stackoverflow.com/questions/29450709/how-to-create-reusable-activities-in-android
Multi-module project
● Multiple APKs with common library
with a multi-module project (some
modules are libraries, some
modules are applications.
● Problem: many apps in the same
project.
dependencies {
project(":library-module")
}
Reference: http://stackoverflow.com/questions/29450709/how-to-create-reusable-activities-in-android
Locally stored library
● Compile classes in a jar file
● Problem: updating local jar every
time you update the library
Reference: http://stackoverflow.com/questions/29450709/how-to-create-reusable-activities-in-android
dependencies {
compile files('libs/some_library.jar')
}
Deployed libraries
● The best way to use libraries in a
project
● You need a server (unless you
create an open source, in that case
you can use Maven Center or
jCenter)
● You can use a local repository, but
that’s difficult when you’re working
with other developers
dependencies {
compile 'com.google.code.gson:gson:2.2.4'
}
Reference: http://stackoverflow.com/questions/29450709/how-to-create-reusable-activities-in-android
Keep calm and write reusable code in Android
Peewah’s product:
Native apps for events
● Home
● Schedule (by day and topic)
● Twitter feed
● Gallery
● Photobooth
● Maps
● Phone numbers
Peewah’s architecture (native apps for events)
Characteristics of our architecture
● The library is an Android Module
● Each menu is a fragment and it’s decoupled from the others
● Each menu has parameters to configure it
● If there are dependencies between fragments we use a publisher/subscriber
mechanism (Otto)
● The library don’t depend of the technology used in the backend
The library is an Android Module
Each menu is a fragment and it’s decoupled
Each menu has parameters to configure it
Publisher/subscriber mechanism for dependencies
MainActivity
notifies two
type of events
to the
subscribers:
Events and
Milestones
The fragment (subscriber) receives the
information
The fragment subscribes to the Activity
(publisher)
Don’t depend of the technology used in the backend
Before our library
● Average development time: 2 months
● Many classes, activities, fragments, layouts (>60)
After our library
● Average development time: 1 week
● One class in the app (MainActivity)
Add menus in the Main
Activiy
After our library
Add Home Menu to the Main Activity with the Library (3 lines of code)
Questions
@csacanam - camilo@peewah.co

More Related Content

PPTX
Урок 1 для 10 класу - Техніка безпеки при роботі з комп'ютером і правила пове...
PPT
куля
PDF
практ заняття 21
PPTX
втілення сюжетів і образів священних книг людства у
PDF
UX for developers
PDF
Android: Reuse Models
PDF
Clean code on Android (Droidcon Dubai 2015)
Урок 1 для 10 класу - Техніка безпеки при роботі з комп'ютером і правила пове...
куля
практ заняття 21
втілення сюжетів і образів священних книг людства у
UX for developers
Android: Reuse Models
Clean code on Android (Droidcon Dubai 2015)

Similar to Keep calm and write reusable code in Android (20)

PDF
Android Development
PDF
Android development first steps
PDF
Innovation Generation - The Mobile Meetup: Android Best Practices
PDF
Android Bootcamp
PDF
597724227-mad-unit2.pdf-Mobile Application Development
PPT
PPT Companion to Android
PDF
Android Workshop Part 1
PDF
Where All Libraries & Data Required For Android App Development Are Present
PPTX
Android 101 Session @thejunction32
PPTX
Android apps development
PDF
Android development - the basics, MFF UK, 2012
PPT
Unit I- ANDROID OVERVIEW.ppt
PDF
Android app development SEO Expert Bangladesh LTD.pdf
PDF
Android app development SEO Expert Bangladesh LTD.pdf
PPTX
architecture of android.pptx
PDF
Marakana android-java developers
PDF
Running Code in the Android Stack at ABS 2014
PDF
Android Jump Start
PPTX
Mobile Application Development
PPT
Android Tutorial
Android Development
Android development first steps
Innovation Generation - The Mobile Meetup: Android Best Practices
Android Bootcamp
597724227-mad-unit2.pdf-Mobile Application Development
PPT Companion to Android
Android Workshop Part 1
Where All Libraries & Data Required For Android App Development Are Present
Android 101 Session @thejunction32
Android apps development
Android development - the basics, MFF UK, 2012
Unit I- ANDROID OVERVIEW.ppt
Android app development SEO Expert Bangladesh LTD.pdf
Android app development SEO Expert Bangladesh LTD.pdf
architecture of android.pptx
Marakana android-java developers
Running Code in the Android Stack at ABS 2014
Android Jump Start
Mobile Application Development
Android Tutorial
Ad

More from Juan Camilo Sacanamboy (13)

PDF
Problema del barbero durmiente
PDF
OFDM (Orthogonal Frequency Division Multiplexing )
PDF
Functional Testing
PDF
FTP (File Transfer Protocol)
PDF
Protocolo de Enrutamiento RIP (Versiones 1 y 2)
PDF
Modelado de circuitos con ED de orden superior
PDF
PDF
Ecuación de bessel
PDF
Algoritmo JPEG
PDF
Tutorial ASP .NET
PPTX
Usando el entity framework
Problema del barbero durmiente
OFDM (Orthogonal Frequency Division Multiplexing )
Functional Testing
FTP (File Transfer Protocol)
Protocolo de Enrutamiento RIP (Versiones 1 y 2)
Modelado de circuitos con ED de orden superior
Ecuación de bessel
Algoritmo JPEG
Tutorial ASP .NET
Usando el entity framework
Ad

Recently uploaded (10)

DOC
办Rice毕业证学历认证,哈金森社区学院毕业证留学本科毕业证
PDF
Facial Recognition System Singapore_ 1 SGD Per Month.pdf
PPTX
Key Components to Evaluate Before Building a Mobile App: Native vs Hybrid
PDF
Empowerment technologies presentation that helps student
PPTX
Digital Wallet Infographics: Template for Presentation
PDF
mao_guerrilla_warfare,mao zedongun gerilla savaşı üzerine yazısı
PPTX
The Future of Custom Mobile App Development.pptx
PPTX
BSNL Consumer Workshop ppt.pptx Telangana
DOC
哪里买SBU毕业证学历认证,卡罗尔大学毕业证留学学历
PPTX
mathematucicsSolving_Equationspptegypt.pptx
办Rice毕业证学历认证,哈金森社区学院毕业证留学本科毕业证
Facial Recognition System Singapore_ 1 SGD Per Month.pdf
Key Components to Evaluate Before Building a Mobile App: Native vs Hybrid
Empowerment technologies presentation that helps student
Digital Wallet Infographics: Template for Presentation
mao_guerrilla_warfare,mao zedongun gerilla savaşı üzerine yazısı
The Future of Custom Mobile App Development.pptx
BSNL Consumer Workshop ppt.pptx Telangana
哪里买SBU毕业证学历认证,卡罗尔大学毕业证留学学历
mathematucicsSolving_Equationspptegypt.pptx

Keep calm and write reusable code in Android

  • 1. Keep calm and write reusable code in Android Juan Camilo Sacanamboy Co-founder and mobile developer at Peewah http://peewah.co
  • 2. Content 1. Code reusability 2. The most used technique 3. Code reusability in Android
  • 4. History ● 1968: Douglas Mcllroy of Bell Laboratories proposed basing the software industry on reusable components ● Write once and use it in many projects ● Not only reuse the code but also test suites, designs, documentation, and so on. ● Improve the code duplication caused by copy & paste programming
  • 5. Types of reuse Concerning motivation and driving factors, reuse can be: ● Opportunistic ● Planned Reuse can be categorized further: ● Internal reuse ● External reuse Concerning form or structure of reuse, code can be: ● Referenced ● Forked
  • 6. Examples ● Software libraries ● Design patterns ● Higher-order function ● Retrocomputing ● Components ● Outside of computers
  • 7. Reusable software? Just don’t write generic code ● “It’s a common mistake to make software too generic” ● “Maximizing genericity complicates use” ● “Reusable software requires loose coupling and strong cohesion” ● Pitfalls ○ Abstraction layers everywhere ○ Excessive use of inheritance Reference: http://josdejong.com/blog/2015/01/06/code-reuse/
  • 8. 2. The most used technique
  • 9. Yes! It’s the copy & paste programming
  • 10. The Last Line Effect ● 84 code fragments ○ 41 contain mistakes somewhere in the middle ○ 43 contain mistakes in the last copied code block Reference: http://www.viva64.com/en/b/0260/ - Andrey Karpov Figure 1. A rough diagram of mistake distribution in five homogeneous code blocks. The probability of making a mistake in the last pasted block of code is 4 times higher than in any other block.
  • 15. Activities “An application component that provides a screen with which users can interact in order to do something…each activity is given a window in which to draw its user interface.” Reference: http://developer.android.com/guide/components/activities.html
  • 16. Fragments Added in API level 11 (Android 3.0) “A modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running” Reference: http://developer.android.com/guide/components/activities.html
  • 17. Java Archive (jar) vs Android Archive (aar) ● jar: Java classes ● aar: drawables, layouts, assets, styles, Android Manifest, libs, and so on In Android Studio (Gradle build) you can use library in 3 ways 1. Library is a separate module in a project (multi-module project) 2. Library can be stored locally in the project (typically in libs folder) 3. Library can be deployed into a repository (Maven, Ivy Repository, JCenter) Reference: http://stackoverflow.com/questions/29450709/how-to-create-reusable-activities-in-android
  • 18. Multi-module project ● Multiple APKs with common library with a multi-module project (some modules are libraries, some modules are applications. ● Problem: many apps in the same project. dependencies { project(":library-module") } Reference: http://stackoverflow.com/questions/29450709/how-to-create-reusable-activities-in-android
  • 19. Locally stored library ● Compile classes in a jar file ● Problem: updating local jar every time you update the library Reference: http://stackoverflow.com/questions/29450709/how-to-create-reusable-activities-in-android dependencies { compile files('libs/some_library.jar') }
  • 20. Deployed libraries ● The best way to use libraries in a project ● You need a server (unless you create an open source, in that case you can use Maven Center or jCenter) ● You can use a local repository, but that’s difficult when you’re working with other developers dependencies { compile 'com.google.code.gson:gson:2.2.4' } Reference: http://stackoverflow.com/questions/29450709/how-to-create-reusable-activities-in-android
  • 22. Peewah’s product: Native apps for events ● Home ● Schedule (by day and topic) ● Twitter feed ● Gallery ● Photobooth ● Maps ● Phone numbers
  • 24. Characteristics of our architecture ● The library is an Android Module ● Each menu is a fragment and it’s decoupled from the others ● Each menu has parameters to configure it ● If there are dependencies between fragments we use a publisher/subscriber mechanism (Otto) ● The library don’t depend of the technology used in the backend
  • 25. The library is an Android Module
  • 26. Each menu is a fragment and it’s decoupled
  • 27. Each menu has parameters to configure it
  • 28. Publisher/subscriber mechanism for dependencies MainActivity notifies two type of events to the subscribers: Events and Milestones The fragment (subscriber) receives the information The fragment subscribes to the Activity (publisher)
  • 29. Don’t depend of the technology used in the backend
  • 30. Before our library ● Average development time: 2 months ● Many classes, activities, fragments, layouts (>60)
  • 31. After our library ● Average development time: 1 week ● One class in the app (MainActivity) Add menus in the Main Activiy
  • 32. After our library Add Home Menu to the Main Activity with the Library (3 lines of code)