Best Practices for Scientific Computing

In lieu of the holiday season, Matlab Geeks is going to take a small break from writing Matlab code. Instead, we’d like to share this great article on the best practices for scientific computing. Increasingly, scientists are writing computer programs to perform their research. However, most scientists only have rudimentary training in computer programming and do not know how to create efficient, reliable, and maintainable code. This brief article lists ten recommendations that can help increase the productivity of scientists and engineers. We have summarized the main points of the paper in this article. We try to use these recommendations ourselves and believe the dissemination of this knowledge will help the scientific computing community become more productive.

Original article

1. Write programs for people, not computers. Ensure that your code is easy to read. Humans are only able to hold a few facts in their minds at one time, so try to create code that is as easy to read and simple as possible. This can be accomplished by writing code that is modular, unambiguous, and intuitive.

2. Automate repetitive tasks. Try to remove all human interaction from the testing loop. Automate as much of a task as possible to save time. Try to reduce human interaction with the program to the point where only a single command is necessary to run a single test or a group of tests.

3. Use the computer to record history. Record a large amount of information about your program, data, and parameters. Enough information should be stored in your records to enable the reproduction of any past event that your program has performed. Record version numbers, dates, and output.

4. Make incremental changes. Work in small steps with constant feedback and course correction. Think ahead, but do not commit yourself to large, intricate plans. Adapt to new information and be willing to change directions. This is also known as “agile development,” which is a popular paradigm.

5. Use version control. Instead of exchanging code with your collaborators through email, use a version control system such as Git or SVN to maintain a central repository of your code. Doing so increases efficiency and reduces errors. A version control system allows users to check out code and handle merging of different versions.

6. Don’t repeat yourself (or others). At small scales, only keep one copy of your data and code. Write your code modularly and create libraries that multiple programs can access instead of copying and pasting your code into several different locations. This has been shown to reduce errors and save time. At large scales, use libraries that others have written instead of writing your own.

7. Plan for mistakes. Use defensive programming, write and run tests of your software, turn bugs into test cases, and use a symbolic debugger.

8. Optimize software only after it works properly. Do not obsess about speed and efficiency in the first draft of your program. Write your program in a way that is understandable and then use a profiler to discover the performance bottlenecks. Additionally, for large projects that you know must eventually have high performance, it is faster to prototype your code in a higher-level language, such as Matlab, than to write the original code in a lower-level language, such as C.

9. Document the design and purpose of code rather than its mechanics. Explain the interfaces, purpose, and overall design of your code, rather than the details of the implementation. Create variable and function names that are readable and self-explanatory instead of writing a comment on every line. Additionally, embed your documentation in the code itself.

10. Conduct code reviews. Have your software team review your code and check for errors before you commit it to the repository. This helps disseminate information and ensures that your code is understandable to others. Teams should also use an issue tracking tool, which will reduce the amount of duplicated work, keep track of progress, and prioritize tasks.

One thought on “Best Practices for Scientific Computing

  1. Pingback: Matlab Best Practices - Kyle Bradbury

Leave a Reply

Your email address will not be published. Required fields are marked *