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

Reusable Code

Reusable Code

Reusable code is the holy grail of consulting, but there are many challenges to make reusable code work. In this talk we go through tips for building libraries, open or closed source, and making them successful and profitable.

Ryan Harter

July 30, 2015
Tweet

More Decks by Ryan Harter

Other Decks in Programming

Transcript

  1. Harter’s Tips for (Code) Happiness • Identify good candidates •

    Limit scope and requirements • Design a good API • Document and Communicate • Treat it like a client project
  2. Identify Good Candidates • It’s not a library if it’s

    only used once • Don’t reinvent the wheel • Focus on core of app
  3. Identify Good Candidates • Google for libraries • Check Android

    Arsenal • Look at what other apps use • Be Confident • Count Stars • Inspect Source
  4. Limit Scope and Requirements • Choose a single problem to

    solve • Picasso - Efficient image loading and caching • Retrofit - Easy REST API access • Avoid “Apache Syndrome” • Every Apache library depends on every other Apache library
  5. Limit Scope and Requirements • Library mission statement • One

    sentence • What the library does “Allow the user to load an image from any source.”
  6. “Allow the user to load an image from any source.”

    Limit Scope and Requirements • Library mission statement • One sentence • What the library does
  7. “Allow the user to load an image from any source.”

    Limit Scope and Requirements • Library mission statement • One sentence • What the library does
  8. “Allow the user to load an image from any source.”

    Limit Scope and Requirements • Library mission statement • One sentence • What the library does
  9. Design a Good API • Who is your user? •

    If user needs to mess with code, it’s not a library • Design from the outside in • Package your library
  10. API Strategies - Utility Requirements: Persist filter state to support

    undo/redo. public interface HistoryManager<T> { public T undo(); public T redo(); public void push(T state); public void clear(); }
  11. API Strategies - Utility Requirements: Persist filter state to support

    undo/redo. public class HistoryManager<T> { public T undo() { … } public T redo() { … } public void push(T state) { … } public void clear() { … } } public class HistoryManager<T> { public T undo() { … } public T redo() { … } public void push(T state) { … } public void clear() { … } }
  12. public class HistoryManager<T> { public HistoryManager(File file) { … }

    public T undo() { … } public T redo() { … } public void push(T state) { … } public void clear() { … } } API Strategies - Utility Requirements: Persist filter state to support undo/redo. public class HistoryManager<T> { public HistoryManager(File file) { … } public T undo() { … } public T redo() { … } public void push(T state) { … } public void clear() { … } }
  13. public class HistoryManager<T> { public HistoryManager(File file) { … }

    public T undo() { … } public boolean canUndo() { … } public T redo() { … } public boolean canRedo() { … } public void push(T state) { … } public void clear() { … } } API Strategies - Utility Requirements: Persist filter state to support undo/redo. public class HistoryManager<T> { public HistoryManager(File file) { … } public T undo() { … } public boolean canUndo() { … } public T redo() { … } public boolean canRedo() { … } public void push(T state) { … } public void clear() { … } }
  14. API Strategies - Superclass public class BaseExportActivity extends Activity {

    /** * Returns a list of preferred packages which appear above the * divider with a slightly larger icon than the rest. * * @return the package ids of the preferred packages. */ protected List<String> getPreferredPackages() { return DEFAULT_PREFERRED_PACKAGES; } /** * @return the public folder name to save files to. */ protected File getSaveFolder() { return new File(getPublicPicturesDirectory(), "Pixite"); } /** * Allows the AppInfo to be updated for things like Refragment or * Refilter. Default implementation does nothing. * * @param info The app info to update. */ protected void updateAppInfo(AppInfo info) { } }
  15. API Strategies - Superclass public class BaseExportActivity extends Activity {

    /** * Returns a list of preferred packages which appear above the * divider with a slightly larger icon than the rest. * * @return the package ids of the preferred packages. */ protected List<String> getPreferredPackages() { return DEFAULT_PREFERRED_PACKAGES; } /** * @return the public folder name to save files to. */ protected File getSaveFolder() { return new File(getPublicPicturesDirectory(), "Pixite"); } /** * Allows the AppInfo to be updated for things like Refragment or * Refilter. Default implementation does nothing. * * @param info The app info to update. */ protected void updateAppInfo(AppInfo info) { } } public class BaseExportActivity extends Activity { /** * Returns a list of preferred packages which appear above the * divider with a slightly larger icon than the rest. * * @return the package ids of the preferred packages. */ protected List<String> getPreferredPackages() { return DEFAULT_PREFERRED_PACKAGES; } /** * @return the public folder name to save files to. */ protected File getSaveFolder() { return new File(getPublicPicturesDirectory(), "Pixite"); } /** * Allows the AppInfo to be updated for things like Refragment or * Refilter. Default implementation does nothing. * * @param info The app info to update. */ protected void updateAppInfo(AppInfo info) { } }
  16. API Strategies - Superclass public class BaseExportActivity extends Activity {

    /** * Returns a list of preferred packages which appear above the * divider with a slightly larger icon than the rest. * * @return the package ids of the preferred packages. */ protected List<String> getPreferredPackages() { return DEFAULT_PREFERRED_PACKAGES; } /** * @return the public folder name to save files to. */ protected File getSaveFolder() { return new File(getPublicPicturesDirectory(), "Pixite"); } /** * Allows the AppInfo to be updated for things like Refragment or * Refilter. Default implementation does nothing. * * @param info The app info to update. */ protected void updateAppInfo(AppInfo info) { } } public class BaseExportActivity extends Activity { /** * Returns a list of preferred packages which appear above the * divider with a slightly larger icon than the rest. * * @return the package ids of the preferred packages. */ protected List<String> getPreferredPackages() { return DEFAULT_PREFERRED_PACKAGES; } /** * @return the public folder name to save files to. */ protected File getSaveFolder() { return new File(getPublicPicturesDirectory(), "Pixite"); } /** * Allows the AppInfo to be updated for things like Refragment or * Refilter. Default implementation does nothing. * * @param info The app info to update. */ protected void updateAppInfo(AppInfo info) { } }
  17. API Strategies - Superclass public class BaseExportActivity extends Activity {

    /** * Returns a list of preferred packages which appear above the * divider with a slightly larger icon than the rest. * * @return the package ids of the preferred packages. */ protected List<String> getPreferredPackages() { return DEFAULT_PREFERRED_PACKAGES; } /** * @return the public folder name to save files to. */ protected File getSaveFolder() { return new File(getPublicPicturesDirectory(), "Pixite"); } /** * Allows the AppInfo to be updated for things like Refragment or * Refilter. Default implementation does nothing. * * @param info The app info to update. */ protected void updateAppInfo(AppInfo info) { } }
  18. API Strategies - Isolated Activity Intent i = new Intent(context,

    InspirationActivity.class); i.putExtra(EXTRA_USER_ID, "1234567890"); i.putExtra(EXTRA_ACCESS_TOKEN, "super_secret_access_token"); i.putExtra(EXTRA_SKIP_TAG, "fragmentpromo"); i.putExtra(EXTRA_USERNAME, "fragmentapp"); startActivity(i);
  19. API Strategies - Isolated Activity <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" style="?attr/inspirationActivityStyle"> <ListView

    android:id="@android:id/list" style="?attr/inspirationListStyle"/> <TextView android:id="@+id/error" style="?attr/inspirationTextStyle"/> </FrameLayout>
  20. API Strategies - Isolated Activity <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" style="?attr/inspirationActivityStyle"> <ListView

    android:id="@android:id/list" style="?attr/inspirationListStyle"/> <TextView android:id="@+id/error" style="?attr/inspirationTextStyle"/> </FrameLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" style="?attr/inspirationActivityStyle"> <ListView android:id="@android:id/list" style="?attr/inspirationListStyle"/> <TextView android:id="@+id/error" style="?attr/inspirationTextStyle"/> </FrameLayout>
  21. API Strategies - Isolated Activity <style name="AppTheme" parent=“Theme.AppCompat.NoActionBar"> <item name="inspirationActivityStyle">@style/FragmentInspirationStyle</item>

    <item name=“inspirationListStyle">@style/FragmentInspListStyle</item> <item name=“inspirationTextStyle">@style/FragmentInspTextStyle</item> </style>
  22. API Strategies - Isolated Activity <style name="AppTheme" parent=“Theme.AppCompat.NoActionBar"> <item name="inspirationActivityStyle">@style/ShiftInspirationStyle</item>

    <item name=“inspirationListStyle">@style/ShiftInspListStyle</item> <item name=“inspirationTextStyle">@style/ShiftInspTextStyle</item> </style>
  23. Document and Communicate • If a library is only used

    once, is it a library? • Communicate outside of engineering • Sales needs to know what to sell • Design needs to know components and limitations • Javadoc for Engineering, Wiki for others
  24. Manage the Project • Treat internal libraries like a client

    project • Dedicate resources • Manage requests • Define requirements • Enforce versioned release cycle
  25. Retrofit public interface GithubService { @GET("/users/{user}/repos") List<Repo> listRepos(@Path("user") String user);

    } http://square.github.io/retrofit/ A type-safe REST client for Android and Java
  26. Retrofit public interface GithubService { @GET("/users/{user}/repos") List<Repo> listRepos(@Path("user") String user);

    } RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint("https://api.github.com") .build(); http://square.github.io/retrofit/ A type-safe REST client for Android and Java
  27. Retrofit public interface GithubService { @GET("/users/{user}/repos") List<Repo> listRepos(@Path("user") String user);

    } RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint("https://api.github.com") .build(); GithubService service = restAdapter.create(GithubService.class); http://square.github.io/retrofit/ A type-safe REST client for Android and Java
  28. Retrofit public interface GithubService { @GET("/users/{user}/repos") List<Repo> listRepos(@Path("user") String user);

    } RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint("https://api.github.com") .build(); GithubService service = restAdapter.create(GithubService.class); List<Repo> repos = service.listRepos("octocat"); http://square.github.io/retrofit/ A type-safe REST client for Android and Java
  29. Retrofit public interface GithubService { @GET("/users/{user}/repos") List<Repo> listRepos(@Path("user") String user);

    } RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint("https://api.github.com") .build(); GithubService service = restAdapter.create(GithubService.class); List<Repo> repos = service.listRepos("octocat"); http://square.github.io/retrofit/ A type-safe REST client for Android and Java
  30. Android Support https://developer.android.com/tools/support-library/features.html • AppCompat • ActionBar and friends •

    Material Themes • CardView • GridLayout • MediaRouter • Palette • RecyclerView
  31. Calligraphy https://github.com/chrisjenx/Calligraphy Custom fonts in Android an OK way. <TextView

    android:layout_width="wrap_content" android:layout_height="wrap_content" app:fontPath=“fonts/Roboto-Bold.ttf”/> <style name="AppTheme.Widget.TextView" parent="..."> <item name="fontPath">fonts/Roboto-ThinItalic.ttf</item> </style> Styles Direct
  32. Butterknife http://jakewharton.github.io/butterknife/ View "injection" library for Android class ExampleActivity extends

    Activity { @InjectView(R.id.title) TextView title; @InjectView(R.id.subtitle) TextView subtitle; @InjectView(R.id.footer) TextView footer; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_activity); ButterKnife.inject(this); // TODO Use "injected" views... } }
  33. Butterknife http://jakewharton.github.io/butterknife/ View "injection" library for Android class ExampleActivity extends

    Activity { @InjectView(R.id.title) TextView title; @InjectView(R.id.subtitle) TextView subtitle; @InjectView(R.id.footer) TextView footer; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_activity); ButterKnife.inject(this); // TODO Use "injected" views... } }