I just started to learn programming, going through a lot of different tutorials, trying out different programming languages and I stumble in the same sort of questions all over the place.
I give you an simple egg timer in bash I use for cooking.
#!/bin/bash ### varibales min=$1 sec=$2 message=$3 ### run timer=$(($min * 60 + $sec)) for i in $(seq 0 $timer) do remain=$(( $timer - $i )) echo -ne " $(($remain / 60)):$(($remain % 60)) \r" sleep 1 done echo -ne " \r" echo "$message" afplay ~/alarm.mp3
This egg timer is very simple and therefore usually reliable. I use it for cooking. Yet, when I imagine doing it on a grander scale, there would be so many questions, that are neglectable in such a small application. But I am not sure if they would stay neglectable.
Is it a good idea to keep the additional variable
timerin there? It helps readability, but wouldn't that hold another variable in my RAM? Shouldn't I better forget about readability and directly calculate it in the
echocommand. Same goes with the variables set for
message. Those variables are basically doubled for readability alone. I could directly use them in the script.
sleepto count down time. As far as I know, things like
pausecan be off when the CPU is under heavy usage. Something probably to consider for scripts on my old raspberry, that only has one core. Shouldn't I first calculate the time of the alarm and then use my system time to calculate the time left? Should it do that in every iteration of the loop, that has to comes at least every second or should I only do it every minute or hour?
If I check it with system time, should I rather put the checks in interlapping loops than add an if check? Feels like an interlapping loop would take less processing power than use if checks every time it runs.
While we're on the question of waiting. How do I find good timings for programs? I mean, let's assume my program needs to check something regularily. Like, a file on the computer. How do I find a good timing for it? Every .5 second would feel pretty responsive. Yet, am I clogging my CPU with unnecessary checks? Is there some rule of thumb, like check 10 times more often for things on your RAM instead of things on your HD?
Like I said, those programs won't very much suffer from such things, but I wonder right now if I build on a wrong premise when trying to learn to code without considering writing effective code and are there any materials that you can point me to that answers these sorts of questions and hopefully a lot more questions I haven't thought of yet?