Hacker News new | ask | show | jobs
by randomdata 1329 days ago
> And personally, I've implemented a Must function in many services. E.g. you try to parse a config file during service startup and parsing fails.

regexp.MustCompile exists to catch programmer mistakes. You've presumably done your testing and when you ship the software you believe the regexp is correct and it should be impossible to fail, so if it still fails, something exceptional has happened; an exception as we call them in the biz.

What you are doing when reading a config file is dealing with user mistakes. These are not exceptional, they are very much expected to happen and when they do you would traditionally want to provide useful feedback to the user, not simply crash.

This may be something that people do, but it isn't what you'd want them to do. panic/recover are intended for exceptions, not errors. Why make it easier for developers to do silly things? In the rare cases where you actually have exceptions to deal with, a little extra work to make the situation clear to the reader is okay.

1 comments

Your distinction between user and programmer doesn’t make sense to me. I typed the regex and I typed the config file. Why is a regex syntax error different than a YAML syntax error? Both are irrecoverable errors for my programs.
The difference is that the regexp is embedded in your code as part of your code and won't change after you've shipped your code. It can only panic if your code is flawed. If the compiler was smarter, it'd be a compiler error. It only ends up a runtime concern because the compiler isn't smart enough to interpret your regular expressions to find your mistakes.

If you were loading the regexp from an alterable file at runtime, to allow the user to change it after the program is shipped, you wouldn't use regexp.MustCompile. You would use regexp.Compile and gracefully deal with any errors that the user may have made.

If you are talking about compile-time configuration, where once your program is built it won't change, why would you use YAML when you could simply use language-native variables/constants with actual compile-time safety? Regexps can improve on some programming problems over writing the equivalent code, so it's a good tradeoff in some cases. YAML is never nicer than the equivalent code.

This config file is likely mounted in container, possibly different on different environemnts - ie. not "embedded", not "known at compile time".
And so failure is a user mistake, not a programmer mistake, and should be handled as such. The programmer and the user being the same person is irrelevant.
The difference is that the config is loaded at bootstrap stage - if it fails, starting service fails, there is not much to handle other than crash and supervisor (ie. k8s or whatever) will try to start it again.
You would likely exit. Crashing on invalid user input would be nonsensical.