HomeAboutThreads

programming feels like writing

April 08, 20203 min read

Sometimes programming is lumped together with math and science. Apparently, it’s for people who are logical and ‘brainy’. While logical thinking is a component of programming, most people under-estimate how creative programming is.

Paul Graham compares programming to painting; I find programming similar to writing prose.

When you program, you need to write code that a computer can read. That’s where logical thinking comes in, because a machine only understands words based on a strict rule-based system.

However, in modern-day programming, you also need to write code that’s human readable because you and your colleagues will return to the code for years. When you add a new feature or fix bugs, you first have to read through the current code and understand how it works.

You’ll often hear programmers talk about code being ‘elegant’. What they mean is: there are many ways to write code that will functionally do the same thing. However, the particular way this code was written is easily understandable, or abstracts away complexity in an unexpected way.

Another way that creating software is like writing is that there’s a lot of editing. Just like in writing, you start with a messy first draft. If you’re writing a function, you first focus on getting the function to work; that is, making it computer-readable.

After the function works, you edit your code to improve conciseness and human-readability. Maybe a function you originally named `sentence` should be named, more descriptively, `replyToPuppy.` Maybe a variable called ‘string’ is not needed at all.

// Poorly named function ("sentence") and argument ("input")
// Extraneous variable "string"
function sentence(input) {
  string = `Hello ${input.name}!`
  if (input.good) {
    string = 'Good boy!'
  } else if (input.bad) {
    string = 'Bad dog.'
  }
  return string
}

// Well named function ("replyToPuppy") and argument ("puppy")
// Removes extraneous string
function replyToPuppy(puppy) {
  if (puppy.good) {
    return 'Good boy!'
  } else if (puppy.bad) {
    return 'Bad dog.'
  } else {
    return `Hello ${puppy.name}!`
  }
}

This is a contrived example, but the more complexity you are trying to encapsulate in your code, the more critical good editing is.

Imagine the difference between tweeting and writing a 3000-word article. You can send a tweet without much thought, but a long article requires outlining, writing, getting feedback, re-writing. A complex program is similar. You decide what you want the program to do. Then you outline how you will organize your data, what helper functions you’ll write. Then you start coding, and realize you’ve forgotten to account for edge case X, Y, and Z, and that your original plan isn’t as straightforward as you thought.

When you finally have a first draft, a co-worker edits your work (except in programming-speak we call it ‘code review’).

How could the code be more concise? Better named? What edge cases is your code not accounting for? Edge cases in code might be analogous to the possible rebuttals someone might have to your article.

So the process of writing and programming feel similar to me. You are stringing together complex ideas, and that takes multiple drafts, good word choice, clarity of thought.

Programming is closer to writing than math, and if you enjoy writing, you might enjoy programming. If you might be interested in learning programming, DM me! I’m happy to point you towards programming resources or answer any questions you have 😊