Take the 2-minute tour ×
Computational Science Stack Exchange is a question and answer site for scientists using computers to solve scientific problems. It's 100% free, no registration required.

I'm entering my 3rd year of my PhD program, and up until now my research code (numerical inverse problems/medical imaging/image processing/etc) consists mostly of disorganized MATLAB scripts and functions, with a few MEX'd C++ and CUDA routines thrown in for some added speed here and there. Working with MATLAB has been (mostly) enjoyable, and I probably won't give it up completely, however:

This summer, I've been working on a 'big' code at a national lab, and I'm starting to see the beauty in a well oiled, object oriented, version controlled code with a good use of gmake and other computer science toys.

My question is this: to what extent should I try to incorporate these tools into my research code? How much time should I spend "planning" the structure and implementation of my code, or should I stop thinking about it and just write good routines? I feel as though I should be developing a well planned open source code base as a product of my dissertation, for the experience and CV cred, but I'm not sure how to navigate this process. Any tips, book/article/website recommendations, etc?

share|improve this question
1  
You could add your research code into an appropriate existing project. –  k20 Aug 14 at 19:41
1  
There's a lot of thoughtful points in the answers to this question on the Academia.SE, which might be useful. –  Christian Clason Aug 14 at 20:37
1  
Version control is great for even for single-person research code. Need to reproduce something you ran for a paper in 2012, with the exact bugs/lack of improvement that you had back then? Fetch the appropriate commit from git and run it. –  Peteris Aug 15 at 11:13

2 Answers 2

I'd consider these questions:

  1. Do you want your code to become a fairly general purpose code that you can reuse later, or is this just a bit of code that you're using for one research project or paper that you don't plan to reuse? If you have no plans to reuse the code, then it's probably not worth spending time and effort improving on the code.

  2. Have you learned enough about the algorithms and data structures that you're using that you have confidence that you'll want to keep using these algorithms and data structures? If not, then it might be premature to freeze your research into a stable code.

  3. Do you want to share the code, get others to make use of the code, and ultimately to get other people help develop the code further? As a researcher, you should understand that this is one of the most effective ways of making sure that your code has an impact on your field of research. However, to get this started you have to supply a reasonably robust code and you'll want to release it under an open source license so that others can make use of the code in their own projects.

share|improve this answer

My question is this: to what extent should I try to incorporate these tools into my research code?

Only as much as you feel will pay off for whatever you're trying to do. If you're mostly doing MATLAB scripts, version control and maybe unit testing are going to be all you need. If you have MEX files, it's probably good to have a Makefile that compiles them, if only for your own sanity, since typing in sequences of compile commands each time you want to build the MEX files is fragile.

Part of the reason these workflow tools get used and incorporated into large projects is because the investment in these tools pays off for their goals. As an example, for one-off research code supporting your thesis, it's probably not worth using a cross-platform build system like CMake. For a library whose stated purpose is to be cross-platform compatible, like, say, Elemental, it makes more sense to invest time in using a cross-platform build system.

How much time should I spend "planning" the structure and implementation of my code, or should I stop thinking about it and just write good routines?

It depends on how familiar you are with the problem you're trying to solve, appropriate algorithms, data structures, programming practices, and so on. Much like outlining and prewriting are helpful for journal articles, a certain amount of brainstorming and pseudocoding is helpful for writing well-structured code. I like the Pseudocode Programming Process in Code Complete, by Steve McConnell; he also includes some references and guidelines in terms of how much time should be spent on the design phase, depending on the type of project.

Writing throwaway versions of routines and experimenting with short bits of code is also really helpful. A common aphorism in software development is that you'll always throw away at least one version of your code.

I tend to believe that "agile" development practices tend to work best with most scientific software development, based on Greg Wilson's work at Software Carpentry (disclaimer: I have volunteered with them in the past). Broadly, "agile" means you should set goals you think you'll reach in a short period of time (say, a couple days, a week, maybe a month at most), plan out how to reach those goals by doing some pseudocoding and designing, and then write code, and repeat. Short cycles will help you react to changes, such as when your adviser decides that he wants you to extend your work in ways you haven't anticipated.

I feel as though I should be developing a well planned open source code base as a product of my dissertation, for the experience and CV cred, but I'm not sure how to navigate this process. Any tips, book/article/website recommendations, etc?

That all depends on what you want to do. For positions involving software development, developing an open source code base is helpful, because it's something you can post on GitHub and point to. That said, if you want to make it a software package people will use, you're going to have to spend some time maintaining it; you may not want to do that. Contributing your research code to relevant existing projects can also be a really good option. Companies seem to want a mix of both. If you can contribute to other people's code, it shows you're a team player and that you can read other people's code and still do something useful with it.

In terms of references, Software Carpentry's reference reading list is well-geared towards scientists, and if you want to dig deeper into software engineering practice, Code Complete (see the list in the previous link) has further references that are starting to become a little dated, but are useful to look up. The papers they put out on best practices in scientific computing are also helpful

Software Carpentry's lessons are helpful, too. They're Python-centric when it comes to programming, so you might take that with a grain of salt, but the version control parts are worth looking at.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.