I had a “getLineTotalForSummary” function which returned subtotal, normally used inside a loop. (Basically a user can add multiple line items to an invoice and this function returns the total for each line taking into account few other values).
The function worked fine with dummy data but as we put the application on dev and started to do real testing we realized that something was off with the totals across the app.
The function was being used by 100 other functions. Pretty typical of functions like these.
I looked throughout the code but couldn’t figure out why totals were off. Good thing that the bug was consistent.
As I started testing each function and loaded them with really random values from unit tests, I figured what was wrong.
The function was suppose to return FLOAT values.
But its return type was set as INT, it kept converting FLOAT to a whole number.
This means 20.5 became 20!
The fix was simple but it caused a lot of chaos.
I think the lesson here is that you have to be very careful about the return type you set, if you’re unsure, don’t set the wrong type because you WILL forget to fix it later. I guess this is someones an advantage when using languages like PHP that you don’t have to set the return type.
It’ll come back and bite your at some point in future.
Leave a Reply