File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Performance and the fly likes Loose coupling : Avoid using implementation types like 'LinkedList'; use the interface instead Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Performance
Reply Bookmark "Loose coupling : Avoid using implementation types like Watch "Loose coupling : Avoid using implementation types like New topic
Author

Loose coupling : Avoid using implementation types like 'LinkedList'; use the interface instead

Mahendra Athneria
Greenhorn

Joined: Jan 18, 2009
Posts: 16
Hi All,

I am using sonar to check the code quality. i got below error but dont know how to fix it.

" Loose coupling : Avoid using implementation types like 'LinkedList'; use the interface instead"

in my code i have declared


and somewhere in the function i am using it like


when i am running sonar check i am getting below error

" Loose coupling : Avoid using implementation types like 'LinkedList'; use the interface instead"

when i try to put List instead of LinkedList in declaration then i am getting compilation error because List does not have addFirst() method.

how to fix that error?

Help guys...

Regards,
Mahendra Athneria
Mumbai
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

Mahendra Athneria wrote:
how to fix that error?


It's not an error, it's a piece of advice -- and in this case, it's bad advice, since you're actually using the richer interface of LinkedList. I haven't used Sonar personally, but hopefully it has some way to turn off this warning for this line -- with a setting somewhere, or possibly using an annotation in the source (ugh).

Anyway, feel free to ignore it. This is a case where automatic style checking just makes more work (in fact, most of the time, automatic style checking just makes more work, but that's another discussion.)
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
I'm not sure what this has to do with performance -which is the title and topic of this forum- but "add(0, ...)" -which is part of the List interface- does the same as "addFirst(...)".
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

It does indeed, except that by calling addFirst(), you're guaranteed to be calling an efficient operation. If you generalize to add(0,...) then you're implying that any List is Ok, when the truth is that anything but LinkedList will be inefficient. Using the more specific type is really more appropriate here, IMO.
Mahendra Athneria
Greenhorn

Joined: Jan 18, 2009
Posts: 16
Hi Ernest Friedman-Hill,

Thanks for your reply. but my doubt is that by using addfirst() i am using more specific method and that is my requirement. if we can do the same thin with add(0,..) then what is the use of addFirst() and linkedlist?

Regards,
Mahendra Athneria
Mumbai
India
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Ernest already mentioned it: efficiency reasons. There are implementations of List (notably ArrayList) for which inserting an element at position 0 is an O(n) operation instead of O(1) (which it is for LinkedList).
Mahendra Athneria
Greenhorn

Joined: Jan 18, 2009
Posts: 16
Hi Lester,

thanks, i got it.

but it means List add(0,..) and linkedList addFirst() work in the same fashion? only difference is the Big O complexity of both.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
it means List add(0,..) and linkedList addFirst() work in the same fashion?

Yes, functionally they're equivalent.
Mahendra Athneria
Greenhorn

Joined: Jan 18, 2009
Posts: 16
Thanks guys for the helps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loose coupling : Avoid using implementation types like 'LinkedList'; use the interface instead
 
Similar Threads
LinkedList problem
LinkedList
What is the Advantage of Implementing List?
Can service discovery and binding automated ?
Interfaces