Monday, August 04, 2025

My most frequent type of bug

The OP told me about a problem in the management program, that it wasn't calculating bonuses correctly. Each psychologist gets paid a certain amount per interview, but in order to encourage them, the OP defined that if they carry out more than a certain number of interviews in a given month, they'll receive a bonus for each interview. There can be defined more than one level of bonus, eg if a psychologist carries out more than 10 interviews, then there will be a 15 NIS bonus per interview (for all, not only for those after the tenth), and if she does more than 15, then the bonus will be 45 NIS from the first interview.

I worked on this quite heavily a few months ago and got certain things straightened out; I won't go into this now. The OP said that although the psychologists were getting a bonus, they weren't getting the correct bonus.

I started debugging the code, watching how much a certain psychologist would get as a bonus. Apart from moving one statement out of the loop that updates the interviews (a loop invariant), I didn't change anything. The code worked perfectly. I then tried it out for all the psychologists; the wrong amount was being added to the basic price. Again, I checked for one psychologist (a different one) and the code worked. For everybody, the code did not work.

I decided to run the code for everybody under the debugger, checking the size of the bonus for each psychologist. The first received 15 NIS (ok), the second 15 NIS, the third 15 NIS as did the fourth. At this stage, it began to become clear what the problem was. There is a query that obtains from the bonuses table the correct bonus amount for a psychologist during a period time for a given number of interviews; this query was returning the correct value for the first psychologist but not for the others.

This seems to be the most frequent type of programming mistake that I make: I pass parameters to a query, open it, get values back ... and then forget to close the query. As a result, the next time that parameters are passed, they get ignored because the query is still open. One might say that this is a bug with the query component, but to be honest, it's my fault that I forget to close the queries.

Once I made this small but important correction, the program calculated correctly the bonuses for all the psychologists.

Just to show that I'm not the only one who has 'senior moments', the OP texted me asking why she couldn't update one of the rows in the bonuses table. She was trying to define for one of the psychologists a period that ended on 31/06/25, and she couldn't understand why the SQL engine kept on refusing her edit. I asked her how many days there are in June and hinted that it's not 31.



This day in blog history:

Blog #Date TitleTags
27404/08/2010The in-basket 6In-basket
96604/08/2016UpdatesPersonal, DCI Banks, Fotheringay, John Le Carre, Police procedurals
141104/08/2021Third Covid-19 vaccine shotTrains, Covid-19
152304/08/2022My first year at Bristol Grammar School (1967-8), along with memories of sportsPersonal, Bristol Grammar School

No comments: