Code Review – Bound Parameter


There is a piece of bad-smelling code, I happen to read today. The following aims to compute the angle between two line vectors.

how-to-test-bad-smell-code Code Review - Bound Parameter c # code review

There is a piece of bad-smelling code, I happen to read today. The following aims to compute the angle between two line vectors.

This code has been in the code repro for a long time, and it is brought to attention because of a failing unit test, where the input to the Math.acos function is bigger but very close to 1. Math.acos function returns Nan in this case.

Easy verification in Javascript:

js-math-acos Code Review - Bound Parameter c # code review

js-math-acos

1
Math.acos(Math.max(Math.min(1, x), -1));
Math.acos(Math.max(Math.min(1, x), -1));

It is not elegant, but it works. If the precision is a high priority, we can create a class that stores the parts of the floating number (integer and fraction) using integer type. The integers do not lose precision and we need to implement the arithmetic operations e.g. how to carry over the values.

We also have the Math.Net library which provides a rich set of library regarding to Math vectors so this is totally re-inventing the wheel, however if this is a sandbox code, you may also consider operator overloading the vector type. The above code is easily error-prone because it is easy to mess up with the array indices.

You may also like: 代码审核之限制参数范围

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
393 words
Last Post: The profiler told me I wrote some useless code (An Example of Defensive Programming)
Next Post: How to Use BrainFuck to Protect Your Steem Wallet Password(s) ?

The Permanent URL is: Code Review – Bound Parameter

Leave a Reply