Skip to content

How we learn to write code

Question

How have you learned to write code? Were you given any formal training?

Unless you studied Software Engineering, you may never have had any formal training. And that's okay! Nobody writes perfect code.

There are various resources available (including this book) that can help you to improve your coding skills. But the most effective way to improve is to write code and get feedback.

Tip

You can practice shooting eight hours a day, but if your technique is wrong, then all you become is very good at shooting the wrong way.

— Michael Jordan

How we learn to write papers

Throughout our research careers, we are continually learning and developing our ability to write scientific papers. One of the main ways that we develop this ability is to seek feedback early and often, by circulating drafts to co-authors, supervisors, and trusted colleagues.

This feedback not only helps us improve the paper that we're currently working on, but also improves our ability to write papers in the future.

We gradually learn how to express ourselves clearly at multiple levels:

  • Writing individual sentences that clearly convey a single thought or observation;

  • Constructing paragraphs that span a single topic or idea;

  • Structuring an entire paper so that the reader can easily navigate it.

How we currently learn to code

Many of us learn to write code as a by-product of our chosen research area, and may not have any formal computer programming training. However, while we may make our finished code available as a support material for our published papers, we don't typically show our code to our co-authors.

Info

While there are many reasons why we are reluctant to share our code, perhaps the biggest factor is a sense of shame. We may feel that our code is "bad" — too bad to share with others! — and that if we've ever made a mistake in our code, we're the only person who has ever done so.

This is simply untrue!

How we should learn to code

We should treat writing code the same way that we treat writing papers, grant applications, and fellowship applications: seek feedback early, and seek feedback often.

Question

Wouldn't you prefer that the first person who looks at your code is a trusted colleague, rather than a random person who has read your paper and now wants to see how the code works?

Peer code review offers a structured way to:

  • Discuss and critique a person's work in a kind and supportive manner;

  • Praise good work;

  • Identify where code is well-structured and clear, and where it could be improved; and

  • Share relevant knowledge and expertise.

Similar to writing papers, we should seek feedback at multiple levels:

  • Are individual lines of code clear and correct?

  • Are strongly-related lines of code grouped into functions that each do a single thing?

  • Are functions grouped into modules that focus on specific aspects or features?

  • Can the reader easily navigate the code?