1. Clean Code on Android!
DroidCon Dubai
Danny Preussler
Groupon
The
Living
Room,
Colin
and
Sarah
Northway,
CC
by
2.0
h<ps://www.flickr.com/photos/apes_abroad/1479254938
2. Groupon is the global
leader in local commerce,
making it easy for people around the world to search !
and discover great businesses at unbeatable prices.
Source: Internal data,, March 2014
WORLDWIDE!
260M+ subscribers 53.9M active customers 500+ markets 900M+ deals sold
2
$5B+ in annual billings 12,000+ global employees
3. Leading the way in mobile commerce
Our mobile app is available in 43 countries.
Groupon’s vibrant mobile marketplace connects!
consumers with their local economy
3
Sources: Internal Data; iTunes ranking for US stores available here - https://itunes.apple.com/WebObjects/
MZStore.woa/wa/viewFeature?id=500873243&mt=8&v0=www-itunes25Bcountdown-appstore
Nearly 110 million people worldwide have
downloaded our mobile app at the end of Q4 2014.
One of the 25 most downloaded free
apps of all time
More than 50% of our Global transactions were
completed on a mobile device by the end of Q4
2014
7. Separation of concerns (1974)!
• Modularize, Encapsulate, Layer!
Example: Don‘t do HTTP request in Activities/Fragments!
flickr.com/photos/ionics/6337522871,
Black
Sheep
Meets
White
Sheep
by
Leon
Riskin,
CC
by
2.0
9. ViolaTons
of
DRY
are
typically
referred
to
as
WET
soluTons,
which
is
commonly
taken
to
stand
for
either
"write
everything
twice"
or
"we
enjoy
typing”
(h<p://en.wikipedia.org/wiki/Don't_repeat_yourself)
Android Firewall by Uncalno, CC BY 2.0
flickr.com/photos/uncalno/8538679708
10. Clean Code!!
The
Living
Room,
Colin
and
Sarah
Northway,
CC
by
2.0
h<ps://www.flickr.com/photos/apes_abroad/1479254938
Clean
Code
by
Robert
C.
Mar0n
11. Robert C. Martin – Clean Code!
The
Living
Room,
Colin
and
Sarah
Northway,
CC
by
2.0
h<ps://www.flickr.com/photos/apes_abroad/1479254938
Uncle
Bob
says:
(Keep
it)
small!
12. Robert C. Martin – Clean Code!
The
Living
Room,
Colin
and
Sarah
Northway,
CC
by
2.0
h<ps://www.flickr.com/photos/apes_abroad/1479254938
Uncle
Bob
says:
Do
One
Thing!
13. Robert C. Martin – Clean Code!
The
Living
Room,
Colin
and
Sarah
Northway,
CC
by
2.0
h<ps://www.flickr.com/photos/apes_abroad/1479254938
Uncle
Bob
says:
One
Level
of
Abstrac0on
per
Func0on!
14. Robert C. Martin – Clean Code!
The
Living
Room,
Colin
and
Sarah
Northway,
CC
by
2.0
h<ps://www.flickr.com/photos/apes_abroad/1479254938
Uncle
Bob
says:
Reading
Code
from
Top
to
BoHom
15. Robert C. Martin – Clean Code!
The
Living
Room,
Colin
and
Sarah
Northway,
CC
by
2.0
h<ps://www.flickr.com/photos/apes_abroad/1479254938
Uncle
Bob
says:
Comments
Do
Not
Make
Up
for
Bad
Code
16. Robert C. Martin – Clean Code!
The
Living
Room,
Colin
and
Sarah
Northway,
CC
by
2.0
h<ps://www.flickr.com/photos/apes_abroad/1479254938
Uncle
Bob
says:
Avoid
switch
It’s
[..]
hard
to
make
a
switch
statement
that
does
one
thing.
By
their
nature,
switch
statements
always
do
N
things.
17. Much more...!
The
Living
Room,
Colin
and
Sarah
Northway,
CC
by
2.0
h<ps://www.flickr.com/photos/apes_abroad/1479254938
A
must
Read!
19. Si_ng
in
a
Tree,
Big
Bend
NaTonal
Park
by
Adam
Baker,
CC
by
2.0,
flickr.com/photos/atbaker/5474766579
Watch out for !
Primitive
Obsession!
20. Ban null !
from !
your code!!
nothing
is
nothing
by
darwin
Bell,
CC
by
2.0,
flickr.com/photos/darwinbell/272818496
21. private MyParcelable myParceable;
...
Bundle bundle = getArguments();
if (bundle != null) {
myParceable = bundle.getParcelable("ARGUMENT");
}
....
if (myParcaeable != null) {
...
}
...
if (myParcaeable != null) {
...
}
Don‘t:!
Red
Stop
Bu<on
by
Ben
K
Adams,
CC
by
2.0,
flickr.com/photos/schtumple/5475697999
22. Cleaner
by
atomicjeep,
CC
by
2.0,
flickr.com/photos/atomicjeep/144719649
Do: Inject Dependencies!
private MyParcelable myParceable = MyParceable.NONE;
...
Bundle bundle = getArguments();
if (bundle != null &&
bundle.hasParceable(„ARGUMENT“) {
myParceable = bundle.getParcelable("ARGUMENT");
}
....
if (myParcaeable != null) {
...
}
...
if (myParcaeable != null) {
...
}
23. The power of
Dependency
Injection!
• Don‘t
create
dependencies
• Invert
Control!
• Inject
them!
I
love
the
Light
&
I
dig
the
Sun
by
fady
habib,
CC
by
2.0,
flickr.com/photos/unTtlism/2609684221
24. public class WetFragment extends Fragment {
MyController controller;
...
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
controller =
new MyController(activity.getApplicationContext());
Don‘t:!
Red
Stop
Bu<on
by
Ben
K
Adams,
CC
by
2.0,
flickr.com/photos/schtumple/5475697999
25. Cleaner
by
atomicjeep,
CC
by
2.0,
flickr.com/photos/atomicjeep/144719649
public class DryFragment extends Fragment {
@Inject MyController controller;
...
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
controller =
new MyController(activity.getApplicationContext());
Do: Inject Dependencies!
26. public class WetFragment extends Fragment {
EditText myEditField;
@Override
public View onCreateView(LayoutInflater inflater, ...) {
View view = inflater.inflate(......
myEditField = (EditText)view.findViewById(R.id.edit1);
...
Don‘t:!
Red
Stop
Bu<on
by
Ben
K
Adams,
CC
by
2.0,
flickr.com/photos/schtumple/5475697999
27. Cleaner
by
atomicjeep,
CC
by
2.0,
flickr.com/photos/atomicjeep/144719649
public class DryFragment extends Fragment {
@Inject(R.id.edit1) EditText myEditField;
@Override
public View onCreateView(LayoutInflater inflater, ...) {
View view = inflater.inflate(......
myEditField = (EditText)view.findViewById(R.id.edit1);
...
Do: Inject views!
28. public class WetFragment extends Fragment {
private MyParcelable myParceable;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Bundle bundle = getArguments();
if (bundle != null) {
myParceable = bundle.getParcelable("ARGUMENT");
if (myParceable == null) {
myParceable = MyParcelable.NONE;
}
}
}
Don‘t:!
Red
Stop
Bu<on
by
Ben
K
Adams,
CC
by
2.0,
flickr.com/photos/schtumple/5475697999
29. Cleaner
by
atomicjeep,
CC
by
2.0,
flickr.com/photos/atomicjeep/144719649
public class DryFragment extends Fragment {
@Inject(„ARGUMENT“) MyParcelable = myParceable.NONE;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Bundle bundle = getArguments();
if (bundle != null) {
myParceable = bundle.getParcelable("ARGUMENT");
if (myParceable == null) {
myParceable = MyParcelable.NONE;
}
}
}
Do: Inject arguments / extras!
30. public class WetFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ...) {
return inflater.inflate(
R.layout.main_layout, container, false);
}
Don‘t:!
Red
Stop
Bu<on
by
Ben
K
Adams,
CC
by
2.0,
flickr.com/photos/schtumple/5475697999
31. Cleaner
by
atomicjeep,
CC
by
2.0,
flickr.com/photos/atomicjeep/144719649
@InjectLayout(R.layout.main_layout)
public class DryFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ...) {
return inflater.inflate(
R.layout.main_layout, container, false);
}
Do: Inject layout!
32. Cleaner
by
atomicjeep,
CC
by
2.0,
flickr.com/photos/atomicjeep/144719649
@InjectLayout(R.layout.main_layout)
public class DryFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ...) {
return inflater.inflate(
R.layout.main_layout, container, false);
}
Do: auto (find) layout!
Simply have dry_fragment.xml or dry.xml
35. Complicated,
Rohit
Ma<oo,
CC
by
2.0,
flickr.com/photos/mar00ned/229903286
@Override
public void onResume() {
super.onResume();
getFragmentManager()
.beginTransaction()
.add(R.id.fragments, new WetFragment())
. commitAllowingStateLoss();
}
Dont: write untestable code:!
36. test
(c)
DaveBleasdale,
CC
by
2.0,
www.flickr.com/photos/sidelong/246816211
Do: write testable code:!
@Inject MyTransactionUtil transactions;
@Override
public void onResume() {
super.onResume();
transactions.addAllowingStateLoss(
R.id.fragments, new WetFragment());
}
37. • Split your Activities
• Alternative Mortar github.com/square/mortar
Do: use the Power of Fragments!
the
icon
(CC)
by
MarTn
Fisch,
CC
by
2.0,
flickr.com/photos/marfis75/7164769781