4 ms·
Please, everyone who thinks, "I just expect correct inputs" take note of part two. I've had so many discussions with colleagues about that "Why don't you check
by sgift 9y ago
Please, everyone who thinks, "I just expect correct inputs" take note of part two. I've had so many discussions with colleagues about that "Why don't you check for null pointers and give an error message?" "I think it's perfectly fine to just crash with a null pointer, I don't expect null pointers."
That attitude hurts everyone who has to support what you write.
edit: Reformatted as a habit "Make sure whatever you write can be supported without unnecessary headaches."
- nostrademons 9y agoOnly if a.) you can do something to recover from the null pointer or b.) you can provide more information about its cause than you could recover from an ordinary stack trace. Otherwise you're just adding code without changing the ultimate result of the NPE: a debugging session. Assertions, however, are great. If you can force the program to throw that NPE closer to the point where it actually illegitimately became null, the debugging session becomes much shorter. Null-safety in the type system (like with most modern languages: Swift, Haskell, Kotlin, Scala, Rust, etc.) is even better.
- sgift 9y agoI have almost never seen a program where you have neither a nor b, but I agree - if you are in a situation where neither a nor b apply, don't do anything. And: Using a different language is always a great idea, but often not possible.
- brlewis 9y agoThis! I especially see this problem in Java, where IDEs always suggest catching an exception. Like a dog chasing cars, you really should think through what you're going to do when you catch one. I've wasted a lot of time debugging problems that get masked over by try/catch only to pop up at some later point in a seemingly unrelated part of the code.
- pards 9y agoThis is what Martin Fowler refers to as the Tolerant Reader pattern. https://martinfowler.com/bliki/TolerantReader.html https://martinfowler.com/bliki/TolerantReader.html Code should tolerate all sorts of bad inputs. Code should produce pristine outputs. Just like being a model member of society
- vram22 9y agoI think I read somewhere that Jonathan Postel (Internet pioneer) also had a rule advising that. Googled and found it: https://en.wikipedia.org/wiki/Robustness_principle https://en.wikipedia.org/wiki/Robustness_principle https://en.wikipedia.org/wiki/Jon_Postel https://en.wikipedia.org/wiki/Jon_Postel "In his lifetime he was known as the "god[2] of the Internet" for his comprehensive influence on the medium."