Learn Programming as a Laboratory Scientist
Stop banging your head against a textbook.
Four chapters into “MATLAB for Neuroscientists”, my friend asked me for advice. Her knowledge of psychology and neuroscience helped her a lot as an experimentalist, but with little background in mathematics or programming, my friend found herself slogging through the textbook. Feeling stuck in learning programming was all too familiar to me — although I read some programming books, took some programming classes, and successfully taught myself to build functional robots, it was AFTER these experiences when I gained strong reasoning abilities for programming. In retrospect, I could have saved at least a hundred hours of frustration if I knew what I know now about how to learn programming. I gave my friend the same advice I would give my past self.
The textbook is a reference
Textbooks are great because they put a lot of information in one place, they have indexes, and the reader can look the things up that they need. But textbooks can also be very dry and devoid of analogies. Learning programming by reading a textbook is like learning a foreign language by reading a textbook on grammar in the foreign language. Without enough prior knowledge to begin with, you may find yourself lost. Although the textbook may seem like the most accessible way to get started in programming, conversations with people who program can be more useful for a person who is very new to the field.
Learn what to Google
When you are so lost that you do not know what to Google, you will need to learn what to Google. Like the textbook, Google is also a reference. Looking up the errors that the command window gives you can give more insight to the errors. Being able to Google means you can learn to code without necessarily needing to lug a textbook around. Online forums, such as Stack Overflow, may show multiple solutions to the same problem. At first, reading the descriptions of what different functions do may seems strange, but as you become a stronger programmer, it will become easier to determine which search terms will likely return a useful result from Google and how to make sense of the documentation. If I remember how I would create an algorithm, but forget the precise syntax, a quick Google search will help me to format my idea into the correct syntax for that particular programming language.
Learning one programming language can help to learn others later. Similar to how knowing English can help with learning Spanish, and knowing English and Spanish make it much easier to learn Italian, knowing one programming language can help you learn others with relative ease. The big difference is which syntax to use. For example, if I know that I would need to assign a variable if I was programming in MATLAB, but am learning to program in R, then I can Google “assign a variable in R” and know what to write into my script or command line. I have learned that Googling how to do things is a reason why programmers commonly have a seemingly-ridiculous number of tabs open in their internet browser at once.
I think the biggest reason why people give up on learning to program is because they feel too stuck and cannot find their way to become un-stuck. Gaining some individualized attention can help you to keep moving forward.
Look through good code from your own lab with a more experienced programmer (coach)
There is no need to re-invent the wheel, but it can help to deconstruct and/or reconstruct the wheel.
If you are in a lab that uses computational techniques, chances are that it already has a repository of code. This is how programming often works: by modifying programs that others have already created. While this sounds simple, it is important to know what the code is doing and how to debug it.
Do NOT go into your lab’s repository and look at a random script. Instead, ask a more experienced programmer (I will refer to this person as a “coach”) in your lab for a simple script to look over, a script that your lab uses. A coach can be one person, or you can be coached by several people. The point of a coach is that they are not really doing the programming for you, but they are helping you to learn to produce your own programs and to debug things yourself. While this may be fairly time-consuming up front, it can save a lot of time in the long run.
Check out the simple script your coach identified for a bit on your own first, Googling things that you do not know. Then, ask your coach to look over the script together, line by line. This can be done remotely with a screen share if needed. Tell your coach what you think each line of code does. They can validate whether you are correct or not and they can see specifically where you may have gone off track in your reasoning. You can ask your coach about what common errors they find when running that code, and what may need to be modified for different experimental conditions. If your coach can give you mnemonics, stories, draw diagrams, or make analogies to help the ideas stick, that is a big plus.
Repeat this process, but with increasing levels of complexity to the code, with code that calls other functions in code. This process will knock out two birds with one stone: you will learn about how the code in your lab works in theory AND you may be able to apply this knowledge in practice to help your lab for the specific data analysis you need.
Modify a script for a particular problem in your lab
After you have some experience understanding other peoples’ code from your lab, you can try making modifications so that it fits the needs of different datasets. Chances are that you will need to debug the code for it to work perfectly. This can help you to learn about applying the debugging process.
Start building scripts
Some projects may require methods of analysis that your lab has never used before, and you may find yourself needing to write scripts. Remember that a script really is just a list of instructions. If you find yourself overwhelmed, write pseudocode: a list of instructions for what needs to be done, written in plain English. Then, think about how to make this happen using the commands and syntax of your programming language of choice, Googling as needed.
Throughout the process of learning programming, ask for rapid feedback
If you spend more than ten minutes stuck with the same problem and cannot think of a way around it, talk with a coach. Ask them to give you a hint or to let you know if you are barking up the right tree. Bugging them may help to debug you.
Some ways to ease the feedback process include taking screenshots of specific error messages, asking questions about what to keep in mind, and bringing up your own ideas of what you have tried and what the results were.
Afterword
Being able to use a computer to process large datasets is an important skill to hone, especially in the information age we live in. Programming can be very rewarding when you understand it well. By applying these methods, you can learn to code efficiently. In summary, the steps are:
- Find at least one coach who is more experienced than you in programming, preferably in the same lab.
- Talk through each line of a simple script with a coach.
- Repeat step 2, with increasing levels of complexity.
- Start modifying code that already exists.
- Start building new scripts.
My fellow lab scientists, you can get with the program!
The views expressed in this article are solely those of the author and do not necessarily reflect those of the OmarLab, University of Michigan, or Intvo. The electrophysiological trace image is a screenshot taken during an openEphys recording session.