Writing Code is like Writing Fiction

My Coding Philosophy

Nuwan I. Senaratna
On Technology
5 min readApr 26, 2024

--

During a Q & A, someone asked, “What is your coding philosophy?”

The question was somewhat “irrelevant” because the talk I was giving was on Data Visualization. But I still attempted an answer, and a surprisingly interesting dialogue followed.

I ended with “And so yes. For me, writing code is like writing fiction”.

I thought of expanding on this quote in this article.

Be Creative

Inexpert coders are told exactly what to do. They are effectively “translators” who translate detailed requirements specifications into code.

“Write a class called ‘person,’ which has attributes name, address and age.”

Expert coders are often also told what to do. But the “what” is often vague, high level, very big picture, and “up in the air.” It’s like a germ of an idea to author a novel.

“Write a novel about how Modernization affected a traditional village in Sri Lanka.”

If you want to be an expert coder, you need to learn how to be creative. Like a novelist. That means, learning how to turn vague ideas into large codebases. Not unlike a complicated building complex.

It's no coincidence that expert coders are often referred to as “architects.”

An interesting aside. AI coding tools like GitHub’s CoPilot do most uncreative coding tasks fairly well. But they are terrible at creative coding. So, in the future, it is almost certain that only expert coders will be needed. In other words, all coders will be like novelists.

Draft, Draft and Draft again

All great works of fiction are not written.

They are written, and then re-written, and again rewritten many, many times. It is not unusual for a novel to have a several dozen drafts and revisions before it is published.

I’ve heard coders say that you should be able to write good code at one go. I agree, you can write good code in a single attempt. But to write great code, you need to write in many times.

This also ties to the previous point. You can’t be very creative if you worry too much about style, and coding conventions, and writing your code right the first time.

So, personally, I like to write my first draft of code very quickly, and very, very freely, letting my mind be as creative as possible. And they I refactor over several iterations. Until the code is not only creative, but also correct and conventional.

Interestingly, I’ve also found that this method is much faster.

Finally, because you get your first draft out very quickly, you can also “fail fast,” if you need to.

Develop Style

You can read one short paragraph from a Martin Wickremasinghe Novel and say immediately, “This was written by MW.” You can hear a short sequence of tones plucked from a sitar and say “That’s Anoushka Shankar.” You can run your eyes in circles around the bodies of a bevy of buxom ladies and say “That’s George Keyt.”

And you can also look at a few lines of code, and immediately recognize a great coder.

Now, like I said before, good coders follow correctness and convention, and in this sense often it is difficult to tell the code of one good coder from another good coder.

But great coders go one step beyond.

Obviously, they write correct code. Otherwise, it would not work, or worse it would work in the wrong way, and airplanes would fall out of the sky.

But great coders don’t always follow conventions. Because conventions are exactly that — conventions. A way in which people have agreed to usually do something. Great coders develop their own conventions, and often these conventions become the new conventions.

Collectively these novel and imaginative new conventions define the coder’s style. So, create your own style.

Have fun!

Great writers write not because they like to write, or they want to write, but because they have to write. Not because of some external pressure, but because the words are swelling inside their beings, and are threatening to burst out.

Great coders are also like that. Constantly they have ideas for all manner of interesting coding projects.

This instinct must be encouraged. When you have an idea for something, it must not be suppressed. If you suppress it, the ideas will gradually stop coming.

So, whenever you have an idea for some coding project, code it up. Nevermind if it is frivolous or even trivial. If you already haven’t, create a GitHub account for your fun projects, and the moment you have an idea code it up. Don’t worry if you’re never going to do anything with it.

Once in a while, you will find that one of your fun projects will also be a very useful, even remunerative project.

True. Great writers earn a lot of money from writing. But that’s not why they write. They write because it is fun. All great coders, code because it is probably the most fun thing that they do. So, don’t suppress fun.

Read

You can’t write fiction if you don’t read fiction.

The same applies to code. Try to read as much code as you can, and not just passively, but actively. Noticing patterns, styles and why certain things were done in certain ways.

Another good way to force yourself to read code is to review code. Many coders don’t like to review other people’s code. They often think it an unproductive thing to do. In fact, it is a highly effective way to learn.

Try to review code written my coders who are more expert than you. Sadly, too many software companies don’t allow this. But try to do it anyway.

Make Notes

When I read a novel, I make a lot of margin notes about things I’ve noticed. The better the novel the more prolific the notes.

The coding equivalent to margin notes is comments. When you read code, make comments.

If your “not allowed” to add comments, then check out your own version of the code base and make your own private comments.

Never stop coding

Did you ever hear of a great novelist, great musician, or great painter change careers once they were a certain age? Would they stop writing, performing, or painting, and (say) “go into management”?

True — as a coder becomes more senior in an organization, they have to take on new responsibilities like managing other coders. But this never needs to be at the expense of coding.

I’ve managed quite large groups of people, spread across multiple teams. But I’ve never seen that as a reason to stop coding. I’ve always made time to code.

In the best software companies, coding is respected, and considered a “first-class skill.” Coders are the highest paid and the most respected people in the company.

In mediocre companies, coding is considered a low-level skill, performed by servants. The masters are in management, and if a coder wants to earn, not just money, but also respect, they have no choice but to become managers. If you are serious about being a coder, don’t join a mediocre software company that treats coders like servants.

Don’t forget: Once a coder, always a coder!

DALL.E

--

--

Nuwan I. Senaratna
On Technology

I am a Computer Scientist and Musician by training. A writer with interests in Philosophy, Economics, Technology, Politics, Business, the Arts and Fiction.