I've been through a very similar situation when I had to deal with a terrible legacy Windows Forms code written by developers that clearly didn't know what they were doing.
First of all, you're not overacting. This is bad code. Like you said, the catch block should be about aborting and preparing to stop. It's not time to create objects (specially Panels). I can't even start explaining why this is bad.
That being said...
My first advice is: if it's not broken, don't touch it!
If your job is to maintain the code you have to do your best not to break it. I know it's painful (I've been there) but you have to do your best not to break what is already working.
My second advice is: if you have to add more features, try keeping the existing code structure as much as possible so you don't break the code.
Example: if there's a hideous switch-case statement that you feel could be replaced by proper inheritance, you must be careful and think twice before you decide to start moving things around.
You will definitely find situations where a refactoring is the right approach but beware: refactoring code is more likely to introduce bugs. You have to make that decision from the application owners perspective, not from the developer perspective. So you have to think if the effort (money) necessary to fix the problem is worth a refactoring or not. I've seen many times a developer spending several days fixing something that is not really broken just because he thinks "the code is ugly".
My third advice is: you will get burned if you break the code, it doesn't matter if it's your fault or not.
If you've been hired to give maintenance it doesn't really matter if the application is falling apart because somebody else made bad decisions. From the user perspective it was working before and now you broke it. You broke it!
Joel puts very well in his article explaining several reasons why you should not rewrite legacy code.
http://www.joelonsoftware.com/articles/fog0000000069.html
So you should feel really bad about that kind of code (and you should never write anything like that) but maintaining it is a whole different monster.
About my experience: I had to maintain the code for about 1 year and eventually I was able to rewrite it from scratch but not all at once.
What happened is that the code was so bad that new features were impossible to implement. The existing application had serious performance and usability issues. Eventually I was asked to make a change that would take me 3-4 months (mostly because working with that code took me way more time than usual). I thought I could rewrite that whole piece (including implementing the desired new feature) in about 5-6 months. I brought this proposition to the stakeholders and they agree to rewrite it (luckily for me).
After I rewrote this piece they understood I could deliver much better stuff than what they already had. So I was able to rewrite the entire application.
My approach was to rewrite it piece by piece. First I replaced the entire UI (Windows Forms), then I started to replace the communication layer (Web Service calls) and last I replaced the entire Server implementation (it was a thick client / server kind of application).
A couple years later and this application has turned into a beautiful key tool used by the entire company. I'm 100% sure that would've never been possible had I not rewritten the whole thing.
Even though I was able to do it the important part is that the stakeholders approved it and I was able to convince them it was worth the money. So while you have to maintain the existing application just do your best not to break anything and if you're able to convince the owners about the benefit of rewriting it then try to do it like Jack the Ripper: by pieces.
There's a lot to say here.
Inventory
's api looks like a value object (since it has no identity, on its own, I suppose that it belongs to a Player
). If so, it should be immutable, returning a new instance each time you add or remove an Item
.
- Your
Item
class looks like a shared identifier, thus it should override equals
and hashcode
and (strictly speaking, in DDD term) it should not hold any reference to the icon
since it's not useful for its own invariants. You should use a domain service that take the Item and provide the proper icon instead (probably wrapping a statically defined Hashtable). However, be pragmatic with this: if you measure actual performance issues with such (formally correct) approach, keep the field there.
- The
Slot
, as it stands, cannot be considered a domain object, because it does not implement equality in a domain relevant way (it ignores the quantity). This is perfecly correct for a private class internally used by the Inventory
, but should not be exposed to clients (as in the second Inventory
's implementation of the first solution you proposed). This because the clients could compare for equality two Slot
s from two different players, obtaining unreliable results.
- To get rid of most boilerplate code, I would code
- a supporting functor interface like
ItemQuantityChanger
(below)
- a "functional" immutable collection, say
ItemsSet
, with a metod of signature ItemsSet set(Item item, ItemQuantityChanger transformation)
that iterates over a simple array for the item to change. Such method would always create a new ItemsSet to return, without the Item (if the trasformation returns zero), with a new Item if the item is not found (with the quantity returned by applying the transformation to zero) or simply with the previous item coupled with the result of the trasformation.
However, concretely, I would just use an ImmutableList.Builder
, since the functional design described in 4 adds few value in this case. A single if, in a method, is not that huge smell.
public interface ItemQuantityChanger {
/**
* Accept the current quantity of an Item and return the new one.
*/
int applyTo(int currentQuantity);
}
Best Answer
What?
No.
You could use method overloading, something like
, and when calling
handle
, the most appropriate method will be called. : However, I would say that you basically do three things with Exceptions:throws
1 and 2 are not worth it; calling a generic exception handler would be more verbose than just writing the code required.
3 tends to be longer code, however, it is very unusual that you can catch and treat exceptions in the same way in different parts of your code; if you do, this tends to be a code smell; perhaps you need a broader try statement or some disjoint code needs to be brought together.
So I don't think there's a reason to do what you describe.