$30 off During Our Annual Pro Sale. View Details »

Hidden mysteries behind big mobile codebases RELOADED

Hidden mysteries behind big mobile codebasesย RELOADED

You have a really cool and impactful project, but as soon as your codebase gets bigger, and more and more contributors come into play, things can become challenging in regards to aspects like: code consistency, technical debt, refactoring, application architecture and team organization. Let's jump onboard on this journey and walk through different techniques that can help us keep our code sane and healthy for better scalability.

Disclaimer: This talk is going to be focused from a mobile standpoint but most of the practices included can also be applied to any software project under development.

MEMORIES OR TO FOLLOW UP AFTER THE TALK:

**S.O.L.I.D Principles:**
- https://medium.com/backticks-tildes/the-s-o-l-i-d-principles-in-pictures-b34ce2f1e898

**Engineering Design Patterns:**
- There are a bunch here: https://www.tutorialspoint.com/design_pattern/design_pattern_overview.htm

**References mentioned:**
- Martin Fowler -> https://martinfowler.com/ (Such a valuable source of information: browse the topics)
- Robert C. Martin

**Books:**
- Clean Code
- Refactoring
- The Pragmatic Programmer
- The Gang of Four

And then any specific programming language ones. I personally do not marry to any programming language, there is no better or worse programming Language, they have all been design with a purpose in mind.

**REMEMBER:**
- "Use the best tool for the right job"
- "Do not do over-engineering"
- "Share and share and let other people participate. "

Fernando Cejas

April 15, 2017
Tweet

More Decks by Fernando Cejas

Other Decks in Technology

Transcript

  1. hidden mysteries behind
    big mobile codebases.
    'reloaded edition'
    @fernando_cejas

    View Slide

  2. Meet @fernando_cejas
    โ†’ Curious learner
    โ†’ Software engineer
    โ†’ Speaker
    โ†’ Works at @soundcloud
    โ†’ fernandocejas.com

    View Slide

  3. This begins with a story...
    โ†’ You are a happy developer
    โ†’ You have a lightweight pet project
    โ†’ You are the only maintainer

    View Slide

  4. One man Development Process Model.

    View Slide

  5. At some point in time...
    โ†’ Project starts to grow...
    โ†’ More features are required...
    โ†’ You are extremely happy for its success...

    View Slide

  6. View Slide

  7. First problem: Success!
    Android total installs: 161.854.238
    iOS total installs: 122.423.077

    View Slide

  8. Success under the hoods
    โ†’ Codebase grows.
    โ†’ No tests.
    โ†’ Inconsistency across the code.

    View Slide

  9. View Slide

  10. Unsustainable situation. Why?
    โ†’ More requirements/features.
    โ†’ More contributors.
    โ†’ Time to market and dealines.
    โ†’ Complexity going up.

    View Slide

  11. Many questions to answer.
    โ†’ Can we add a new functionality fast?
    โ†’ Is our codebase prepare to scale?
    โ†’ Is it hard to maintain?
    โ†’ What about technical debt?
    โ†’ How to keep it healthy and sane?
    โ†’ Is it easy to onboard new people?
    โ†’ What about our team organization?

    View Slide

  12. Fact #1
    If your codebase is hard to work with...then change
    it!

    View Slide

  13. Soundcloud
    โ†’ From a monolith to a microservices architecture.
    โ†’ How we evolved our codebase on the server side.

    View Slide

  14. Soundcloud App Repo DEMO.

    View Slide

  15. What can we do in terms of...
    โ†’ Codebase.
    โ†’ Team Organization.
    โ†’ Working culture.
    โ†’ Processes.
    ...to support big mobile code bases?1
    1 Disclaimer: no silver bullets.

    View Slide

  16. Our Codebase and its worst enemies...

    View Slide

  17. Size
    Methods in app-dev-debug.apk: 95586
    Fields in app-dev-debug.apk: 61738
    Lines of code: 137387

    View Slide

  18. Complexity
    private Node delete(Node h, Key key) {
    // assert get(h, key) != null;
    if (key.compareTo(h.key) < 0) {
    if (!isRed(h.left) && !isRed(h.left.left))
    h = moveRedLeft(h);
    h.left = delete(h.left, key);
    }
    else {
    if (isRed(h.left))
    h = rotateRight(h);
    if (key.compareTo(h.key) == 0 && (h.right == null))
    return null;
    if (!isRed(h.right) && !isRed(h.right.left))
    h = moveRedRight(h);
    if (key.compareTo(h.key) == 0) {
    Node x = min(h.right);
    h.key = x.key;
    h.val = x.val;
    // h.val = get(h.right, min(h.right).key);
    // h.key = min(h.right).key;
    h.right = deleteMin(h.right);
    }
    else h.right = delete(h.right, key);
    }
    return balance(h);
    }

    View Slide

  19. Flaky tests
    +---------------------------------------------------------------+------------------------------------------------------+--------------+
    | ClassName | TestName | FailureCount |
    +---------------------------------------------------------------+------------------------------------------------------+--------------+
    | com.soundcloud.android.tests.stations.StationHomePageTest | testOpenStationShouldResume | 7 |
    | com.soundcloud.android.tests.stream.CardEngagementTest | testStreamItemActions | 4 |
    | com.soundcloud.android.tests.stations.RecommendedStationsTest | testOpenSuggestedStationFromDiscovery | 3 |
    | com.soundcloud.android.tests.player.ads.VideoAdsTest | testQuartileEvents | 2 |
    | com.soundcloud.android.tests.player.ads.VideoAdsTest | testTappingVideoTwiceResumesPlayingAd | 2 |
    | com.soundcloud.android.tests.player.ads.AudioAdTest | testQuartileEvents | 2 |
    +---------------------------------------------------------------+------------------------------------------------------+--------------+

    View Slide

  20. Anti-patterns
    public class NotificationImageDownloader extends AsyncTask {
    private static final int READ_TIMEOUT = 10 * 1000;
    private static final int CONNECT_TIMEOUT = 10 * 1000;
    @Override
    protected Bitmap doInBackground(String... params) {
    HttpURLConnection connection = null;
    try {
    connection = (HttpURLConnection) new URL(params[0]).openConnection();
    connection.setConnectTimeout(CONNECT_TIMEOUT);
    connection.setReadTimeout(READ_TIMEOUT);
    return BitmapFactory.decodeStream(connection.getInputStream());
    } catch (IOException e) {
    e.printStackTrace();
    return null;
    } finally {
    if (connection != null) {
    connection.disconnect();
    }
    }
    }
    }

    View Slide

  21. Technical Debt
    public class PublicApi {
    public static final String LINKED_PARTITIONING = "linked_partitioning";
    public static final String TAG = PublicApi.class.getSimpleName();
    public static final int TIMEOUT = 20 * 1000;
    public static final long KEEPALIVE_TIMEOUT = 20 * 1000;
    public static final int MAX_TOTAL_CONNECTIONS = 10;
    private static PublicApi instance;
    @Deprecated
    public PublicApi(Context context) {
    this(context,
    SoundCloudApplication.fromContext(context).getAccountOperations(),
    new ApplicationProperties(context.getResources()), new BuildHelper());
    }
    @Deprecated
    public PublicApi(Context context, AccountOperations accountOperations,
    ApplicationProperties applicationProperties, BuildHelper buildHelper) {
    this(context, buildObjectMapper(), new OAuth(accountOperations),
    accountOperations, applicationProperties,
    UnauthorisedRequestRegistry.getInstance(context), new DeviceHelper(context, buildHelper, context.getResources()));
    }
    public synchronized static PublicApi getInstance(Context context) {
    if (instance == null) {
    instance = new PublicApi(context.getApplicationContext());
    }
    return instance;
    }
    }

    View Slide

  22. How can we battle this enemies and
    conquer a large mobile code base?

    View Slide

  23. Fact #2
    Architecture matters:
    โ†’ New requirements require a new architecture.
    โ†’ Scalability requires a new architecture.

    View Slide

  24. Pick an architecture and stick to it
    โ†’ Onion Layers
    โ†’ Clean Architecture
    โ†’ Ports and adapters
    โ†’ Model View Presenter
    โ†’ Custom combination
    โ†’ Your own
    โ†’ Sacri๏ฌcial Architecture?

    View Slide

  25. Bene๏ฌts of a good architecture:
    โ†’ Rapid development.
    โ†’ Good Scalability.
    โ†’ Consistency across the codebase.

    View Slide

  26. View Slide

  27. Architecture Android
    public class MainActivity extends PlayerActivity {
    @Inject PlaySessionController playSessionController;
    @Inject Navigator navigator;
    @Inject FeatureFlags featureFlags;
    @Inject @LightCycle MainTabsPresenter mainPresenter;
    @Inject @LightCycle GcmManager gcmManager;
    @Inject @LightCycle FacebookInvitesController facebookInvitesController;
    public MainActivity() {
    SoundCloudApplication.getObjectGraph().inject(this);
    }
    protected void onCreate(Bundle savedInstanceState) {
    redirectToResolverIfNecessary(getIntent());
    super.onCreate(savedInstanceState);
    if (savedInstanceState == null) {
    playSessionController.reloadQueueAndShowPlayerIfEmpty();
    }
    }
    @Override
    protected void setActivityContentView() {
    mainPresenter.setBaseLayout(this);
    }
    @Override
    protected void onNewIntent(Intent intent) {
    redirectToResolverIfNecessary(intent);
    super.onNewIntent(intent);
    setIntent(intent);
    }
    private void redirectToResolverIfNecessary(Intent intent) {
    final Uri data = intent.getData();
    if (data != null
    && ResolveActivity.accept(data, getResources())
    && !NavigationIntentHelper.resolvesToNavigationItem(data)) {
    redirectFacebookDeeplinkToResolver(data);
    }
    }
    private void redirectFacebookDeeplinkToResolver(Uri data) {
    startActivity(new Intent(this, ResolveActivity.class).setAction(Intent.ACTION_VIEW).setData(data));
    finish();
    }
    }

    View Slide

  28. Architecture Android
    public class StreamFragment extends LightCycleSupportFragment
    implements RefreshableScreen, ScrollContent {
    @Inject @LightCycle StreamPresenter presenter;
    public StreamFragment() {
    setRetainInstance(true);
    SoundCloudApplication.getObjectGraph().inject(this);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(getLayoutResource(), container, false);
    }
    @Override
    public MultiSwipeRefreshLayout getRefreshLayout() {
    return (MultiSwipeRefreshLayout) getView().findViewById(R.id.str_layout);
    }
    @Override
    public View[] getRefreshableViews() {
    return new View[]{presenter.getRecyclerView(), presenter.getEmptyView()};
    }
    @Override
    public void resetScroll() {
    presenter.scrollToTop();
    }
    private int getLayoutResource() {
    return R.layout.recyclerview_with_refresh_and_page_bg;
    }
    }

    View Slide

  29. Fact #3
    Code evolution implies:
    โ†’ Constant refactoring.
    โ†’ Exploring new technologies.
    โ†’ Taking new approaches.

    View Slide

  30. Refactoring
    โ†’ Code evolution.
    โ†’ Boy scouting.
    โ†’ Baby steps.

    View Slide

  31. Code to refactor:
    private void startProcessing(Map map) {
    Processor myProcessor = new Processor();
    for (Entry entry : map.entrySet()) {
    switch(entry.getKey()) {
    case KEY1:
    myProcessor.processStuffAboutKey1(entry.getValue());
    break;
    case KEY2:
    myProcessor.processStuffAboutKey2(entry.getValue());
    break;
    case KEY3:
    myProcessor.processStuffAboutKey3(entry.getValue());
    break;
    case KEY4:
    myProcessor.processStuffAboutKey4(entry.getValue());
    break;
    ...
    ...
    }
    }
    }

    View Slide

  32. Create an abstraction:
    public interface KeyProcessor {
    void processStuff(String data);
    }

    View Slide

  33. Fill a map with implementation:
    Map processors = new HashMap<>();
    processors.add(key1, new Key1Processor());
    ...
    ...
    processors.add(key4, new Key2Processor());

    View Slide

  34. Use the map in the loop:
    for (Entry entry: map.entrySet()) {
    Key key = entry.getKey();
    KeyProcessor keyProcessor = processors.get(key);
    if (keyProcessor == null) {
    throw new IllegalStateException("Unknown processor for key " + key);
    }
    final String value = entry.getValue();
    keyProcessor.processStuff(value);
    }

    View Slide

  35. Fact #4
    Rely on a good test battery that backs you up.

    View Slide

  36. Technical debt
    "Indebted code is any code that is hard to scan."
    "Technical debt is anything that increases the dif๏ฌculty of
    reading code."
    โ†’ Anti-patterns.
    โ†’ Legacy code.
    โ†’ Abandoned code.
    โ†’ Code without tests.

    View Slide

  37. Fact #5
    Do not let technical debt beat you.

    View Slide

  38. Addressing and detecting technical debt:
    โ†’ Technical Debt Radar.
    โ†’ Static Analysis tools.

    View Slide

  39. Technical
    Debt Radar

    View Slide

  40. Fact #6
    Favor code readability over performance unless the
    last one is critical for your business.

    View Slide

  41. Perfomance
    โ†’ First rule: Always measure.
    โ†’ Encapsulate complexity.
    โ†’ Monitor it.

    View Slide

  42. Fact #7
    Share logic and common functionality accross
    applications.

    View Slide

  43. At SoundCloud
    โ†’ Android-kit.
    โ†’ Skippy.
    โ†’ Lightcycle.
    โ†’ Propeller.

    View Slide

  44. Fact #8
    Automate all the things!

    View Slide

  45. At SoundCloud
    โ†’ Continuous building.
    โ†’ Continuous integration.
    โ†’ Continuous deployment.

    View Slide

  46. Lessons learned so far:
    โ†’ Wrap third party libraries.
    โ†’ Do not overthink too much and iterate.
    โ†’ Early optimization is bad.
    โ†’ Trial/error does not always work.
    โ†’ Divide and conquer.
    โ†’ Prevention is better than cure.

    View Slide

  47. Fact #9
    Work as a team.

    View Slide

  48. Team Organization
    โ†’ Platform tech lead
    โ†’ Core team
    โ†’ Feature teams
    โ†’ Testing engineering team

    View Slide

  49. View Slide

  50. Working culture
    โ†’ Pair programming.
    โ†’ Git branching model.
    โ†’ Share knowledge with other platforms.
    โ†’ Agile and ๏ฌ‚exible.
    โ†’ Collective Sync meeting.

    View Slide

  51. Processes
    โ†’ Onboarding new people.
    โ†’ Hiring people.
    โ†’ Sheriff.
    โ†’ Releasing: Release train model + release captains.
    โ†’ Alpha for internal use (Dog fooding).
    โ†’ Beta community.

    View Slide

  52. Recap #1
    โ†’ #1 If your codebase is hard to work with, just
    change it.
    โ†’ #2 Architecture matters.
    โ†’ #3 Code evolution implies continuous improvement.
    โ†’ #4 Rely on a good test battery that backs you up.
    โ†’ #5 Do not let Technical Debt beat you.

    View Slide

  53. Recap #2
    โ†’ #6 Favor code readability over performance,
    unless it is critical.
    โ†’ #7 Share logic and common functionality accross
    applications.
    โ†’ #8 Automate all the things.
    โ†’ #9 Work as a team.

    View Slide

  54. Conclusion
    โ†’ Use S.O.L.I.D
    โ†’ Software development is a joyful ride.
    โ†’ Make it fun.

    View Slide

  55. Q & A

    View Slide

  56. Thanks!!!
    โ†’ @fernando_cejas
    โ†’ fernandocejas.com
    โ†’ soundcloud.com/jobs

    View Slide