CHAPTER 31Writing Robust Code

INTRODUCTION

This chapter addresses some key topics involved in writing code that is robust in the sense that it:

  • Is fairly general and useable in a reasonably wide set of similar circumstances.
  • Is transparent and clear.
  • Is reasonably computationally efficient.
  • Is free from errors, at least when applied to its core uses, and its limits of validity are documented.

KEY PRINCIPLES

The remainder of this chapter covers a range of topics that help to ensure that code is robust.

From the Specific to the General

Typically, it can be helpful to start the code-writing process by first creating some simple code that works in a specific circumstance, and to generalise it so that it is more flexible and applies to a wider range of situations. Examples of the approaches to generalise code typically include:

  • Adapting recorded code so that it works in a wider range of cases, beyond the specific case and environment in which it was recorded.
  • Ensuring that the code still works if the input data is changed in some way, such as where there are more (or less) rows of data than in the original context, or where the position of the data within the Excel worksheet is different.
  • Detecting whether time-series data that are input from another source have the date-axis in ascending or descending order, and running the appropriate code to ensure the correct treatment in each case.
  • Building error-checking or error-handling procedures, for example as to whether the input ...

Get Principles of Financial Modelling now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.