paint-brush
Writing Good Code is like Writing a Novelby@bertwagner
4,169 reads
4,169 reads

Writing Good Code is like Writing a Novel

by Bert WagnerApril 5th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

I recently finished reading <a href="https://www.goodreads.com/book/show/12543.Bird_by_Bird" target="_blank">Bird by Bird</a>, an account by Anne Lamott on the struggles of being a writer.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail

Coin Mentioned

Mention Thumbnail
featured image - Writing Good Code is like Writing a Novel
Bert Wagner HackerNoon profile picture

I recently finished reading Bird by Bird, an account by Anne Lamott on the struggles of being a writer.

While thinking over her observations, I came to the realization that there are many similarities between a programmer writing code and an author writing a novel.

Characters

When writing a novel, authors need to get into the minds of their characters. Authors must grow to understand who their characters are, how they act, what their likes and dislikes are, and how they think. I use the word “grow” because it is rare for a writer to fully know the inner workings of a character at the start of a story; the characters evolve throughout the story writing process.

Programmers face the same scenario, but instead of characters we have customers. Although you might think you know your app’s customer from day one, it’s more likely than not that your understanding of that customer will change over time. Maybe you think they will use your application in one way, but after some user testing you discover they are using it in an unexpected but still beneficial way.

When this happens, you can’t try to force your customers to fit into the initial definition you had of them. Instead, you must evolve your understanding and application to meet the needs of the new definition of your customer.

First Drafts

Writers write. They write a lot. They write as much as they do because they understand that it’s better to get words down on a page, knowing full well that the majority of their work will get deleted or reworked in the editing phases. Although it can be very tempting to start editing once a sentence or paragraph is on the page, it breaks the writer’s flow and prevents even better work from being written. Writers will go through a first, second, and usually third and fourth drafts of their work.

The same can be applied to programming. Your first go at it should be focused on trying to get the code onto the screen and working as intended. Later on, you can go back and refactor once you realize you have some duplicate code or performance starts becoming an issue.

Over time you will evolve as a programmer and you’ll start writing cleaner and more concise code on the first draft. Just remember you can always perform another refactor. At the end of a day it’s better to have some ugly code that works rather than a few “perfect” lines of code that you can’t show to anyone.

Write Every Day

Writers always emphasize that they need to be in a routine and write everyday. It doesn’t matter if that day is working on a book, composing poetry, logging in a journal, or any other type of focused writing. Why do writers do this? They do it for the same reason that musicians, athletes, comedians, and chefs perform their craft every day: practice makes them better.

As programmers, we also need to practice on a regular schedule to get better. Whether it’s learning a new language or adding new functionality to an existing app, it’s important to code on a regular basis. Getting your brain to think of some code today will make it easier to create more code again tomorrow. And working on a program everyday this week will make it easier to come up with cleaner solutions to problems in the weeks and months that follow.

It doesn’t matter how many tutorials you watch on YouTube, books you read, or conferences you attend, at the end of the day if you aren’t applying what you learn through practice then you will not improve your craft.

Feelings of Self-Doubt, and Frustration

I’m empathetic with writers; often times they will spend months or years of their time writing a story, only to receive rejection letters from publishers or harsh reviews from critics for their completed creative work. On-top of that, the process to get a story finished is full of hurdles. Writer’s block can be debilitating even for the most seasoned professional. Feelings of fear, doubt, and self-loathing about the quality of the writer’s work are never lingering too far away either.

Programmers face the same types of problems. Often times we work on projects that feel never-ending and wish we could move on to something new. Other times we might feel like we are working on an unsolvable programming problem, or feel like the code we are able to write is of terrible spaghetti-code quality.

Although there’s no easy solution to any of these problems, it’s important to remember to celebrate the small wins and not just the long-term victories. If stuck on a problem, take a step away from he computer and do something else.

And if you feel like you are writing spaghetti code today, build some confidence by looking at some code you wrote a year ago — now that was some true spaghetti code :).