Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Android antipatterns


Published on

Inspirations for this presentation were drawn from a couple of sprints in one of our internal projects in which we had the freedom of choosing our own technical solutions.
We go through premature optimisation, silver-bullet antipattern, duplication and null-pointer hell.
In the end other antipatterns are swiftly mentioned.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Android antipatterns

  1. 1. Android antipatterns mobile guild #cododajnia Bartosz Kosarzycki @bkosarzycki
  2. 2. Anti-pattern a common response to a recurring problem that is usually ineffective [and] usually counter-productive.
  3. 3. Premature optimisation
  4. 4. Donald Knuth Author of “The Art of Computer Programming” Sir Tony Hoare Quick Sort, Null-reference, Hoare logic
  5. 5. Donald Knuth: For those who don't work to strict memory or CPU cycle limits, PrematureOptimization is an AntiPattern, since there is only cost and no benefit. For those who do, it is often confused with poor coding, or with misguided attempts at writing optimal code. Quotes original author is still questioned. “We should forget about small efficiencies, say about 97% of the time”
  7. 7. WHY?
  8. 8. Enums have obvious benefits: - make the code more self-documenting - the compiler can assign default values to enumerator automatically - prevents the programmer from writing illogical code - e.g. multiplying enums? - IDEs, type checking, correctness etc.
  9. 9. Change it to integers? public class View { public static final int VISIBLE = 0x00000000; public static final int INVISIBLE = 0x00000004; public static final int GONE = 0x00000008; }
  11. 11. Which leaves us with... public class View { public static final int VISIBLE = 0x00000000; public static final int INVISIBLE = 0x00000004; public static final int GONE = 0x00000008; @IntDef({VISIBLE, INVISIBLE, GONE}) @Retention(RetentionPolicy.SOURCE) public @interface Visibility {} }
  13. 13. -keep public enum android-how-to-keep-enum-from-proguard [link] proguard-wont-keep-a-class-members-enums [link] Proguard config #keep enum classes -keep public enum com.intive.nearbyplaces.model.ApiProviderType** { **[] $VALUES; public *; } You can also keep the enums...
  14. 14. Silver-bullet
  16. 16. If you don’t need it - don’t use it! Library Method count Butterknife 119 Retrofit2 402 Realm 2343 9651 Glide 2364 Kotlin 5090 RxJava + RxBinding + RxPref + RxLoc + RxTuples 5319 + 758 + 90 + 176 + 137 = 6480 Hawk 121 RxJava is heavy on android *without proguard
  17. 17. Use reactive: - Constant flow of data - “Instant” search cases Debounce(), throttleFirst() - Reflecting settings in UI ? It’s NOT only RxJava Maybe Android bindings are enough?
  18. 18. Don’t use reactive: - button clicks @Click is probably more concise - Simple “lookups” from db Category category = Category.forId(263); - Large, expensive, cached objects
  19. 19. Duplication
  20. 20. RxJava Duplication of reactive approaches EventBus
  21. 21. EventBus Don’t use a separate library - both approaches are reactive!
  22. 22. /** * Rx-style Event bus. Each event type implements its own subscription methods. */ public class RxEventBus { private static RxEventBus instance; private PublishSubject<Location> mGpsLocationEvents = PublishSubject.create(); public static RxEventBus instanceOf() { if (instance == null) { instance = new RxEventBus(); } return instance; } /** * Pass events of LOCATION type down to event listeners. */ public void newLocationEvent(Location location) { mGpsLocationEvents.onNext(location); } /** * Subscribe to this LOCATION type Observable. On event Action will be called. */ public Observable<Location> getLocationEvents() { return mGpsLocationEvents; } }
  23. 23. Nullpointer hell
  24. 24. @NonNull Java is not null-safe
  25. 25. @NonNull How can we overcome this?
  26. 26. @NonNull - We could use a higher order language like Kotlin/Scala/Dart - Optional<Type> - Annotations @NonNull @Nullable
  27. 27. @NonNull funtion add(@NonNull Square s1, @NonNull Square s2) { }
  28. 28. @NonNull funtion add(@NonNull Square s1, @NonNull Square s2) { } TEDIOUS!
  29. 29. @NonNull Let’s assume parameters and return values are NonNull by default
  30. 30. @NonNull file: @ParametersAreNonnullByDefault @ReturnValuesAreNonnullByDefault package com.intive.nearbyplaces; Gradle tasks for generating these files: download, download
  31. 31. Antipatterns summary - Premature optimization - Silver bullet - Duplication - Nullpointer hell
  32. 32. Other Antipatterns Superstitious coding Coding to handle error conditions which are already known to be impossible. Reinventing the square wheel/ Not Invented Here (NIH) Failing to adopt an existing solution and instead adopting a custom solution which performs much worse than the existing one Tester Driven Development Removing the determination of requirements and letting the testers (or the QA team) drive what they think the software should be through the testing (or QA) process. Lava flow Retaining undesirable (redundant or low-quality) code because removing it is too expensive
  33. 33. Links - Premature optimization is the root of all evil - Donald Knuth link - The antipattern catalogue Donald Knuth - link - The price of enums Android Developers - link - AntiPatterns: Refactoring Software, Architectures, and Projects in Crisis link - When not to use reactive - link - NonNull gradle task generator - link, link
  34. 34. Thank you! QUESTIONS Bartosz Kosarzycki @bkosarzycki