views:

218

answers:

6

There seems to be some debate over refactoring to utilize java generics within my current team. The question I have is what are the current industry standards in terms of refactoring older Java code to take advantage of some of these features? Of course by industry standards I am referring to best practices. A link to a book or a site with these listed will be awarded the answer vote as that is the least subjective way to handle this question.

+3  A: 

Utilizing java generics is definitely a good idea. It is backward compatible. So the codebase you cannot convert will continue to work with the new code.

EDIT : I should have mentioned type erasure as the reason which makes backward compatible.

fastcodejava
thanks for the helpful information, however is there any set of standards as to what should be refactored to generics and not just do it for the sake of doing it.
Woot4Moo
@Woot4Moo - there are no such standards. It is a decision that needs to be made on a case-by-case basis.
Stephen C
+1  A: 

Best practices for adopting generics? The first best practice is "do". Try to eliminate as many casts as you can from your code. If you want to make your life easier, use IntelliJ's "Generify" refactoring -- just point it at your entire codebase, let it do its thing, and then do a little post-cleanup.

Kevin Bourrillion
the question now becomes is it do for the sake of doing or do because its proper?
Woot4Moo
Do it if you can pull it off. Sometimes, writing generic non-container classes can be difficult (learning to use upper, lower bounds and multi-bounds). The best rule of a thumb is that if you are casting a value that comes *from* generic call, then most likely your generification is not good enough or you've hit one of these few corner cases that the type system can not express. Having false sense of security provided by generics and seeing your system fail at runtime with ClassCastException can be a very frustrating experience.
ddimitrov
@Woot4Moo - well obviously, you should NOT do it just for the sake of doing it!
Stephen C
+4  A: 

The papers from this MIT research group might provide you with some useful guidelines:

polygenelubricants
+7  A: 

I don't think that blindly following what somebody else declares to be "best practice" or "industry standard" is ever a good idea. You're in the best position to decide whether changing your code is worthwhile or not.

The questions you need to answer are what benefits will you get from upgrading the old code, what will it cost, and what are the risks?

The main benefit is that you will have improved compile-time type checking, which should help to detect bugs in new code that uses the updated code. It may even highlight bugs in existing code. Code that uses generics, while sometimes quite verbose, is typically more readable as it is explicit about which types are valid in which contexts. You'll also no longer have to suppress/ignore compiler warnings.

The cost is the amount of time it will take to make and test the necessary changes to introduce generics. Any time you make code changes there is a chance that you might introduce bugs, so that's a risk. Do the benefits outweigh the costs? That depends on how much code you have, how it's being used and what other demands you have on your time.

Dan Dyer
Indeed, I have been refactoring package by package only when it makes sense for each one.
Software Monkey
+1 for "improved checking for new code". I'd use generics only for new code and update the old code class for class only when I need to touch that class anyway.
Thilo
+1  A: 

Your team should be balancing the benefits of a large-scale refactoring against the cost and the technical and business risks of doing this ... and the other priorities that your team has.

"Best practice" arguments and opinions from people who don't understand your project and the business context are simply not relevant here.

Stephen C
+1  A: 

Best Practices don't exist. That's a weird term that suggests that the door closes on the 'bestness' of a particular solution... Use generics? Yes. Immediately. It's an awkward journey, since so many of the big libraries (Hibernate, Spring) still fail to embrace them completely.. but in my experience, dealing with a mix of generics and brave-casts still makes for a better code base than not using them at all.

I'd also make it policy to convert-as-you-touch instead of some sort of giant refactoring mission.

royal