cromo_ · 2022-11-13 19:33
nv-elisp · 2022-11-13 20:28
A few suggestions for your example command:
When using `message` you'll want to provide the format-string argument.
Otherwise the user's strings will fail if they include format specifiers.
e.g. if your `goodvibes` list includes `"%dont mind me%"`, the `%d` will be interpreted as part of the format-string argument and will throw an error.
For an error caused by user input, it's better to use `user-error`.
This will prevent the debugger from being hit any time a user inputs an out of range number.
You can rely on `or` in simple cases like this.
Another version of your command could be:
(defun cheer-me-up (n)
(interactive "nChoose a number, take good vibes~ ")
(let ((index (1- n)))
(message "%s" (or (nth index goodvibes)
(user-error "Number out of bounds: %d > %d"
index (length goodvibes))))))
cromo_ · 2022-11-13 22:42
I totally get your point, thank you for your comment. I will update the post accordingly.
Edit: I'm going to cite your comment in my post and advising the reader to prefer `user-error` to `error`. The other changes are of course better, but I expressly avoided `let` and other keywords for sake of simplicity, so I will stick to the previous logic.
howardthegeek · 2022-11-14 15:56
Nice work. Thanks for sharing
cromo_ · 2022-11-15 18:30
Thanks, I'm glad this is appreciated :D