- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Frist...
Admin
Looks like someone had a difficult job of getting it to work where it crashed in certain circumstances, and clearly couldn't be bothered to tidy up after him/herself after putting all the appropriate logic into doCalculation.
Admin
But why should the request data be modified?
Admin
It shouldn't be modified, but if I didn't explicitly say so, someone would have pointed out that we didn't know for sure that request.includeFooFlag() might not have changed and maybe the lack of else if was deliberate. Which is not the case.
Admin
But that only handles the true- and false-cases.
What if request.includeFooFlag() is FileNotFound?!
Admin
FileNotFoundException
, duh!Admin
Actually, "Error Details"
Admin
So, basically:
Admin
You can do better than that.
Admin
Addendum 2016-09-28 09:46: Doh,
doCalculation
has its own exception handler... /facepalmAdmin
Another example of why Java is horrible.
Admin
This definitely reads like code that either used to do different things, or where the developer just couldn't figure out what was going on and so kept adding tests until the error went away (probably due to an unrelated edit). Good times.
Admin
..or
Admin
Well, according to the article, this would suffice: return doCalculation(request);
Admin
I don't think you can blame the language here.
Admin
Or where the developer intended to have something more generic but in the end never pulled it up a layer. The outer exception case might make sense. I replace all unhandled unsafe exceptions with a wrapper exception that is safe to pass up through IO. This will be wrapped around any function called which happens in a style akin to RPC. It's natural in that case that you're going to have it even wrapping methods that don't through an exception or don't need to try catch.
I still think a failure to clean up is more likely. doCalculation having its own try catch doesn't sound good.
Admin
We won't get into the debate about where function return statements should go.
Admin
This is pretty common. You write a block of code that has two branches. Then through refactoring and general code changes, the if/else blocks do the same thing. But it never gets cleaned up. I'm sure the code several revisions ago probably looked a lot more logical. Granting catching Exception is usually a very bad thing unless you have a very good reason for it. And in that case, you should catch the specific runtime exception. Now things can go wrong but you don't know why especially without logging.
Admin
Unless
request.includeFooFlag()
will modify the result ofdoCalculation(request)
, or throw an exception, or change values, three possibilities that should not go overlooked.Admin
Obviously the project lead told the programmer to write error details into the response in case of an exception.
Anyway, these are only 24 lines of code. I've seen similar contraptions stretching over hundreds of lines.
If rollout is due the day after tomorrow, and you know there's still a bug in a (really big) code block, and you are expected to avoid an unhandled exception at all cost, what would you do? (except for a comment why you introduced the try block, and an error message with more than half a bit of information)
On a very loosely related note, I'm glad I didn't encounter the exception handling equivalent of if ... else if, that is, try ... catch try. So far.
Admin
But should it be cleaned up?
After a few more general code changes, the if/else blocks won't do the same thing any more. Will that be due to intentional changes, or coffee & pasta failures?
Admin
Today's "Internet Rant on Today's Featured Language" post. http://tech.jonathangardner.net/wiki/Why_Java_Sucks
Admin
I've just read that incompetent rant and I want my 5 minutes back.
Admin
"Anyway, these are only 24 lines of code. I've seen similar contraptions stretching over hundreds of lines."
This is simplified and cleaned up a lot because it took me the best part of an hour to make sense of the original and figure out that it was as redundant as it looked.
For instance, before every return point there's about 15 lines including another DAO call and ANOTHER return point. And I omitted three more layers of if/else with similar code between the outer try{} and the portion of the innards shown here. The whole thing is just shy of 200 lines, and the two if() blocks make up over half of that.
Admin
Also, it doesn't literally say "error details". But it is a hard-coded and only slightly more informative string that's the same everywhere.
Admin
"I still think a failure to clean up is more likely. doCalculation having its own try catch doesn't sound good."
Yeah, I like to have the exception handling placed as high up the stack as possible. But since we don't know anything about it, we can't really tell if it's good. What if
doCalculation
has a side effect (e.g., I/O or network comms), and it needs to be able to handle layer-specific exceptions without passing them up to its callers?(Why a function named
doCalculation
would have side effects is another issue.)(Also whoever wrote this code doesn't seem likely to be that concerned with proper design.)
Admin
This is debatable. We don't know the public/private nature of the other method. Unless doCalculation is a private method, the callee should NOT assume it is handling the try/catch logic. This looks to be more of a re-factor that was never cleaned up as the other person suggested.
There's nothing worse than one method RELYING on the fact that another method is 'taking care' of the exception handling, only to have another person re-factor that method because it's being called by yet a third method and they wanted access to the Exceptions being thrown. Bam, now doCalculation throws and crashes the web serivce. Where as at least the original WTF code will still work.
Sometimes the code in WTF is not a WTF and just someone wanting to be a narcissistic jerk and join in on a good ol' fashion circle
Admin
Another comment why you are stupid