- 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
Could be worse. It could have forced you to put the cabinets back in the same order you found them 14 hours later.
Admin
Well, if the list was never correctly sorted in the first place, what could still happen at that point? At least the exception gets logged so that you know what blew up where.
I'm more concerned about it returning null when an exception happens...
Admin
Logged twice, actually...
Admin
How do you mean? Log4J uses the first argument as a message (which could contain more info about the context), the second argument is for printing the exception and it's stacktrace.
Admin
Is this code implying that it iterates over the cabinets to individually add said cabinets (as cabinets of another (simpler?) type) to the list to sort them (sans last element in the list)?
Maybe the requirements was to create separation between the Ajax side and the application?
Admin
The e.toString() gives some data about the exception, which is then printed again when the stack trace is printed. It's not technically logged twice but the e.toString() is pretty much useless
Admin
INB4 listed The article is already visible on the frontpage.
Admin
Well the implementation leaves some room for improvement, but overall we can see the programmer was careful to write robust code. For example, the repeated sorting ensures the list is never left in an unsorted state. So even when it'd abort on an exception, it could return the list it built so far and it would be mostly sorted. I think keeping order is best done continuously so you never have a big mess at hands!
Admin
and you always know which element was most recently added -- it's the last one - bonus!!
Admin
is only executed if something goes wrong (
return
in the try block).But no matter what - anything with an
open
or aclose
in it without a correspondingclose
resp.open
in the same procedure and called under the exactly same conditions (save for the corresponding open having failed) looks so wrong to me it hurts.Admin
Nice feature! I think programmers these days are too obsessed with efficiency. For starters, this article doesn't even measure the performance but relies on some academic notion of how performant it ought to be.
Well in theory the code could run in O(n) if the result of getAllPrograms() was already sorted and Collections.sort() could introspect what parts of a Vector were already sorted. Eat that pessimists.
Admin
Admin
a finally clause is always executed, regardless of return statements or exceptions.
Admin
Right. Had forgotten. Confusing, though.
(I avoid that kind of pattern if possible. Am fine with using, though.)
That doesn't change anything about the fact that there is only one place here that mentions
HibernateSessionFactory
.Admin
Mmm nope...
Source: https://msdn.microsoft.com/en-us/library/zwc8s4fz.aspx
Admin
Sorting on every insert. Very common mistake. One line fix.
Admin
Also, use the Vector class for some additional thread-safe inefficiency.
Admin
Umm, isn't this Java code? Because in Java, the finally would be executed.
Admin
Surprising yet sane behavior.
Admin
It is Java... man I need more coffee today :sweat_smile:
Anyway, in C#, the finally is executed in all cases too.
Admin
The other problem: if whatever goes wrong causes the VM to be hosed, then the finally clause won't be run because the process that was supposed to run it is sitting in a corner crying.
Admin
Um, so TRWTF is "while it.hasNext()" instead of "for (Iterator it = cabinets.iterator(); it.hasNext(); it.next())" or whatever the syntax is that I can't be bothered to check?
Of course, what David S. should immediately do is use:
and so replace 4 lines of code with 1. A significant improvement.
Oh come on. Nobody is using pre-Java-5 any more.
Admin
There is exactly one reason to use the
Iterator
syntax that I can think of:For a lot of
Collection
types, the Iterator'sremove
command is the only way to remove an element while iterating without Java throwing aConcurrentModificationException
.Admin
I would think the order would be something like...
On2 log n
Except that, since it doesn't sort the last element added each time, maybe...
O(n2-n) log n
...and that is terrible. A bubble sort would be faster. You can at least tweak that to get O½n2.
Admin
Admin
Meanwhile, George Takei calculates this as being O(My)!
Admin
Although this is terrible, interestingly enough in Java 7 (and up) the overall O is lower, thanks to TimSort and its huge bag of special cases.
Admin
What we have here is a failure to communicate.
Admin
YMBNH
It's curious that the cabinet reference went unseen by you, what do they teach at university these days? The world of disks that we call the internet has more than enough information in it for you to figure out what I am talking about.
Admin
Actually initially when I saw the 'SearchProgramShell' thing my brain parsed that as 'shell: sort list' (that is, fork a shell, call /bin/sort with a list of elements and parse the result back in again...., Yes that is possible to do in some languages, however I would never recommend that....)
Admin
Maybe you're one of those young 'uns messing up my lawn. The "empty and then replace later" is highly reminiscent of "Cool Hand Luke." Hence my response.
PS I've been on TDWTF for at least 8 yrs. how 'bout you :smile:
Admin
The "14 hours later" part was supposed to be a reference to a cabinet in the Discworld novels. Hence my response.
For all I know, based on your post and likes history, you never got past the articles section :smile:.Admin
Admin
I think code like this is why Apple's Objective-C sort first checks whether there is a sequence sorted in ascending / descending order at the start and the end of an array. So when you add the n-th element and sort, it sees in O (n) that the first n-1 elements are sorted, and can merge the subarray from 1 to n-1 with the subarray from n to n, also in linear time. All in all it's not much slower than a linear insertion sort. O (n^2).
(Seriously, it happens quite often that an array is sorted which is already mostly sorted).
Admin
@PWolff The whole point of "finally" is that it always executes no matter what happened before.
Admin
And Delphi
Admin
Clearly the goal of this code is to optimise the
m.add(ca)
by giving it a pre-sorted list. :passport_control:Of course that would only make sense if
m.add()
needed a sorted list in the first place, which it would only do if it was adding the new element in the correct place, in which case you wouldn't need to sort the list explicitly because it would already be sorted. But hey, defensive coding, right?Admin
Was this before the introduction of sorted lists? Otherwise that guy might have used one, and write a hand-crafted routine to verify it was indeed sorted. (And sort it, should it be unsorted.)
Anyone else that wants to tell that a finally block will be executed no matter what (unless the thread is killed?)