Program Developing and Testing


Program Developing and Testing

matlab



Errors and debugging

The editor gives indications about possible errors (red) and warnings (yellow) on the right had side next to the scroll bar. However there are different types of errors, to explain them we use the following example:

clear
a = input( 'Enter a: ' );
b = input( 'Enter b: ' );
c = input( 'Enter c: ' );
r = b * b – 4 * ac;
x1 = (-b+sqrt(r))/(2*a)
x2 = (-b-sqrt(r))/(2*a)
  • Syntax Error: The typed code breaks the rules on how the language should be written. E.g in r = b*b - 4 ac there is no multiplication sign, *, between 4 and ac.
  • Run-time Error: Harder to spot as in r = b*b - 4 * ac the syntax is ok. Even though the editor will not show a syntax error, the code will fail at run time. This is due to the fact that it believes ac to be a variable and it is not defined. Therefore we still need to add a * between the ac in the example to fix this problem.
  • Logic Error: This is the hardest to find as both the syntax is ok and there is no error when the code is run, but it does not give the correct results. It is often due to incorrect implementation of a formula or algorithm. For example, if the coder forgets about operator precedence.

Validating input arguments

When a function is written, particular kinds of data for the arguments is often expected. We can use the inbuilt error function when what the input given makes it impossible to proceed. This call to error can be set to give a message to the program-user before exiting and halting the program immediately. For example, a function which estimates the expected weight for a baby whose age is given in months:

function W = expectedWeight(ageInMonths)
% Usage:
% ... usage text goes here ...
if (ageInMonths < 0)
    error('Age cannot be negative')
end
% ... rest of function continues below ...

Another inbuilt function known as warning can be used when it allows the user to proceed with the input but the results may be unreliable. With call to warning, the message is reported to the user but it does not exit the program but continues. E.g:

function W = expectedWeight(ageInMonths)
% Usage:
% ... usage text goes here ...
if (ageInMonths > 60)
    warning('Really a baby??!')
end
% ... rest of function continues below ...

Incremental Development

When writing a program to solve a problem or model a system etc. Adopt the following steps for reliable code with fewer errors:

  1. Start with a small amount of code
  2. Test to make sure code behaves as expected
  3. Add a small amount of code
  4. Test again
  5. Repeat steps 3 and 4 until completion

The aim of this method is to always have a working version of the program. Even if it doesn’t yet do everything it is supposed to do. It helps to avoid trying to write everything in one go, and it tests the program repeatedly and often.

Tips for better code and less errors

  • Always validate the input
  • Step through the program with the debugging tool and check the values of variables as the program steps through line by line.
  • Use the increment/test approach.
  • Always seek to have working code
  • Many small functions are better than one large function.
  • Use a second script/function that calls the main program and tests it.
  • The early versions can be non-interactive working with fixed data values passed in as arguments or ‘hard-coded’ in the main function. The results for these can be pre-calculated and checked for correctness at each test. The later versions can then introduce interaction.


return  link
Written by Tobias Whetton