34
1 The First Few Steps
elegantly in the code, since it is possible to (put simply) try some statements,
and if they go wrong, rather run some other code lines! This way, an exception
is handled, and an unintended program stop (“crash”) is avoided. More about
exception handling in Sect. 5.2.
Testing Code When a program finally runs without error messages, it might be
tempting to think that Ah. . . , I am finished!. But no! Then comes program testing,
you need to verify that the program does the computations as planned. This is almost
an art and may take more time than to develop the program, but the program is
useless unless you have much evidence showing that the computations are correct.
Also, having a set of (automatic) tests saves huge amounts of time when you further
develop the program.
Verification Versus Validation
Verification is important, but validation is equally important. It is great if
your program can do the calculations according to the plan, but is it the right
plan? Put otherwise, you need to check that the computations run correctly
according to the formula you have chosen/derived. This is verification: doing
the things right. Thereafter, you must also check whether the formula you have
chosen/derived is the right formula for the case you are investigating. This is
validation: doing the right things.
In the present book, it is beyond scope to question how well the mathematical models describe a given phenomenon in nature or engineering, as the
answer usually involves extensive knowledge of the application area. We will
therefore limit our testing to the verification part.
1.8 Concluding Remarks
1.8.1 Programming Demands You to Be Accurate!
In this chapter, you have seen some examples of how simple things may be done
in Python. Hopefully, you have tried to do the examples on your own. If you have,
most certainly you have discovered that what you write in the code has to be very
accurate.
For example, in our program ball_plot.py, we called linspace in this way
t = np.linspace(0, 1, 1001)
If this had rather been written
t = np.linspace[0, 1, 1001)
we would have got an error message ([ was used instead of (), even if you and I
would understand the meaning perfectly well!
Remember that it is not a human that runs your code, it is a machine. Therefore,
even if the meaning of your code looks fine to a human eye, it still has to comply in
detail to the rules of the programming language. If not, you get warnings and error
Précédent

- 56/350

Suivant