How Learning to Write Can Help You Code Better

How Learning to Write Can Help You Code Better

I recently started writing. It’s something new to me, so I decided to approach the skill in the same way that I approached other skills: by immersing myself in books, articles, and style guides to get some advice.

A few months into my journey, I discovered that I’ve already heard similar suggestions. Even better, I had given advice of my own — not to aspiring writers but junior developers. It turns out programming is much more similar to writing than I expected. Here are some helpful tips from the best writers that you can use in your coding.

Create Your First Draft

You should think before you start writing. It doesn’t matter if you want to write a great novel or a great app. Thinking is crucial. But never let overthinking stop you from doing actual work. It’s easy to get stuck in considerations about the best approach to the problem, but to achieve something, you need to get your hands dirty.

At first, you don’t need to make your code perfect. You need to create any code that resembles the final solution. Don’t be afraid of failure. Assume you’ll fail at first. Done is better than perfect — at least for now.

Write your first draft as fast as possible. Maybe it won’t be any good, but you’ll explore some options and check if your approach is viable at all. Fast feedback is the best feedback. If your method failed, try another one. If the code works, you can build from there.

Thinking in drafts takes the pressure off. You know that this is the first of many attempts, so your expectations regarding the quality are lower and you can start rapidly. The faster you start, the quicker you’ll progress. Create drafts until your code works. Now is the time for ruthless editing.

Image for post

Think of your code as a map. Photo by Annie Spratt on Unsplash.

Know Your Audience

Writing and programming are both about communication. But who are you communicating with? Many programmers, especially early in their careers, think they need to communicate only with the machine. After all, the computer needs to understand instructions to execute them. But in reality, programmers communicate mostly with other programmers.

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” — Martin Fowler

Almost every non-trivial application is created by multiple programmers. What’s more, code is read much more often than it is written. You need to communicate with other programmers efficiently, so making the code readable is crucial. It helps to work faster, simplifies maintenance, and facilitates further development.

You may think that your side project’s readability is not essential. After all, you’re the only developer and you know what you’re doing. But do you? Imagine you need to take a break from the project for half a year. Are you sure you’ll understand that code line or that magical number in six months? I don’t think so.

Think of your code as a map. A map only you can use is, well, useless. Your map is for other people. Everyone who understands the map-reading basics should get it. Strive for this level of clarity in your code.

Here are some tips to make your code more like a map and less like a personal diary.

Write to express — not to impress

You can implement a tree traversal algorithm in 80 characters. That’s impressive, but what about all the programmers who don’t have your skill?

Next month or next year, someone will bump into your code and need to add or change something. How long will it take to understand the codebase? Writing code is not a contest. You are a part of the team.

Your team doesn’t want to be impressed by you — just to understand. When we want to be astonished, we can take a look at your code katas. In our project, we’d rather appreciate your consideration.

Follow the conventions

To become a successful writer, you need to follow ground rules. It’s the same with coding. Countless problems have already been solved. You should take advantage of that.

Follow the patterns created by your predecessors, and you’ll be understood. You don’t need to be a slave to others’ opinions. Just be sure you know why you’re breaking convention and if it’s worth it.

Cut excess fat

The longer it takes to create the application, the more cluttered the code will be. Remember to get rid of unnecessary things that impede code understanding.

Remove unused variables, misleading comments, and overly complicated abstractions. Leave only the code that solves the problem you need to address.

Also, make the code more concise as long as it doesn’t impair the readability.

Image for post

Reading great code will make your code better. Photo by Fabian Grohs on Unsplash.

Read Great Writers

Proficient writers are proficient readers. It’s much easier to become a great programmer if you take the time to read quality code.

Open-source is a great place to start looking for exquisite samples. Try to understand them. The unique code will let you get into its writer’s head. And if the writer is a good programmer, you certainly want to get into their head. It’s all for free!

You’ll be amazed at how many new patterns and ways of solving problems you can learn. There were many giants in the industry before you. Take a sip from their well of knowledge. Dissect the code you admire, imitate better programmers. It’s all waiting for you.

Don’t worry about encountering lousy code. You can learn from it too. Analyze mistakes in sloppy code and tiny bugs in edge cases. Reading code will let you develop and understand complex applications. Learn to distinguish between superb code and bad code, and try to make yours a bit better.

Did you find this article valuable?

Support Szymon Adamiak by becoming a sponsor. Any amount is appreciated!