- 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
worse, we silently gobble the exception up, throwing only if rolling back fails!
Admin
So that's "Rest in Perdition" ?
Admin
A shocking number of people do redundant empty checks, I used to work at a place that used jQuery(back when it was semi-popular), and it was the coding standards to always check if the length was empty, and if so replace it with null.
This resulted in code like
Instead of the much simpler and obvious:
I complained about it so often but the tech lead at the time really didn't understand how anything worked and insisted we do what he knew worked rather than trying to change things to make them better.
He was also the person who thought the observer pattern meant we should pass the list of listeners to be triggered to the observer ala:
It took us 2 weeks to convince him that maintaining the list of events everywhere they may be triggered is exactly the problem the observer pattern is supposed to solve, and that if we don't stop it we are no better off than before we wanted to introduce the pattern.
It was interesting to say the least.
Admin
Reminds me of the time I worked for a manager who was all about the latest trendy thing. If it was new and cool, it was in the stack. (And if it used to be new and cool but was now old and busted, it was still in the stack.) One of these things was NoSQL. Everybody who was anybody in those days was using a NoSQL DB, including our team. Except that the actual product was a replacement for an old MS Access app, and the data was highly relational. Of course, NoSQL is not designed for relational operations, which meant that all of the filters and joins and everything else that your typical SQL engine does really well had to happen in the application code instead. And they wondered why the system was so slow and fragile...
Admin
The only time the empty check is appropriate is if you want to display/log something about the collection being empty in place of just doing nothing or showing an empty table. But if there's no
elsefor theif, it's redundant.Admin
Now you have to check whether there's some code that was counting on Foo's not having Bar's as a sort of 'new' status!
Admin
It's been a long time since I had anything to do with Oracle DBs, but I'm sure this could have been easily dealt with by using insert, update & delete triggers on the appropriate table(s). If a batch process was really required, then a stored procedure would do the job with just one call instead of all that nonsense
Admin
A lot on life is just moving stuff around
Doing laundry? Move laundry into laundry basket Move laundry basket to the washing machine Move laundry into the machine Move detergent into the machine Mine the knob on the machine to the right setting Move the laundry into the dryer Move the laundry into the basket ...
Admin
Another problem with this code is catching 'Exception' - that's the super-class for all exceptions. Generally, you want to catch only the most specific exceptions that you can. Particularly if you actually handle the exceptions you expect properly. Basically
catch(TransactionException ex) {transaction.rollback()}, otherwise any exception at all (including out-of-memory, file IO issues, null-pointer exception, idiot-using-the-computer exception) will cause the transaction to roll back. Which might be what you want, but probably isn't.Admin
Catching that one has been the holy grail of systems engineers since before C was a thing. I should know. I was there. Unfortunately we've made zero progress so far.
Admin
The worst sin of all is a separate transaction for each
Foo(plus the implicit one for reading the list of them), when it would be quite practical to hoist the whole thing into the database and get it to do all the updates in a single call (or at least only a few if you take the time to implement some sort of batching scheme). This code is going to be vastly less efficient than it should be, and purely through sheer incompetence.The second worst sin is the swallowing of exceptions like that.
Admin
In general, the trouble is that the code you run in a transaction is not limited to just throwing database exceptions. There may be other legitimate exceptions that mean you want to roll back the transaction but not necessarily terminate the program. The point of a transaction is to keep your database logically consistent: either every update you do in a transaction is applied to the database or none of the updates is applied to the database. Every exception that stops you from from doing all of the updates needs to cause a rollback.
Another reason why you would catch all exceptions is if somebody chooses to wrap any exception thrown in a
RuntimeExceptionso they then don't need to think about handling errors. This is a WTF pattern and its used in this article.