Computational Methods in Finance, Fall 2000 Comments on homework 1 READER FRIENDLY CODE. In the workplace, you will not be coding just for yourself, but in collaboration with a group. It is crucial that your code be readable, that someone can read over your code and understand it easily. Your homeworks will be graded for readability. It takes some planning to write readable code. Moreover, readable code is more likely to be correct. The person who misunderstands "spagetti code" is likely to be it's author. Make good use of indentation, white space, and comments. Avoid variable names like "temp1" or "aaa" (unless it's a bond rating). Follow conventions, such as all caps for a #defined variable. Avoid very long names, such as "NumberOfWeeksRemainingForTheLoan", especially when they are involved in arithmetic expressions or procedure calls. Avoid unnecessary pointer use and dynamic memory allocation. One program had the expression "double ****f;", and without a comment. That is, f points to a pointer that points to a pointer that points to a pointer . . .. Think of a simpler looking way to do this. Use intermediate quantities to break up large expressions. If a single line of code is more than 2 lines long, you can almost certainly make it easier to understand by breaking it up. BEWARE THE SUBLTIES OF COMPUTER ARITHMETIC. Floating point arithmetic is unlikely to be done exactly. If you use a floating point computation to get an integer (such as the number of possible interest rates), the result is likely to be off by one, as in this example: #include /* A test code to explore floating point arithmetic. */ int main() { double x, y, z; int n, m; x = 1./5.; y = x*x; z = x/y; n = (int) x/y; /* This computes (int) x then divides by y. */ m = (int) z; /* This computes (int) (x/y), as desired. */ printf("An illustration of floating point arithmetic"); printf(" and truncation to integer.\n"); printf("z = %8.4f , n = %2d , m = %2d\n", z, n, m); x = .99; n = (int) x; printf("The truncation of x = %8.4f is n = %2d.\n", x, n); return 0; /* A signal that nothing went wrong. */ } Which gave output: An illustration of floating point arithmetic and truncation to integer. z = 5.0000 , n = 0 , m = 4 The truncation of x = 0.9900 is n = 0. I strongly recommend that you avoid serious scientific computing in VBA or Matlab precisely because the types of variables are uncertain and you have no control over type conversions. BE CREATIVE AND SPEND SOME TIME FIGURING OUT HOW TO VALIDATE YOUR CODE AND ITS ANSWERS. This week's homework included many easily avoided wrong answers.