This is a rant post. About tech stuff. Angry. You've been warned.
One of the things that people don't seem to understand in programming is exceptions. Some you need to deal with. Some you need to fail as soon as you can and let the issue bubble up the stack until some over all wrapper code deals with it.
For example, if you're testing input from a user to make sure that it's a number, you might try to convert it to the number and if it doesn't work, you can assume that what they entered isn't a proper number. Simple.
Other things like the disk being gone, the db down (for a db driven app), or, you know, your language not understanding utf-8: Fail early. Fail fast. I flagged something in a code review today and it turned into a 15 minute discussion / argument for something that was the least of the problems with the code.
The code in question was along the following lines by doing some processing on a string:
try {
byte[] utf8Bytes = myString.getBytes("UTF8");
}
catch (Exception e) {
// just return the original
return myString;
}
Now, one the code was littered with "catch exception" everywhere, which just drives me batty, but that is a different rant. My issue was that if java you are using doesn't understand utf-8, then perhaps that would be an exceptional case. Rather than trying to deal with that issue or letting UnsupportedEncodingException bubble up, just wrap the exception in a RuntimeException and be done with it.
When an exceptional case happens, let the exception happen.
People writing code to protect against a future case and things that might happen one day drive me insane. And not the good insane either. Oh no.
*sigh* I think that there might be a reason why they put us in small rooms with padded walls at work.
No comments:
Post a Comment