• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Mahendra Athneria
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it means List add(0,..) and linkedList addFirst() work in the same fashion?

Yes, functionally they're equivalent.
 
Mahendra Athneria
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for the helps.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!