Some magic Lutz code:
For a while now I have been unconsciously squirming in my seat when reading over code like this. This morning I became aware of that discomfort and cried in alarm: magic numbers!Code: Select all
(error-event (fn () (if (= 4 ((last-error) 0)) (println "ERR:" (last (last-error 4))) (println ((last-error) 1)))))
To the trained webber this might read transparently lucid; succinct even. But to the novice seeing this for the first time or the junior revisiting six month (week? day?!) old code, these opaque little numbers muddy the cognitive stream, making it nearly impossible to reason about the code until the manual has been consulted.
Oh, Horrors
So, I played with it...
It turns out that: (last-error) returns the last error -- an ordered pair of [ error-number , error-string ], where ((last-error) 0) and ((last-error) 1) retrieve the number-only and string-only, respectively.
Ok. That's not too bad. Two things to learn there and you're set for life:
1. newLISP's awesome implicit list indexing;
2. (last-error) returns such an ordered pair
fine, but what about those 4's...?
Code: Select all
(if (= 4 ((last-error) 0))
But, what is error 4?
Another quick test:
Code: Select all
(last-error 4)
Code: Select all
(4 "problem accessing file")
While the solution to the mystical zero and one numbers was trivial, the same approach will not work here -- it is not plausible to memorise the table of errors.
Solutions?
1. Have the Error Codes handy;
2. Have constants representing error numbers:
Code: Select all
(last-error ERR_FILE_ACCESS)
Code: Select all
(last-error EFACC)
tl;dr
What is the recommended way to reduce magic numbers in our newLISP code?