(Untitled)

Jun 12, 2009 12:05

Now I don't claim to be an excellent coder, but I've seen some really stupid shit in my several years on my current job.

Just now I've come across quite a doozy:

public Connection getConnection(String username, String password) throws SQLException ( Read more... )

Leave a comment

Comments 4

xpovos June 12 2009, 17:59:46 UTC
Bear with another stupid and purely hobbyist coder.

Is the problem that the SQL connection is public? That seems a no-no to me.

Or is the problem in the exception handling, which is more complicated than I can figure out to really go, "WTF?" over, but recognize as at the very least abnormal.

Reply

mickj June 12 2009, 19:57:46 UTC
Ok in a quick nutshell here's the problem with the code, which has to do with the try/catch:

Typically, you throw exceptions in java when an error case happens, which is why one normally wraps some code in a try catch block in order to trap exceptions and handle them in whatever way that would be appropriate.

The big problem here is that he has a try catch block, but right away he just throws an exception with no other preceding code.

Throwing an exception like that typically means that you are intending for that function to never be called normally. This happens sometimes when you are either extending a class or implementing an interface that has methods that you don't want to be called or don't intend to ever use. But you'd want to just do something like this:

public void dontEverCallMe() {
throw new UnsupportedOperationException("you stupid monkey!");
}
But as you can see, he actually does implement the method after the try/catch, which is puzzling. Essentially, try/catch block he has there does nothing but output stuff to ( ... )

Reply

xpovos June 13 2009, 17:12:33 UTC
So I was on the right track, it was both really.

Reply

pierceh June 15 2009, 06:03:25 UTC
he just wanted to really, really know what the stack trace of this method every single time it was called

This. Seems likely that it's just some hacky debugging code someone threw in and forgot to take out when they were done with it.

Reply


Leave a comment

Up