You often hear stories of programmers who work best in the evening or at night, or programmers who “pulled an all-nighter”.
I take a different approach.
When I am struggling with a piece of code (a bug I cannot track down, for example) I call it a day and sleep on it. I am still amazing at how often I find that I will awaken with a solution.
They say that the following happened in dreams:
- Dmitry Mendeleyev came up with the periodic table of elements
- Dr. James Watson came up with the double helix structure for DNA .
Now I have never done anything as earth-shattering, but here are two recent examples from me.
I wrote a COVID-19 app recently and I was struggling with the HTTP Client class which I was using to interact with the API to get the data. The documentation for the class was pretty light and I could not find any sample code. The problem I was having was specifically in relation to the callback function.
I was having so many problems that I decided to try the Websocket Client class instead. I was struggling with this too.
But when I woke up the next day I was sure that I could get the HTTP Client to work without using a callback and that is exactly what I did and it worked for me.
My daughter was doing a project for her university course which involved designing the hardware and software for a networked automatic hand sanitiser dispenser. I wrote some pseudocode for the software which monitored the proximity sensors and the liquid levels.
The code had a check for thresholds on the liquid levels and could send alerts as these thresholds were passed.
I realised that there might be an issue where (due to slight fluctuations in the readings) the liquid level might seem to cross back and forth over a threshold and this would trigger multiple alerts.
Since this was just pseudocode for a project which would never by built, I decided not to worry about it.
But when I woke up the next day I realised that a simple tweak would fix this.
So now when I get a new reading, I check it see if it is greater then the previous reading. If so, I check to see if the increase is about a configurable threshold (min_increase). If it is not greater then min_increase, then I just set the new reading to be the same as the previous reading. Job done!
If you are struggling with a design or coding problem then try sleeping on the problem.