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. 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)
  2. “Programming is the art of telling another human what one

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

    Minimizes cognitive load • Maximizes code output • Increases Truck-Factor ◦ http://mtov.github.io/Truck-Factor/ Benefits
  4. • 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
  5. Don’t Over Optimize • Optimizing code takes a lot of

    time • Optimize for performance later • ...but optimize for readability and understandability early
  6. • 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
  7. 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
  8. Think Small • Write small files, methods, and classes •

    Smaller components are easier to test, swap, and change • Smaller files usually means good encapsulation
  9. 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 }
  10. 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; } }
  11. 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