Monday, July 27, 2015

Coding Responsibly Part III: Testing Your Code

Keep practicing, and code responsibly! :)
Earlier this week I was reading about code testing and control, and I thought this topic would also be a great addition to the 'Coding Responsibly' series here on Prophage. Because many other people have written about the topic, I am going to offer some of my own perspectives and experiences to the discussion. The goal here is for us to simply become better programmers.

We are not perfect programmers, no matter how experienced we are. We all make mistakes as we write our code, and these mistakes can unfortunately result in incorrect formats, wrong answers, etc. Catching these problems is particularly important in research, and in my field of biomedical research.

If you are a researcher, you know that we would never run experiments without controls. Using PCR (targeted DNA fragment application) as an example, we always run negative controls to ensure our result is above background. Likewise, we always run positive controls to ensure an absence of signal is not due to reagent failure instead of the actual biology of interest. These controls allow us to conduct useful and responsible experiments. But how do run controls on our code?

The simplest way to test it out, and the way I find pretty handy most of the time, is to feed your program a small sample dataset and check to make sure the output is exactly what you expect. Check the output format as well as the values to make sure the math is correct. Also look at other factors to make sure the output data makes sense. For instance, if you know how long some output sequences should be, check the lengths and make sure they are what you expect.

This method is a simple form of unit testing, which is the procedure of testing source code units for errors or bugs. Most programming languages offer more sophisticated unit testing programs, but we are not going to go into them here. The main goal is for us to get used to testing our code, and then we can move on to the more sophisticated methods.

Another great way to test your code is to get other people to use it and check it out. As an example, our lab writes a lot of code that can be useful for other lab members' projects, so we share our code through a lab GitHub account. This way, not only do we get to help each other to do better research more efficiently, but we also find bugs in each others programs. This is great because there will always be circumstances that we did not predict, test for, and write answers for. Pointing these issues out allows us to consistently improve our code as well as our tests. One efficient way to point out code issues is to use the GitHub issue tracking system, which is definitely worth checking out.

Following these few pointers is sure to get you started on your journey toward responsible coding. Simply check your code with test input data, and share it with others who can check it out, especially at work. If you have any other suggestions for good code testing practices, or have a comment or question, please don't hesitate to let me know in the comment section.




  1. Thank you for sharing steps for responsible coding. Please elaborate on the static code review testing, will wait for next blog.

  2. Thanks for the suggestion! :)

    I don't have that in the works right now but I can add it to the list.