4 ms·
When reading C code either all return codes are checked or not. Either way you know whether errors are dealt with. If you don't check a pointer before dereferen
by gizmo 18y ago
When reading C code either all return codes are checked or not. Either way you know whether errors are dealt with. If you don't check a pointer before dereferencing it - the code is wrong. It's that simple. Correct code looks correct, crappy or unfinished code looks crappy or unfinished. This is a good thing.
When you read Ruby code you have no idea if the programmer thought about exceptions at all. In order to figure out what exceptions should be possibly handled you have to take the union of all the exceptions that can be thrown by all the functions you call, and then check if they are all handled correctly. Unfortunately, it's impossible to determine which exceptions can be thrown by a function, because any function can throw any number of exceptions. If your buddy decides to throw a new exception in some helper function then the exception signature of a hundred functions changes.
So basically, there's no way of telling whether the code is correct. Good luck with that. So people give up and just rescue every possible error and return a default value, excactly like described in the article.
For your enjoyment:
http://www.google.com/codesearch?q=lang%3Aruby+rescue&hl=en&btnG=Search+Code http://www.google.com/codesearch?q=lang%3Aruby+rescue&hl...
And this is production code you're looking at.
- BrandonM 18y agoIf you don't check a pointer before dereferencing it - the code is wrong. Really? This might be true after a malloc or another system call, but in general, your modules should have specifications. If you have written a function which states that the incoming value must be a pointer to some value, that pointer shoud never be checked before dereferencing it. It's the caller's job to ensure that a valid pointer is sent. Blindly checking for errors that will never come up is not much better than not checking at all.