Wednesday, April 11, 2018

On Good Code

Good code.  What makes a codebase good?  What makes good code... well, good?

Coming into a new company again has refreshed my mind on what it is like to delve into a complex pre-existing codebase for the first time.  Sometimes the experience is agonizing, sometimes it's fairly straightforward, and sometimes it lies somewhere in between.

I remember when I first started at PlayStation, who are for the most part, a Java shop.  Getting my environment set-up, discovering the shape of things, where to find things, what libraries were there and beginning to dig into the project I'd be working on.  Opening up my IDE for the first time and initializing the first maven pom into a project for IntelliJ to index and for me to digest.

I pulled out my OmniGraffle, and started making diagrams for my own edification, tracing from the start of the application flow, where requests arrive, and following the call flow all the way down into the guts of the application where SQL queries start popping up and the particulars of the relational organization of the platform become evident, making flow charts and relational diagrams as I go so I can understand what kind of a picture it all paints.

Today, coming into a new company, I begin the process all over again, but this time I'm in a director role, so today, my chief concern is more to do with deliverables and project timelines, overarching technology goals and direction that it is with the low level of our implementation.  However, being a startup, it means that whilst my primary focus is more long-term, I have to be conversant with all those gruesome details, and able to function therein.  It's PHP, it's AWS, it's Docker, it's Linux, it's MySQL.

My standards for what "good code" looks like are very high.  Over the years I've discovered this in my professional life, and that most people aren't nearly as exacting as my preferences would prefer.  It's been a long journey from the young up-and-commer who left Southampton University for the U.S.A with a baby on the way and dived into commercial software development with gusto to today, a Engineering Director for a small company in California.  Over those years I've worked on many systems, from the smallest companies like ZiftIt, where the technology team consisted of me doing all the core engineering, a front-end guy doing UI, and the CTO, to vast sprawling multi-billion dollar organizations like Sony PlayStation where I was just one cog in a vast machine delivering content at massive scale to users the world over.

One common thread that shows up across these companies is that good code makes a difference.  Not in theory, but in practice.  I've lived in companies where the toxicity of the codebase rose up and strangled the organization from within as it took more engineers just to beat back the zombies and skeletons of rushed implementations, where interacting with system became an effort in managing the edge cases that were so prevalent it was like trying to play patty-cake with Edward Scissorhands, and the edge-cases were so much at the edges as baked all the way through.

Let me start by describing what good code feels like.  When your codebase is good, it feels safe.  It's a warm blanket that welcomes you to work in the morning, where you feel confident that your timelines are accurate.  Where you can estimate with ease, and new features are just a matter of solving for the complexity of the design.  Where you go home on Friday, and thinking about refactoring something, arrive Monday, and the refactoring is done by close of business Tuesday.  Where when a business owner asks for a new feature, you smile and say, it mostly already does that because it's just a logical extension of the relational design.  Where you can look at your database, and immediately get a sense of what the data means.

Contrast that with bad code.  Where any step taken is fraught with peril.  You can't change anything for fear of the whole system collapsing like a house of cards, worse even, you daren't even step heavily around it, in case the table shakes and the whole thing just collapses apparently of it's own accord.  Where implementing anything requires long heavy test cycles that seem to take forever, and where business is always angry that what they are being given is so full of bugs and problems that never seem to go away all the way.

I want to take a moment now to look at why this is.  What makes one codebase such a pleasure to work with, and another such a horrible pain.  Let's think about the human psyche, where we came from and who we are for the world, let's get metaphysical for a moment.  When you open up a good novel and dive in, what is it that is engaging?  When you look at a page of mathematics, unless you have a PhD in Math, why does it occurs as noise?  All this points to the first trait of a good codebase:

It tells a story.

Open up the sourcecode to your project, and see, what is the story it's telling say?  Can you tell?  The statistics suggest that the average developer spends 10x as much time reading code in a day than they spent writing it.  If your codebase isn't telling a compelling story, and doing so in a way a good book does, you've probably got a pile of frustrated coders on your hands.  When you open up a class or script and your brain fires off in horror "Oh my god, who wrote this?!", or "Oh my god, what does this even do?!", you know you might have a problem.

I'm not an English major, my wife holds that distinction in our family, but I know that a good story has compelling characters, solid plot, a place it starts, a clear direction, and a place it ends up.  The best stories might be surprising or insightful or emotional; but they are all engaging and compelling.

If your codebase doesn't tell a compelling story, there's a pretty good chance that your product doesn't either, and that your company doesn't either.  Conway's law says that companies write applications with the same structure as their organization.  If your code base looks a certain way, it might be an indication of your organizational culture, and, that might also be something you want to look at.

If you look at the Clean Code book by Robert C Martin, you'll see that code that has clearly distinguished levels of abstract will have a mixture of fuction or method types.  There will be methods that talk almost in English: return userDataAccess.fetchUsers() map (getPersonalData andThen tokenize).  A non-developer can, with just a little explanation of what what "map" does, fully understand what this accomplishes!  This function tells a story.  If you don't have functions or methods in your code that looks like this, this is a strong symptom of failing to have appropriate levels of abstraction.  It also means that you like have a great deal of copy/paste in your system going on and that refactoring anything is going to result in you finding places where the same operation is performed with slight variations that were never normalize.

Okay, so you've realized that what you have on your hands is a dry math paper, and not a novel.  What can you do about it?

Cure for code that doesn't tell a story: normalize the heck out of it.

Go through, start seeing where there are services present.  like userDataAccess.fetchUsers.  If what happens all over the place is raw queries to your datastore, you'll benefit from normalizing this into a service component.  Normalize with a passion, normalize with vigor.  You'll see the size of your codebase shrinking and shrinking.  You'll start to see the story of your system emerge.  And, you'll start to see productivity rise.  If you didn't have any before, you'll be able to write tests now.  If you had tests before, you'll start seeing them simplify greatly.  You'll start to see developers actually want to write then because it enables them to develop new features faster.  Incidently, this is also one antidote for tangling and scattering, another common problem.

That's enough pontificating for one morning, I'll come back and write a part two, where, I'll talk more about tangling and scattering, and also abstraction versus simplification.

No comments:

Post a Comment