I'm struggling with a mindset where I see every runtime exception as a major failure. To counteract this, I've gone overboard with static safety, trying to ensure that every possible error gets caught at compile-time. Right now, I'm working on a unit conversion system and have avoided using strings or enums because they seem too risky. Instead, I've created an elaborate system of static classes and nested generics, leading to complex code that is hard to read, even though it's technically safe. My concern is that if I pivot to a simpler, more dynamic approach, I might miss errors that could occur. How do I determine the right balance between writing simple code and ensuring safety?
6 Answers
It really depends on the system you're building. If you're working on something like missile guidance software, collaborating with a team is crucial, and everything needs careful review. But if this project isn’t life-or-death, maybe it's okay to relax a bit. Write comprehensive unit tests and include a cautionary note like 'not for use in critical systems.' After that, just step back and let it be!
You should definitely check out Elixir or Erlang's philosophy of "Let it Crash"—the idea is to let a runtime error happen and then just restart the process. It works well with lighter weight threads, so it might not apply to all languages but worth considering!
It sounds like you want to really use the static type system effectively. In statically typed languages, you expect mismatched types to be flagged at compile time. It's good that you're trying to avoid stringly typed programs. Embrace the idea of ‘parse, don’t validate’ and keep refining your type checks!
You might want to try writing some code that purposely throws a runtime exception just to see how it feels. I had a similar experience with Rust when I had to start using `unsafe`, which felt taboo at first. But honestly, the compiler didn't judge me when I tried it out. If you're really worried about safety, consider isolating that unsafe logic in one part of your code and thoroughly test it. Forbidding strings is probably a bit extreme, though!
I get why you're avoiding strings, but enums can work perfectly here with way less typing overhead. They’re designed for a reason!
The choice of programming language can really affect how much you can statically prevent issues. Some languages are better suited for making invalid states unrepresentable. If you're into this concept, I recommend checking out this paper on Typeful Programming: www.lucacardelli.name/Papers/TypefulProg.pdf

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically