Want to write some code? Get away from your computer!

March 19, 2011

I’ve recently realised something. The best place to write code isn’t in front of your computer, with your compiler, IDE and tools. The best place to write code is far, far away from any of these tools – somewhere where you can think properly. For a language with which you are fairly familiar, the mechanics of translating the program in your mind to a program that the compiler can compile (or the interpreter can interpret) is fairly easy – it’s coming up with that program in your mind which is hard.

The other day I was on a train journey. I had my laptop, but no internet. Unfortunately I was using a commercial programming language (IDL, as it happens) for which I need to use my university’s site license. As I didn’t have access to the internet, I couldn’t get hold of the site license, so couldn’t run the compiler and IDE. Say what you like about commercial programming languages which require expensive licenses, but it stopped me from actually writing code in my editor with the compiler. And…guess what…it actually made me think!

One of the best lessons I learnt from my first boss was: “when your code doesn’t behave as expected, don’t use the debugger, think.”

That is what being away from your compiler forces you to do. It’s very easy to slip into the mindset of:

Write a bit of (fairly bad) code

Compile and run

Test with a poorly chosen test case

Find it doesn’t work

Make small change to the code on the off-chance that it might solve the problem

Repeat…

Of course this leads to code in the end that is ill-understood by the programmer, probably fairly buggy and not well tested.

Being away from the computer forces you to run through all of the thoughts in your head – which tends to take longer than getting a computer to compile and run your code (for small code bases at least…). So you don’t tend to make tiny changes and re-run things, you tend to actually think about what the code is doing. Until I did this on the train the other day, I hadn’t actually run a piece of code on paper (that is, written down columns for each of the variables and worked out what each value will be at each stage in the program) since my Computing A-Level exam!

In the case of the code I was writing the other day, I managed to produce some high quality, fast, bug-free code by writing it in long-hand on a piece of paper, thinking about it, gradually typing up bits of it, thinking some more, and then after a long time trying it in the compiler. The code (which was some region-growing image segmentation code which involved lots of recursion) was eventually copied from my piece of paper to my IDE, compiled (with only one syntax error – impressive I think) and ran correctly first time (and completed all of the tests that I had also devised on paper).

Yea I agree completely, I pick up a pen & paper and try to write down code and this of the corner cases, it helps a lot!
I use it as an excuse to write on paper too, which doesn’t happen very offten :/

When I’ve got a tricky programming problem to solve, I try to force myself not to listen to podcasts and music on the train so that my mind is free to think about the problem. I love my iPhone, it’s a valuable tool and more than an entertainment device, but I worry that it reduces the quiet time I have available to just think.

Any good professor will tell you the same. I always start on paper for some of the reasons mentioned. It helps produce more efficient code as one on paper will want to write less and think more about the algos, patterns, etc of the program.

Another piece of advice that I learned in college: all my friends used IDEs with intellisense while I stuck to my Vim. I later realized how intellisense made them crappier coders because when asked about the signature of a function, return codes, etc… they could never recall – esp during exams. For students, learning the hard way is best.

Another piece of advice that I learned in college: all my friends used IDEs with intellisense while I stuck to my Vim. I later realized how intellisense made them crappier coders because when asked about the signature of a function, return codes, etc… they could never recall – esp during exams. For students, learning the hard way is best.

Another piece of advice that I learned in college: all my friends used IDEs with intellisense while I stuck to my Vim. I later realized how intellisense made them crappier coders because when asked about the signature of a function, return codes, etc… they could never recall – esp during exams. For students, learning the hard way is best.

While I do agree that stepping away from the computer helps for getting started or when you’re stuck, I am more in line with people using CSS rather than Photoshop for prototyping. As the author wrote, converting thought to code is fairly easy, so by writing as I think, I free my mind from containing all the stuff I thought out already.

I totally agree with you, I used to write code 1.Write a bit of (fairly bad) code
2.Compile and run…. It works almost every time. But later I always found that I don’t really understand the how it works.