Writing Code Someone Else Will Read
There is a widespread belief that a timed assessment is the one place where messy code is rational. Nobody is watching, the clock is running, and the grader only checks the return value. It is wrong for a practical reason rather than a moral one: the person who has to read your code first, and under the most pressure, is you at minute forty-five when test seven fails.
The reader is you, twenty minutes from now
Consider a fill calculation written the way it usually is under time pressure.
def f(o, t):
r = []
for x in o:
if x[2] == 'B' and x[1] >= t[1] and t[0] > 0:
q = min(x[3], t[0])
r.append((x[0], q, x[1]))
t = (t[0] - q, t[1])
return r
It may well be correct. But a failing hidden test gives you a wrong answer and nothing else, and now you have to find the defect in this. Is x[1] the price or the timestamp? Does t mean the incoming order or the remaining quantity? You will spend two minutes reconstructing what you wrote, and you will do it again for the next failure.
The rest of this lesson is for subscribers
Unlock every lesson in Programming for Quantitative Developers, and every other premium course.
Subscribe to continueTest your knowledge
Keep reading Programming for Quantitative Developers
25 lessons in this course, and every other premium course, on one subscription.
- Every lesson in every course, with the worked examples and interactive simulators
- Graded questions on every lesson, with explanations for the wrong answers as well as the right one
- The trainers, timed assessments and brainteaser library that go with them