On this page
Level 11 · Advanced Research Methodology
Scientific Programming
The practice of writing software for scientific analysis that is correct, readable and maintainable, covering the structuring of a project into modules with clear inputs and outputs, the writing of tests that check functions against known answers, the handling of numerical precision and the pitfalls of floating-point arithmetic, the documentation of code so that its assumptions and units are explicit, the use of code review and continuous integration to catch errors early, the choice of languages and libraries suited to the analysis, and the recognition that code in research is an output that
1 min read · Updated
Learning objectives
- Structure an analysis project with modules that have explicit inputs, outputs and tests against known answers.
- Identify the numerical and unit errors that commonly corrupt scientific code and state how to detect them.
Scientific software is a research output in its own right, and its errors can change conclusions as surely as errors in the data. Peng (2011) argued that code used in published analyses should be held to the standard of the analysis it supports, since a bug in a single function can propagate into every figure. The practice of scientific programming therefore combines the habits of software engineering with an understanding of the statistical and numerical questions that the code must answer.
A well-structured project divides the analysis into modules, each with clear inputs, outputs and a single purpose. Such a structure makes the code easier to read, to test and to change, and it reveals the dependencies between steps. Sandve et al. (2013) recommended that the structure be documented so that another person can follow the path from raw data to result without reading every line.
Tests check that functions produce known answers for known inputs. A test might compare a statistical routine against a value computed by hand, or check that a function rejects an input of the wrong units. Tests catch errors when code changes, and they document the expected behaviour. Ioannidis (2005) described how undetected errors in analysis contribute to unreliable findings, and tests are a practical defence against them.
Floating-point arithmetic introduces rounding errors, and comparisons of computed values for exact equality are often wrong. Units are a common source of silent error, since a value in millimetres treated as metres changes the result by a factor of a thousand. The code should make units explicit and check them where they are combined. Peng (2011) emphasised that these details belong to the methods of a study, and they should be documented in the code and in the report.
Chapters in this module
- Project Structure
The organisation of a scientific programming project so that its data, code, outputs and documentation are separated and its steps are traceable, covering the separation of raw data, processing scripts, analysis functions and generated outputs into distinct directories, the use of a single entry point that runs the pipeline in order, the naming of files so that their purpose and their version are clear, the writing of a README that states the inputs, the outputs and the commands to reproduce them, the keeping of generated outputs out of the source tree so that they can be regenerated, and the
- Testing Scientific Code
The testing of scientific code to establish that its functions produce correct results, covering unit tests that compare a function with a known answer computed by hand or by an independent method, tests of edge cases such as empty inputs, single values and extreme magnitudes, property-based tests that check invariants such as the conservation of totals or the monotonicity of a relation, regression tests that record a known output and flag any change, the use of simulated data with a known parameter to test whether an estimator recovers that parameter, and the reporting of test coverage and of
- Numerical Precision and Units
The numerical pitfalls and unit errors that corrupt scientific computation, covering the representation of real numbers in floating-point arithmetic and the consequent rounding errors, the danger of comparing computed values for exact equality and the use of tolerances instead, the loss of precision when subtracting nearly equal numbers and when summing many small values, the overflow and underflow of very large and very small quantities, the explicit declaration of units in variable names or in a units library, the conversion of units before combination, and the checks that detect dimensional
- Documentation and Code Review
The documentation of scientific code and the review of code by a second person, covering the documentation of the purpose of each function, its inputs, outputs, units and assumptions, the recording of the reasons for non-obvious choices in comments and in the project log, the use of code review to find errors that the author cannot see, the checklist used in review that asks whether the code matches the method described in the report, whether the tests cover the cases that matter and whether the parameters are justified, the use of pull requests or equivalent records of each review, and the tr
- Version Control in Practice
The day-to-day practice of version control for scientific code, covering the initialisation of a repository and the decision of what to track and what to ignore, the commit of small, coherent changes each with a message that states the reason, the use of branches to try an alternative analysis without disturbing the main line, the merging of branches with review, the recovery of earlier states to reproduce a past result, the avoidance of committing large data files and secrets to the repository, and the tagging of the state of the code that corresponds to each manuscript version.
- Languages and Libraries
The choice of programming languages and libraries for scientific analysis, covering the criteria of suitability for the data types and the analysis required, the maturity and the testing of a library, its licence and its maintenance, the reliance on established statistical routines rather than reimplementation of standard methods, the pinning of library versions so that an analysis runs identically later, the trade-off between performance and readability, the interoperability of tools that allows an analysis to pass data between stages, and the reporting of the software used with its version s
- Performance and Scale
The management of computational performance and scale in scientific analyses, covering the profiling of code to find the parts that consume most time or memory before any optimisation is attempted, the choice of algorithms whose cost grows acceptably with the size of the data, the processing of data in chunks that do not require the whole dataset in memory, the parallelisation of independent tasks across processors, the verification that an optimised version gives the same result as the original, and the recording of the hardware and the timings so that the performance claims of the analysis c
- Code as a Research Output
The treatment of research code as a formal output that must be deposited, licensed, cited and maintained, covering the deposition of the code in a public repository with a persistent identifier, the choice of an open licence that states how the code may be reused, the citation of software in the same way as a publication with its version and identifier, the documentation needed for reuse by others, the maintenance commitments and the handling of dependencies that will age, and the recognition of software as a contribution in its own right within the evaluation of research.
References
- Sandve, G. K., Nekrutenko, A., Taylor, J., & Hovig, E. (2013). Ten Simple Rules for Reproducible Computational Research. PLoS Computational Biology, 9(10), e1003285. https://doi.org/10.1371/journal.pcbi.1003285
- Peng, R. D. (2011). Reproducible Research in Computational Science. Science, 334(6060), 1226-1227. https://doi.org/10.1126/science.1213847
- Ioannidis, J. P. A. (2005). Why most published research findings are false. PLoS Medicine, 2(8), e124. https://doi.org/10.1371/journal.pmed.0020124
Callouts marked Commentary or Speculation in the lesson separate interpretation and open questions from established findings.