SlideShare a Scribd company logo
1 of 33
Download to read offline
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
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

Similar to Keep calm and write reusable code in Android

Innovation Generation - The Mobile Meetup: Android Best Practices
Innovation Generation - The Mobile Meetup: Android Best PracticesInnovation Generation - The Mobile Meetup: Android Best Practices
Innovation Generation - The Mobile Meetup: Android Best PracticesSolstice Mobile Argentina
 
Running Code in the Android Stack at ELCE 2013
Running Code in the Android Stack at ELCE 2013Running Code in the Android Stack at ELCE 2013
Running Code in the Android Stack at ELCE 2013Opersys inc.
 
Top 11 Front-End Web Development Tools To Consider in 2020
 Top 11 Front-End Web Development Tools To Consider in 2020 Top 11 Front-End Web Development Tools To Consider in 2020
Top 11 Front-End Web Development Tools To Consider in 2020Katy Slemon
 
Running Code in the Android Stack at ABS 2014
Running Code in the Android Stack at ABS 2014Running Code in the Android Stack at ABS 2014
Running Code in the Android Stack at ABS 2014Opersys inc.
 
Enhancing and modifying_the_core_android_os
Enhancing and modifying_the_core_android_osEnhancing and modifying_the_core_android_os
Enhancing and modifying_the_core_android_osArnav Gupta
 
Lunch and learn as3_frameworks
Lunch and learn as3_frameworksLunch and learn as3_frameworks
Lunch and learn as3_frameworksYuri Visser
 
Enhancing and modifying_the_core_android_os
Enhancing and modifying_the_core_android_osEnhancing and modifying_the_core_android_os
Enhancing and modifying_the_core_android_osArnav Gupta
 
Android Frameworks: Highlighting the Need for a Solid Development Framework 
Android Frameworks: Highlighting the Need for a Solid Development Framework Android Frameworks: Highlighting the Need for a Solid Development Framework 
Android Frameworks: Highlighting the Need for a Solid Development Framework Mutual Mobile
 
International Journal of Engineering Research and Development
International Journal of Engineering Research and DevelopmentInternational Journal of Engineering Research and Development
International Journal of Engineering Research and DevelopmentIJERD Editor
 
Deploy and Update Jakarta EE & MicroProfile applications with Paketo.pptx
Deploy and Update Jakarta EE & MicroProfile applications with Paketo.pptxDeploy and Update Jakarta EE & MicroProfile applications with Paketo.pptx
Deploy and Update Jakarta EE & MicroProfile applications with Paketo.pptxJamie Coleman
 
Android app development SEO Expert Bangladesh LTD.pdf
Android app development  SEO Expert Bangladesh LTD.pdfAndroid app development  SEO Expert Bangladesh LTD.pdf
Android app development SEO Expert Bangladesh LTD.pdfTasnim Jahan
 
Android app development SEO Expert Bangladesh LTD.pdf
Android app development  SEO Expert Bangladesh LTD.pdfAndroid app development  SEO Expert Bangladesh LTD.pdf
Android app development SEO Expert Bangladesh LTD.pdfTasnim Jahan
 
IRJET- Polymer Javascript
IRJET- Polymer JavascriptIRJET- Polymer Javascript
IRJET- Polymer JavascriptIRJET Journal
 
Embedded Android Workshop with Marshmallow
Embedded Android Workshop with MarshmallowEmbedded Android Workshop with Marshmallow
Embedded Android Workshop with MarshmallowOpersys inc.
 
Embedded Android Workshop with Lollipop
Embedded Android Workshop with LollipopEmbedded Android Workshop with Lollipop
Embedded Android Workshop with LollipopOpersys inc.
 
Developing cross platform apps in Flutter (Android, iOS, and Web)
Developing cross platform apps in Flutter (Android, iOS, and Web)Developing cross platform apps in Flutter (Android, iOS, and Web)
Developing cross platform apps in Flutter (Android, iOS, and Web)Priyanka Tyagi
 
Building Effective and Rapid Applications with IBM MobileFirst Platform
Building Effective and Rapid Applications with IBM MobileFirst PlatformBuilding Effective and Rapid Applications with IBM MobileFirst Platform
Building Effective and Rapid Applications with IBM MobileFirst PlatformAndrew Ferrier
 

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

Innovation Generation - The Mobile Meetup: Android Best Practices
Innovation Generation - The Mobile Meetup: Android Best PracticesInnovation Generation - The Mobile Meetup: Android Best Practices
Innovation Generation - The Mobile Meetup: Android Best Practices
 
Running Code in the Android Stack at ELCE 2013
Running Code in the Android Stack at ELCE 2013Running Code in the Android Stack at ELCE 2013
Running Code in the Android Stack at ELCE 2013
 
Top 11 Front-End Web Development Tools To Consider in 2020
 Top 11 Front-End Web Development Tools To Consider in 2020 Top 11 Front-End Web Development Tools To Consider in 2020
Top 11 Front-End Web Development Tools To Consider in 2020
 
Boquet manager
Boquet managerBoquet manager
Boquet manager
 
Running Code in the Android Stack at ABS 2014
Running Code in the Android Stack at ABS 2014Running Code in the Android Stack at ABS 2014
Running Code in the Android Stack at ABS 2014
 
Enhancing and modifying_the_core_android_os
Enhancing and modifying_the_core_android_osEnhancing and modifying_the_core_android_os
Enhancing and modifying_the_core_android_os
 
Lunch and learn as3_frameworks
Lunch and learn as3_frameworksLunch and learn as3_frameworks
Lunch and learn as3_frameworks
 
Enhancing and modifying_the_core_android_os
Enhancing and modifying_the_core_android_osEnhancing and modifying_the_core_android_os
Enhancing and modifying_the_core_android_os
 
Android Frameworks: Highlighting the Need for a Solid Development Framework 
Android Frameworks: Highlighting the Need for a Solid Development Framework Android Frameworks: Highlighting the Need for a Solid Development Framework 
Android Frameworks: Highlighting the Need for a Solid Development Framework 
 
International Journal of Engineering Research and Development
International Journal of Engineering Research and DevelopmentInternational Journal of Engineering Research and Development
International Journal of Engineering Research and Development
 
Software Engineering 2014
Software Engineering 2014Software Engineering 2014
Software Engineering 2014
 
Deploy and Update Jakarta EE & MicroProfile applications with Paketo.pptx
Deploy and Update Jakarta EE & MicroProfile applications with Paketo.pptxDeploy and Update Jakarta EE & MicroProfile applications with Paketo.pptx
Deploy and Update Jakarta EE & MicroProfile applications with Paketo.pptx
 
Android app development SEO Expert Bangladesh LTD.pdf
Android app development  SEO Expert Bangladesh LTD.pdfAndroid app development  SEO Expert Bangladesh LTD.pdf
Android app development SEO Expert Bangladesh LTD.pdf
 
Android app development SEO Expert Bangladesh LTD.pdf
Android app development  SEO Expert Bangladesh LTD.pdfAndroid app development  SEO Expert Bangladesh LTD.pdf
Android app development SEO Expert Bangladesh LTD.pdf
 
IRJET- Polymer Javascript
IRJET- Polymer JavascriptIRJET- Polymer Javascript
IRJET- Polymer Javascript
 
Embedded Android Workshop with Marshmallow
Embedded Android Workshop with MarshmallowEmbedded Android Workshop with Marshmallow
Embedded Android Workshop with Marshmallow
 
Embedded Android Workshop with Lollipop
Embedded Android Workshop with LollipopEmbedded Android Workshop with Lollipop
Embedded Android Workshop with Lollipop
 
Developing cross platform apps in Flutter (Android, iOS, and Web)
Developing cross platform apps in Flutter (Android, iOS, and Web)Developing cross platform apps in Flutter (Android, iOS, and Web)
Developing cross platform apps in Flutter (Android, iOS, and Web)
 
Angular 2
Angular 2Angular 2
Angular 2
 
Building Effective and Rapid Applications with IBM MobileFirst Platform
Building Effective and Rapid Applications with IBM MobileFirst PlatformBuilding Effective and Rapid Applications with IBM MobileFirst Platform
Building Effective and Rapid Applications with IBM MobileFirst Platform
 

More from Juan Camilo Sacanamboy (13)

Problema del barbero durmiente
Problema del barbero durmienteProblema del barbero durmiente
Problema del barbero durmiente
 
OFDM (Orthogonal Frequency Division Multiplexing )
OFDM (Orthogonal Frequency Division Multiplexing �)OFDM (Orthogonal Frequency Division Multiplexing �)
OFDM (Orthogonal Frequency Division Multiplexing )
 
Functional Testing
Functional TestingFunctional Testing
Functional Testing
 
FTP (File Transfer Protocol)
FTP (File Transfer Protocol)FTP (File Transfer Protocol)
FTP (File Transfer Protocol)
 
Protocolo de Enrutamiento RIP (Versiones 1 y 2)
Protocolo de Enrutamiento RIP (Versiones 1 y 2)Protocolo de Enrutamiento RIP (Versiones 1 y 2)
Protocolo de Enrutamiento RIP (Versiones 1 y 2)
 
Modelado de circuitos con ED de orden superior
Modelado de circuitos con ED de orden superiorModelado de circuitos con ED de orden superior
Modelado de circuitos con ED de orden superior
 
FDDI
FDDIFDDI
FDDI
 
Infrarrojo
InfrarrojoInfrarrojo
Infrarrojo
 
Ecuación de bessel
Ecuación de besselEcuación de bessel
Ecuación de bessel
 
Algoritmo JPEG
Algoritmo JPEGAlgoritmo JPEG
Algoritmo JPEG
 
Tutorial ASP .NET
Tutorial ASP .NETTutorial ASP .NET
Tutorial ASP .NET
 
Usando el entity framework
Usando el entity frameworkUsando el entity framework
Usando el entity framework
 
NRZ code
NRZ codeNRZ code
NRZ code
 

Recently uploaded

Android Application Components with Implementation & Examples
Android Application Components with Implementation & ExamplesAndroid Application Components with Implementation & Examples
Android Application Components with Implementation & ExamplesChandrakantDivate1
 
Mobile Application Development-Components and Layouts
Mobile Application Development-Components and LayoutsMobile Application Development-Components and Layouts
Mobile Application Development-Components and LayoutsChandrakantDivate1
 
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...nishasame66
 
Mobile Application Development-Android and It’s Tools
Mobile Application Development-Android and It’s ToolsMobile Application Development-Android and It’s Tools
Mobile Application Development-Android and It’s ToolsChandrakantDivate1
 
Leading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdfLeading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdfCWS Technology
 

Recently uploaded (6)

Android Application Components with Implementation & Examples
Android Application Components with Implementation & ExamplesAndroid Application Components with Implementation & Examples
Android Application Components with Implementation & Examples
 
Mobile Application Development-Components and Layouts
Mobile Application Development-Components and LayoutsMobile Application Development-Components and Layouts
Mobile Application Development-Components and Layouts
 
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...
 
Mobile Application Development-Android and It’s Tools
Mobile Application Development-Android and It’s ToolsMobile Application Development-Android and It’s Tools
Mobile Application Development-Android and It’s Tools
 
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
 
Leading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdfLeading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdf
 

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.
  • 11. Examples Reference: http://www.viva64.com/en/b/0260/ - Andrey Karpov Source Engine SDK Chromium
  • 12. Examples Reference: http://www.viva64.com/en/b/0260/ - Andrey Karpov ReactOS Multi Theft Auto
  • 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
  • 21.
  • 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)