Woot4Moo
Woot4Moo

Reputation: 24316

Java refactor to Generics industry standards

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.

Upvotes: 5

Views: 869

Answers (7)

mikera
mikera

Reputation: 106351

It is usually good idea to refactor to use generics.

It's not required so don't treat this as an urgent task, but you can consider not using generics is a mild form of technical debt - so if you have time available and the code base is planned to have a long ongoing life then it is worth investing in upgrading it.

The main benefits are:

  • Better compile-time type checking, which will reduce errors
  • Remove of unnecessary casts from your source code, which makes code more readable
  • It is still backwards compatible with old code (thanks to type erasure)

There is no real downside - the only potential issue I can think of is if you ever wanted to port the code back to earlier versions of Java without generic support. But that would be a very unusual thing to do!

If you do decide to refactor into generics then I recommend the following steps:

  1. Turn on all your compiler warnings (Eclipse has pretty good warnings)
  2. Add the generic type to your class first e.g. MyClass<T>
  3. Then change the type of any method signatures / internal fields / data structures to use T
  4. You will probably have many warnings / errors throughout the code at this point. This is OK, just work through them and fix them all. Often your IDE may be able to "quick fix" many of them.
  5. Write / refactor test cases that demonstrate the generic features

It is fairly quick to do all this - I think I managed to refactor about 10,000 lines of Java library code to use generics in less than one day, which included updating some client code.

Upvotes: 0

fastcodejava
fastcodejava

Reputation: 41097

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.

Upvotes: 3

royal
royal

Reputation: 550

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.

Upvotes: 1

Stephen C
Stephen C

Reputation: 718826

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.

Upvotes: 1

polygenelubricants
polygenelubricants

Reputation: 383746

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

Upvotes: 4

Dan Dyer
Dan Dyer

Reputation: 54475

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.

Upvotes: 8

Kevin Bourrillion
Kevin Bourrillion

Reputation: 40851

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.

Upvotes: 1

Related Questions