• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys for the helps.
 
reply
    Bookmark Topic Watch Topic
  • New Topic