Why Code Rusts

or Why Tests Spontanously Fail

Originally posted; 2022-02-07T16:00:00 on the TDDA Blog.

You might think that if you write a program, and don’t change anything, then come back a day later (or a decade later) and run it with the same inputs, it would produce the same output. At their core, reference tests exist because this isn’t true, and it’s useful to find out if code you wrote in the past no longer does the same thing it used to. This post collects together some of reasons the behaviour of code changes over time.¹

The Environment Has Changed

E1
E2
E3
E4
E5
E6
E7
E8
E9
E10
deleting a file the code uses
renaming a file the code uses
editing a file the code uses
emoving or renaming a directory the code uses
changing permissions on a file or directory the code uses
creating a file or directory that the code expects to create and is now unable to, e.g. because of permissions.
E11
E12
E13
E14
E15
E15A
E16
E17
E18
E19
E20
E21
E22
E23
E24
E25
E26
E27
E28
E29
E30

Many of these are illuminated by one of my favourite quote from Beth Andres-Beck:

Mocking in unit tests makes the tests more stable because they don’t break when your code breaks.
— @bethcodes, 2020-12-29T01:26:00Z https://twitter.com/bethcodes/status/1343730015851069440

The Code Has, in Fact, Changed

C1
C2
C3
C4
C5
C6
C7
someone else had pushed a change
you checked out a different branch
you pulled from the wrong repository.
C8
C9
C10
C11
C12
C13
C14
C15
C16
C17
C18
C19
C20
C21

Also from Beth Andres-Beck:

If you have 100% test coverage and your tests use mocks, no you don’t.
— @bethcodes, 2020-12-29T01:51:00Z https://twitter.com/bethcodes/status/1343736477839020032

You Aren’t Running the Code You Think You Are

There is another set of problems that aren’t strictly causes of code rusting, but which help to explain a set of related situations every developer has probably experienced, which all fall under the general heading of you aren't running the code you think you are.

M1
M2
M3
M4
M5
M6
M7
M8
M9
M10
M11
M12
M13
M14
M15

These are the ones that make you question your sanity.

TIP If what’s happening can't be happening, trying introducing a clear syntax error or debug statement or some other change you should be able to see. Then check that it shows up as expected when you’re running your code.

Almost every time I think I'm losing my mind when coding, it's because I'm editing and running different code (or viewing results from different code).

Time has Moved On

T1 Your code has a (usually implicit) date/time dependence in it, e.g.

T2 Time is ‘bigger’ in some material way that causes a problem, e.g.

T3
T4
T5

Resources Used by the Code Have Changed

R1
R2
R3

Stochastic and Indeterminate Effects

S1
S2
S3
S4
S5
S6
S7
S8

It Never Worked (or didn’t work when you thought it did)

[Added 2024-07-19]

I realised there’s another whole class of errors of process/errors of interpretation that could lead us to think that code has “rusted” despite not having been changed. These are all broadly the same as one of the explanations offered before, but now for the original run when you thought it worked, rather than for the current or new run, when it fails.

N1
N2
N3
N4

¹ If you have think of other reasons code rusts, do let me know and I’ll be happy to expand this list (and attribute, of course)

² Touching a file (the unix touch command) updates the last update date on a file without changing its contents.

³ For this reason, a lot of people prefer to run python -m pip rather than pip, because this way you can have greater confidence that the module is getting installed in the site-packages for the version of python you’re actually running.

⁴ Most of these kinds of indeterminacy will, in fact, usually be stable given identical inputs on the same machine running the same software, but it can take very little to change that, and should not be relied upon. Why Code Rusts