Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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)

Slide 3

Slide 3 text

Goldfish.

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

● Easier to decentralize knowledge of the code base ● Minimizes cognitive load ● Maximizes code output ● Increases Truck-Factor ○ http://mtov.github.io/Truck-Factor/ Benefits

Slide 6

Slide 6 text

How to Write Code like a Goldfish?

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

● 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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

● 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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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 }

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

Questions?