What you learn today
- Repeat optimisation
- Combine debugging and robustness
- Spot gaps
The second recap combines three weeks that belong together: optimisation, debugging and robustness. A script is only good when it fits on the chip, does the right thing and stays safe on bad days too.
Optimisation
| Limit | Value | what helps |
|---|---|---|
| lines | 128 | select instead of jumps, subroutines, loops |
| characters | 4096 | short comments, game version |
| characters per line | 90 | wrap comments |
| instructions per tick | 128 | split long passes with a second yield |
The most important rule: copied code is the most expensive code. Three rooms with the same logic are one subroutine and three calls, not the same twenty lines three times.
Debugging
The housing reports loud errors with name and line. Silent errors you find with probes, test cases and the simulator – and with the knowledge of where they live: Sum instead of Average, NaN, −inf, Celsius instead of Kelvin, the forgotten j start.
Robustness
Every sensor may be missing. The only question is what happens then: detect NaN, report, stay safe. And after a restart the chip knows nothing any more – whatever it must remember lives in the device or in a Logic Memory. For a heater the device itself is enough:
lbn on HEIZUNG name On Average # the state lives in the device
snan tmp on
select on tmp 0 on # heater missing: off
That way no room needs a register of its own for its state, and a restart simply takes over whatever the heater is doing right now.
Finding gaps
Take a script from your base and go through it with this page. Does it fit on the chip with room to spare? Does it have a test case for every sensor failure? Do you know what it does after a restart? Every “don't know” is a gap – and a day you should read once more.
The exercise has exactly the three kinds of errors that occur most often in old scripts: copied blocks, Sum for temperatures and not a word about what happens without a sensor.
Exercise
Task
The old version controls two rooms with copied code and Sum. Rebuild it: three rooms “R1”, “R2”, “R3”, each with sensor(s) and heater of the same name, on below 18 °C, off above 21 °C, the mean of all sensors of a room. One subroutine for all rooms. If a room's sensor is missing, its heater is off, and the display “Raeume_Fehler” counts the rooms without a sensor.
Hints
Hint 1
Main part: faults to 0, three times move name HASH("R…") and jal raum, then the display.
Hint 2
In the subroutine Average instead of Sum, snan counts the missing sensor.
Hint 3
Read the state back from the heater, 0 on NaN. The hysteresis switches off on its own with NaN.
Quiz
3 questions. You pass from 60 % – the server does the grading.
Complete the day
- Passed the simulator exercise
- Passed the quiz with at least 60 %
Without an account progress is saved in this browser only.
Report a mistake in this lesson
Please sign in to report. Sign in