Skip to content
  • Data Analysis
  • Objected-Oriented Programming
  • Best practices

Redirecting to exemplar docs...

From Spaghetti to Structure: Writing Approachable Code

A codebase rarely starts messy. You solve a problem, a colleague solves the same problem slightly differently, and then a year later there are two codebases doing the same thing, implemented differently and with loads of copy-pasting to keep them in sync.

This exemplar walks through some true examples from the biomechanics group, giving some opinionated best-practices for writing code that is more approachable. We will cover some broadly applicable software design concepts: the usage of structs, object-oriented programming, how to model real-world systems and making your program's interface how you want it.

This exemplar was developed at Imperial College London by Jonathan Holt in collaboration with Daniel Cummins from Research Software Engineering and Jay DesLauriers from Research Computing & Data Science at the Early Career Researcher Institute.

Learning Outcomes 🎓

After completing this exemplar, students will:

  • be able to model systems in the real-world with appropriate data structures
  • understand why and when to use structures and classes to organise data
  • reflect on how they want to interact with software they write

Target Audience 🎯

Researchers finding themselves maintaining multiple near-identical codebases or struggling to re-use code. Examples are provided in MATLAB but the concepts are applicable to any programming language.

Prerequisites ✅

Academic 📚

  • The concepts are language agnostic, but basic knowledge of MATLAB is helpful. If you have written code before, any particularities of MATLAB should be understandable.
  • Some familiarity with reading data files (e.g. .csv)

System 💻

  • Written using MATLAB 2025a. Older versions have not been tested.

Getting Started 🚀

  1. Start by reading the introduction to understand the problem we're solving and why it matters.
  2. The examples in Structures introduce classes in the simple context of a game before anything domain-specific appears.
  3. From then onwards, these ideas are applied to real code from the lab.

Disciplinary Background 🔬

The code in this exemplar comes from a biomechanics lab that uses optical tracking cameras and robots to study how joints move, but the domain knowledge itself isn't the point.

If you've ever worked with instrument data that comes out in slightly different formats depending on the device, the problems we are solving may feel familiar.

Estimated Time ⏳

Section Time
Introduction 30 min
Structures 30 - 60 min
Creating an Experiment 30 - 60 min
Creating a Generic Camera 60 min
Designing an API 30 - 60 min
Total 3 - 5 hours

Licence 📄

This project is licensed under the BSD-3-Clause license.