|
|
||
Dru Devore's BlogCommunity: JDK ArchivesJava with GenericsPosted by ddevore on January 12, 2006 at 12:06 PM | Permalink | Comments (9)I have been using Generics now for almost a year now and have found a love/hate relationship with them. My introduction to what Generics would do to you was when I compile a project with a UniqueList utility class I have been using for years now. This is a simple extension to the ArrayList which does not allow duplicates, nulls and keeps them in order. I really like this class because it is easy to use and very simple to make. The problem was that when the compiler hit the class I though it was going to hit me over the head with a ball bat. It threw errors everywhere. Since Generics are not going to go away I figured I would start my Generics learning with rewriting my utility class. I started by reading what ever I could find about them then I found this tutorial, nice job Gilad, and went to work. Modifying UniqueList On with Generics Dislike Instead of the new method. This is not a big deal until you use that variable and the compiler yells at you with ball bat in hand. For some reason the compiler thinks it knows more about Generics than I do which I am sure it does. The problem with this is that sometimes you need a "generic" object list and not a Generics list. There is no way to stop this checking and -Xlint:unchecked will show a detailed list of these warnings. Now I am the type of person who likes to have my code as clean as possible. I don't like turning off checking and I usually have checking for deprecated classes turned on to remind me of things which need to be cleaned up. When I find a warning I usually go fix it if there is any way I can do it be it my warning or someone elses. For me to have a clean command line and have this warning bugs me. This is the big dislike. Then you have the length of the lines for Generics. As I mentioned above I like clean code. I keep my code within the 80 col marker when possible but like descriptive names for my classes. This creates a problem of long lines when you use Generics. Take for instance a class with the name AccountInformation used in a ArrayList. This line indented takes you over the 80 col limit so you have to wrap the line Likes New Method Old Method As you can see from this simple example the new method is shorter and simpler to use. The compiler automatically puts in the casting of the type to the variable and iterates over the list. No more manual iteration over the list or unsafe casting. This is possible because the compiler does the type check for you to insure that the list contains only AccountInformation objects, this is where the warning from the dislikes comes in, so you always know what you are working with. If the Old Method above were to be written to insure safe casting it would look more like this. Old Method with Safe Casting You can see from the examples above that it lessens the code required for a loop and insures safe object casting. The compiler also insures the correct type improving the typeing of the objects and helps to eliminate the need for as much error checking. Conclusion More information Update I have not looked at annotations yet I guess that should be my next task and I will test this one out then. | ||
|
|