Courses / Physics I
Computer Science

Debugging and validation

Physics I 207 words Free to read

Debugging Categories

Writing code that runs is not enough; it must produce correct results. The hardest bugs are silent: the code runs without errors but produces wrong answers.

Bug TypeExampleFix
SyntaxMissing colonRead error
RuntimeDivision by zeroAdd guard if x != 0
LogicOff-by-one boundsTrace small example
NumericalFloat driftSmaller step hh

Common pitfall: Fixing the crash site often leaves the root cause untouched. Trace the bad value back to where it was born; the crash is usually just the victim.

A wrong value is born quietly and only complains three steps later

Strategies and Validation

Use code structure to catch mistakes early. Print intermediate values (print(f"x={x}")), use assertions to catch invariants (assert energy > 0), and test known inputs.

Validate your work systematically:

LevelMethodPurpose
1Unit testsCheck functions
2ConservationCheck energy/momentum
3ConvergenceHalve hh to check error
4ComparisonMatch analytics

Conservation checks are your best defense against silent bugs.

Practise this lesson

The explanation above is free to read. The graded practice for this lesson lives in the Tryals app.

15practice questions
2interactive scenes

Computer Science