Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How *not* to design an SDK

How *not* to design an SDK

Talk about good API and SDK design practices

Javier Gamarra

May 14, 2016
Tweet

More Decks by Javier Gamarra

Other Decks in Programming

Transcript

  1. who? • Javier Gamarra / @nhpatt / nhpatt.com • @cylicon_valley

    (19-21/05|04/06) / @agilespain (01-02/07) • @liferay • java/javascript
  2. Select your audience • Mobile developers vs Liferay developers •

    Android vs iOS • Uniform API vs divergent
  3. • Don’t be like the Android SDK ◦ Only if

    you have a captive audience :P • Give feedback in each step • Follow the platform conventions (jcenter/cocoapods) Reduce friction
  4. Do 1 thing and several • Modular compile project(':liferay-screens’) compile

    project(':liferay-material-viewset') compile project(':liferay-westeros-viewset') compile project(':listbookmarkscreenlet') • Enable features with builders
  5. Do 1 thing and several If you become a “Bag

    of things” you’ll need the be the best bag of things
  6. how

  7. Hide complexity • Don’t introduce new concepts (or just a

    few) • Hide internal concepts and problems • Don’t do this: public BasicEvent(int targetScreenletId, Exception exception) { _targetScreenletId = targetScreenletId; _exception = exception; }
  8. Allow extension and customization Don’t restrict your users: The SDK

    should be extensible Allow advanced use cases
  9. Classes should be extensible • Use callbacks • Interfaces to

    allow extension • Fluent interfaces with builders • Factories • Strategy pattern • Template methods • Custom implementation
  10. Callbacks Use callbacks/listeners to notify the developer loginScreenlet.setListener( this); @Override

    public void onLoginSuccess(User user) {info( "Login successful!");} @Override public void onLoginFailure(Exception e) { error( "Login failed", e);}
  11. Interfaces Use interfaces to allow extension public interface CredentialsStorage {

    void storeCredentials(); void removeStoredCredentials(); boolean loadStoredCredentials() throws IllegalStateException; }
  12. Builders Interact with the SDK with a fluent interface through

    builders CredentialsStorage storage = new CredentialsStorageBuilder() .setContext(context) .setStorageType(storageType) .build();
  13. • Force one • Let the developer implement it •

    Provide a default and allow overriding UI?
  14. UI Pass a layout, use a viewmodel (interface) as a

    contract and provide a default <com.liferay.mobile.screens.webcontent.display.WebContentDisplayScreenlet android:layout_width= "match_parent" android:layout_height= "wrap_content" liferay:layoutId="@layout/webcontentdisplaystructured_example"/>
  15. UI • Follow platform conventions • Colors with the primary

    scheme • Themes with default layouts and colors • Layouts (defaults) overridden by android conventions
  16. Use sane defaults Allow the developer either to use this:

    <com.liferay.mobile.screens.webcontent.display.WebContentDisplayScreenlet android:layout_width="match_parent" android:layout_height="wrap_content"/> Or that: <com.liferay.mobile.screens.webcontent.display.WebContentDisplayScreenlet android:layout_width="match_parent" android:layout_height="wrap_content" liferay:articleId="@string/liferay_article_structured_article_id" liferay:labelFields="@string/liferay_articles" liferay:layoutId="@layout/webcontentdisplaystructured_example" liferay:offlinePolicy="REMOTE_FIRST" liferay:structureId="@string/liferay_article_structured_structure_id" />
  17. Easy to use • Follow language idioms • Always be

    compatible with latest versions • Watch out environments (liferay vs android)
  18. Easy to use The Android SDK (or iOS) shapes your

    library: • Orientation problem? (hide!) • Permissions (simplify!) • Dependencies (watch out…) • Sync vs async (be opinionated inside!) • Solve common problems (like cache layer) • Component oriented (or application class)
  19. Hard to misuse • Support null arguments • Gracefully degrade

    • Catch unexpected exceptions (and inform the user consistently)
  20. • Tutorials (task oriented) • References • Javadocs • Forums

    (and bug tracker!) • Youtube Document everything
  21. References • Client library design • Best practices for ios

    api design • Building first class Android SDKs
  22. “Programming is a little bit like sex. If you make

    one mistake, you support it for a lifetime”
  23. Versions 1. Break all the things 2. Acquire users 3.

    Be careful 4. Watch another talk