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

    View Slide

  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)

    View Slide

  3. Goldfish.

    View Slide

  4. “Programming is the art of telling another human
    what one wants the computer to do.”
    - Donald Knuth

    View Slide

  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

    View Slide

  6. How to Write Code like a Goldfish?

    View Slide

  7. ● Design early
    ● Don’t over optimize
    ● Don’t repeat yourself (DRY)
    ● Think small
    Tips

    View Slide

  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

    View Slide

  9. ● Example: https://gist.github.com/arriolac/fd2431ab5e8c1cd9fac3
    Design Early

    View Slide

  10. Don’t Over Optimize
    ● Optimizing code takes a lot of time
    ● Optimize for performance later
    ● ...but optimize for readability and understandability early

    View Slide

  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 createRequest(NetworkCallback> callback) {
    return (new GroupsRequest(callback));
    }
    }
    Don’t Over Optimize

    View Slide

  12. Don’t Over Optimize
    “Premature optimization is the root of all
    evil.”
    - Donald Knuth

    View Slide

  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

    View Slide

  14. Think Small
    ● Write small files, methods, and classes
    ● Smaller components are easier to test, swap, and change
    ● Smaller files usually means good encapsulation

    View Slide

  15. Think Small (e.g.)
    function getScaledBitmapThatFitsWithinBoundsAndHasQualityAndDoFixRotation(sourceFilePath,
    boundsWidth, boundsHeight, fixRotation) {
    // Do stuff…
    return image;
    }

    View Slide

  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
    }

    View Slide

  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;
    }
    }

    View Slide

  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

    View Slide

  19. Questions?

    View Slide