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

Code Like A Goldfish

Chris Arriola
September 10, 2015

Code Like A Goldfish

A talk I gave at Telegraph Academy on how to write clean, maintainable and scalable code.

Chris Arriola

September 10, 2015
Tweet

More Decks by Chris Arriola

Other Decks in Programming

Transcript

  1. Coding like a Goldfish Writing clean, maintainable, and “scale”-able code.

    Chris Arriola
  2. About Me • Wrote 1st mobile app in grad school

    at UPenn (2010) • Hewlett-Packard (2010 - 2011) ◦ Android WebOS & Computer Vision Engineer - Small codebase (R&D) • Edmodo (2011 - 2013) ◦ Android & iOS Development - Large codebase (Production > 1M users) • Independent contractor (2014) ◦ Android Development - Many small codebases (Production 100 - 100,000+ users) • Operator (2015) ◦ Android, iOS, Python, and Javascript - Large codebase (Beta 1000+ users)
  3. Goldfish.

  4. “Programming is the art of telling another human what one

    wants the computer to do.” - Donald Knuth
  5. • Easier to decentralize knowledge of the code base •

    Minimizes cognitive load • Maximizes code output • Increases Truck-Factor ◦ http://mtov.github.io/Truck-Factor/ Benefits
  6. How to Write Code like a Goldfish?

  7. • Design early • Don’t over optimize • Don’t repeat

    yourself (DRY) • Think small Tips
  8. • Have design/architecture discussions early and talk to your team

    • Easy to find flaws early, hard and time-consuming to fix later • Critique thoroughly • “Pseudocode-Driven Development” • Hammock-Driven Development ◦ Talk given by Rich Hickey, creator or Clojure (on YouTube). Design Early
  9. • Example: https://gist.github.com/arriolac/fd2431ab5e8c1cd9fac3 Design Early

  10. Don’t Over Optimize • Optimizing code takes a lot of

    time • Optimize for performance later • ...but optimize for readability and understandability early
  11. • e.g. a base class for displaying a list of

    groups public abstract class GroupsListFragment { // TODO: Currently both MyGroupsListFragment and JoinedGroupsListFragment are // making the same request and then filtering the results. We could optimize by // having those classes implement this method to return only the groups they care // about if the server API supported it. @Override protected NetworkRequest<JSONObject> createRequest(NetworkCallback<List<Group>> callback) { return (new GroupsRequest(callback)); } } Don’t Over Optimize
  12. Don’t Over Optimize “Premature optimization is the root of all

    evil.” - Donald Knuth
  13. DRY • Duplication requires more lines of code • Duplication

    is error prone • Solve duplication through abstraction ◦ Create a method or a class • Example: https://gist.github.com/arriolac/b8fb810dd152848ff11d
  14. Think Small • Write small files, methods, and classes •

    Smaller components are easier to test, swap, and change • Smaller files usually means good encapsulation
  15. Think Small (e.g.) function getScaledBitmapThatFitsWithinBoundsAndHasQualityAndDoFixRotation(sourceFilePath, boundsWidth, boundsHeight, fixRotation) { //

    Do stuff… return image; }
  16. Think Small (e.g.) function readImage(imageFilePath) { // Load image from

    file } function rotateImage(image, rotation) { // Rotate image by “rotation” } function scaleImage(image, scaleType) { // scale image from file } function resizeImage(image, width, height) { // resize image }
  17. Think Small (e.g.) function ImageLoader(imageFilePath) { this.filePath = imageFilePath; this.loadImage

    = function(width, height) { image = readImage(this.filePath); resizeImage(width, height); // apply other image mutations return image; } }
  18. Recommended Reading • “The Pragmatic Programmer” by Andy Hunt and

    Dave Thomas • “Design Patterns—Elements of Reusable Object-Oriented Software” by Erich Gamma, Richard Helm. Ralph Johnson and John Vlissides • “Refactoring” by Martin Fowler
  19. Questions?