The clock problem - to if or not to if?
- by trejder
Let's say, we have a simple digital clock. To "power" it, we use a routine executed every second. We update seconds part in it. But, what about minutes and hours part?
What is better / more professional / offers better performance:
Ignore all checking and update hour, minute and seconds part each time, every second.
Use if + a variable for checking, if 60 (or 3600) seconds passed and update minute / hour part only at that precise moments.
This leads us to a question, what is better -- unnecessary drawings (first approach) or extra ifs?
I've just spotted a Javascript digital clock, one of millions similar on one of billions pages. And I noticed that all three parts (hours, minutes and seconds) are updated every second, though first changes its value only once per 3600 seconds and second once per 60 seconds.
I'm not to experienced developer, so I might me wrong. But everything, what I've learnt up until now, tells me, that if are far better then executing drawing / refreshing sequences only to draw the same content.