Robin's Blog

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

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!

I guess this post is somewhat along the lines of Does Visual Studio rot the mind? and the following quote:

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:

  1. Write a bit of (fairly bad) code
  2. Compile and run
  3. Test with a poorly chosen test case
  4. Find it doesn’t work
  5. Make small change to the code on the off-chance that it might solve the problem
  6. 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).

Job well done, I think, and a useful piece of advice, I hope.


Categorised as: Lessons learnt from..., Programming


36 Comments

  1. Skhan says:

    I am guilty of using the paper and pencil to brainerate some code blocks.

  2. MZ says:

    Funny thing–I had a similar experience with the same language and came up with the same conclusion.

  3. john says:

    maybe you need one of those pens that do OCR? livescribe or something.

  4. Derek says:

    Great post. I think the pragmatic programmers call that bad process “programming by coincidence”. You write some code and hope that it works, and when it does work, you don’t revisit why it works.

    You’re definitely right. It gives a new meaning to think before you write.

  5. Doug says:

    Saying Computers Science is all about Computers is akin to saying Astrology is all about telescope. (dijkstra said that)

  6. matt says:

    I’ve been doing just this! It makes me look at the entire structure of the program differently simply because I’m seeing the code from a different angle.

  7. Dhruva Sagar says:

    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 :/

  8. Paddy3118 says:

    I agree that the computer can get in the way of thinking, but equally when you do get back to the computer: Don’t be afraid to throw a few attempts away!

  9. BlackDivine says:

    Good article, yes it does helps. I mostly do that when I am planning on writing a big thing, but I realize now I have to do it more often xD leave the computer :P

  10. Simon Hibbs says:

    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.

    Simon Hibbs

  11. Jojomonkey says:

    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.

  12. [...] brings me to this interesting article on the pros of writing code by hand on Robin’s Blog. about Soviet Hardball covers the new media arms race in digital design, technology, and [...]

  13. Ya says:

    По-моему єто клиника.
    тестировать код в уме…

  14. [...] 来源:Robin’s blog 我最近想明白了一些事情。最好的写程序的地方不是在你的计算机前,不是使用你的编译器、IDE或其它一些工具。这最好的地方是一个远离这些工具的场所 —— 是某个能让你认真的思考的地方。对于一个你很熟悉的编程语言,你很容易把你脑子里已经构思好的程序转换成编译器/解释器可以编译/解释的程序 —— 难就难在如何在脑子里先把程序编好。 [...]

  15. [...] 来源:Robin’s blog 我最近想明白了一些事情。最好的写程序的地方不是在你的计算机前,不是使用你的编译器、IDE或其它一些工具。这最好的地方是一个远离这些工具的场所 —— 是某个能让你认真的思考的地方。对于一个你很熟悉的编程语言,你很容易把你脑子里已经构思好的程序转换成编译器/解释器可以编译/解释的程序 —— 难就难在如何在脑子里先把程序编好。 [...]

  16. [...] 新闻来源:Robin’s blog 我最近想明白了一些事情。最好的写程序的地方不是在你的计算机前,不是使用你的编译器、IDE或其它一些工具。这最好的地方是一个远离这些工具的场所 —— 是某个能让你认真的思考的地方。对于一个你很熟悉的编程语言,你很容易把你脑子里已经构思好的程序转换成编译器/解释器可以编译/解释的程序 —— 难就难在如何在脑子里先把程序编好。 [...]

  17. [...] Want to write some code? Get away from your computer! [...]

  18. [...] News – Want to write some code? Get away from your computerMarch 21, 2011 – There is a link to: “Does Visual Studio rot the mind?’ where this [...]

  19. [...] Want to write some code? Get away from your computer! [...]

  20. BBQ Chickens says:

    Great post. I think the pragmatic programmers call that bad process “programming by coincidence”. You write some code and hope that it works, and when it does work, you don’t revisit why it works.

  21. Martyn Wilson says:

    “when your code doesn’t behave as expected, don’t use the debugger, think.”

    … or ask your Dad.

  22. [...] a friend pointed out to this excellent post by Robin Wilson which expresses a similar opinion. If you liked this post, you might be interested [...]

  23. Great idea ! Getting away from the computer is absolutely necessary somtimes, and going away for a while is just the thing you need to put your thoughts straight.

  24. Peter says:

    For me it’s most important thing that i have piece, whether i’m in front of computer or i’m just thinking about project

  25. [...] 本文是从 Want to write some code? Get away from your computer! 这篇文章翻译而来。 [...]

  26. 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.

  27. 即时比分 says:

    Great idea ! Getting away from the computer is absolutely necessary somtimes, and going away for a while is just the thing you need to put your thoughts straight.

  28. Culturismo says:

    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.

  29. Very interesting post. I’ve never tried to do this. The next time I use PHP, I’m going to pick up a pen & paper and try to write down code. Thanks for the idea.

  30. amazing post, you write some code and hope that it works, and when it does work, you don’t revisit why it works.

  31. Great website. 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.

  32. 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.

  33. hash says:

    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.

  34. […] 英文原文:Want to write some code? Get away from your computer! 翻译作者:html5tricks – 蒋丽丽 […]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>