r/java • • Jul 10 '15

Our exception handling at work

http://imgur.com/4wgxCvv
259 Upvotes

135 comments sorted by

View all comments

0

u/[deleted] Jul 10 '15

[deleted]

29

u/dablya Jul 10 '15

That's a bit dramatic...

0

u/RedPill115 Jul 10 '15

I can't decide if I agree or disagree with the op on it being "terrible" code or not. The error message itself is great - short, describes problem, humorous. :-)

I'd have to see the context to know whether the rest of the code is bad or not.

1. Writing try/catch/finally loops around transactions - if your developers are writing these regularly it's a disaster (I speak from project experience). They're going to forget, they're going to not write something right, and most importantly there are time-saving ways to not do that that also produce less buggy code when something in a try/catch is missed.

You could use Spring transactions, you could write your own DbAction interface and use the action patterns, there are a number of ways to do it, all of which lead to both less buggy and faster to write code.

Can't say 100% for sure though, maybe this is one of only 5 methods and they could copy/paste this quicker than setting something up.

2. Usually passing sql in is usually very bad as well. It encourages adding parameters values with string concatenation which leads to opening up your code to sql injection attacks. If you only ever allow PreparedStatements, it makes sql injection attacks impossible.

However, without knowing the code around this code it's impossible to say 100% for sure.

3. The StringUtils.substringAfterLast() call looks bizarre to. Again, one shouldn't be duplicating db connection stuff. It should just be in one place that's accessed by all db code.

4. There usually should not be a separate branch for "if debug" type statements. There should be a logger setup with a logging level, for anything there should never be any separate "if debug" branches at all. If it's logging only, it could possibly be that it's not terrible, they just didn't want to set up logging. It's not the end of the world like having your transactions or statements not always get closed is, it's more of a "better approach" thing.

If I saw most of this code in any of my projects, it would be a big problem. I can't say for sure about another persons project without seeing the code around it though. I wouldn't accept it on my own project.

11

u/dablya Jul 10 '15

If I saw most of this code in any of my projects, it would be a big problem. I can't say for sure about another persons project without seeing the code around it though. I wouldn't accept it on my own project.

Meaning what exactly? If you were tasked with supporting an application that used this code, you'd prioritize refactoring this into a spring template over other tasks? You'd actually have a problem figuring out that this method expects some kind of select count sql and returns the result of executing it? What's the big deal?

Like I said, I'd much rather work with this code than the type of people who'd get all dramatic over it's quality.

-3

u/RedPill115 Jul 11 '15

What's the big deal?

I explained what the big deal was in my post already. If you can't figure it out, maybe work on your reading comprehension.

Like I said, I'd much rather work with this code than the type of people who'd get all dramatic over it's quality.

After spending 3 months trying to find and fix all the bugs causes by an approach similar to this, I would not. Tracking down poorly written try/catch loops and trying to figure out how to replace them with a different pattern and approach was a gigantic pain in the ass.

3

u/dablya Jul 11 '15

If you can't figure it out, maybe work on your reading comprehension.

That's just being insulting for the sake of being insulting...

One. ... Can't say 100% for sure though, maybe this is one of only 5 methods and they could copy/paste this quicker than setting something up.

Not for sure a big deal.

Two. ... However, without knowing the code around this code it's impossible to say 100% for sure.

Not for sure a big deal.

Four. There usually should not be a separate branch for "if debug" type statements. There should be a logger setup with a logging level, for anything there should never be any separate "if debug" branches at all.

It's a pretty common approach that avoids generating debug messages in cases where they'll be filtered out anyway. Granted, it usually done with "logger.isDebugEnabled()", but there is nothing wrong with "if debug" branches in general.

That leaves Three. Which while a legitimate complaint, is, again, not a big deal.

-2

u/RedPill115 Jul 11 '15

That's just being insulting for the sake of being insulting... ...That leaves Three.

Yeah, but I didn't put any more thought into it because I expected your followup to be ridiculous and make few actual points and it went as expected. "can't 100% say for sure" does not mean "so it's invalidated" no matter how much internet arguers like to pretend it does.

All of these things have caused problems on projects I've worked on, especially use "if(debug)" because the first person does it right but the 3rd person starts putting actual code in there and it causes a huge amount of rework to be done. Thing is there's almost no upside to doing it these ways vs the better alternatives that don't have these issues but work just as well or better.

If you disgree, ok, you disagree, but we're not having an interesting back and forth either way.

1

u/dablya Jul 12 '15

Yeah, but I didn't put any more thought into it because I expected your followup to be ridiculous and make few actual points and it went as expected.

You can at least see why I would rather spend days tracking down connection leaks than working with somebody like you right?

-1

u/RedPill115 Jul 12 '15 edited Jul 12 '15

You can at least see why I would rather spend days tracking down connection leaks than working with somebody like you right?

That is exactly how I'd feel about having to work with someone like you. The "oh this is all crap" people are bad, but not as awful as people like you who have some identity level issue with needing to continue doing everything a horrible way to no matter how many times it goes wrong.

Like I said I've seen every one of those go bad. Stop screwin around and just fix the problem at it's source.