Like it or not coding is now a major part of science. Some might debate that point but this is my column and these articles go a lot better if you all just agree with me. Also, by coding, I mean programming or writing scripts that make computers go. I feel I have to be clear because I recently got in to an argument online about ‘coding’ being outdated and “implies difficulty and confusion”. Again, let’s just assume I’m right.

Anyway, tangents about my rightness aside, coding is very important to almost all branches of science – from small calculations, to larger development of controlling equipment, or setting up an early warning system based on the force applied by your boss as he opens the door linked to a gauge of how mad he is.

I came to coding quite late in life (aged 29). The very first week of my PhD I was plonked in front of a piece of equipment I had never seen or heard of before and shown a computer program that included a check box with no label that you needed to scroll to and tick. I asked why and was told “oh, that makes it work somehow – the last student put it there”. Which led me to think a) that the last student was possibly and idiot, and b) if the student was an idiot and if he can code that in surely I can code it back out again.

This then led to me spending about 2 months trying to understand what all the wiggly lines meant, what breaks if I delete stuff and why it randomly says “critical error” when it starts but then carries on just fine. But eventually I learned enough to replace the tick box with some better code AND add in some nifty features which saved me about 2 hours per experiment.

Back then I was learning Labview by editing other people’s code. I’ve since moved on from that and now write quite a lot of my own. But over the last few years I’ve been learning Python which has reminded me of some key advice for anyone starting out that I think is worth sharing.

Firstly, if you want to use code – start small. There is no point thinking “hmm, I need a program that has a billion features and literally does everything”. The first Python code I wrote was a script that simply did a little bit of maths I needed. Within a few weeks I was pretty much just using my Python window as a really efficient calculator.

From there, I grew my calculators into little scripts to process my data sets. Normally I’d been doing my data set processing in Excel, which is fine but limited, and can be a bit annoying if you don’t already know the cryptic excel formulae (despite using it several times I still barely understand how hlookup() works).

Python is much better for just crunching a thousand values, converting them to other units and then producing a nice little summary file. If you’re doing anything more complicated than averaging and unit conversion (like resampling, rolling averages or searching) your entire python code will probably end up shorter than the required excel formula!

Getting into coding is like learning anything. Be reasonable about what you are going to do from the start. There are some amazing how-to guides for very clever projects like sword wielding robots or games of thermonuclear warfare. This is a bit like planning a trip to France before you’ve even start learning French. Don’t start it thinking “I’ll be re-designing operating systems by the end of the week”. Start it thinking “I wish I could process my data slightly faster than I do already” because that is the actually useful power of code in science.

Hi there!

Sign up to receive awesome cartoony content in your inbox, every month.

We don’t spam! Read our privacy policy for more info.


Leave a Reply