1

I'm making PriorityQueue<T> classes, and right now I am using List<T> as a backing store. The following are the errors that I have been getting:

Have Correct Suffix/ Do not have incorrect suffix

Do not expose List<T>

For the -Queue suffix I need to inherit Queue<T>, but to sort a Queue<T>, I would need to empty the queue, sort, and then refill the queue. Also, if I inherit from Queue<T>, I would violate LSP because a priority queue is not a FIFO collection.

For one of the types of PriorityQueues that I am making is using an IComparer<T> to compare the elements, but IComparer<T> is only supported on arrays and List<T>.

I did see this question, but it doesnt fully relate to my question.

So here are my questions: Should I suppress these code analysis warnings? Should I inherit from Queue<T> and rewrite my classes to work off of it, even though it is less efficient? If not, should I still swap out List<T> for something else?

Edit: I don't know if this would make any difference, but the following is my setup of each of my classes:

  • PriorityQueue<T> -- abstract base class (Sort() is abstract)

  • PriorityQueue<T, TComparer> -- subclass that uses a comparer to sort

  • ReflectionPriorityQueue<T> -- subclass that uses reflection to sort, specifics not important to this question.
Community
  • 1
  • 1
JKor
  • 3,822
  • 3
  • 28
  • 36
  • Is the `List` a (private) backing store or the base class? – H H Jun 28 '12 at 19:09
  • Code analysis is used to identify *likely problems*, and make *suggestions* about how to avoid them. If your code will be more efficient/easier to read/etc. by ignoring the suggestions, then by all means do so. (Though if your `List` backing store is `public`, you should definitely make it `private`.) – dlev Jun 28 '12 at 19:10
  • Are you exposing the list directly on a public interface? – Wiktor Zychla Jun 28 '12 at 19:11
  • The list is a private field that is then exposed through a read-only protected property. It is not public. – JKor Jun 28 '12 at 19:25
  • 1
    @JKor Shouldn't be exposed protected either - "protected" is still part of the public API, as another assembly can subclass you. – Reed Copsey Jun 28 '12 at 19:31

1 Answers1

5

These errors have nothing to do with using a List<T> - they're about the public API.

Have Correct Suffix/ Do not have incorrect suffix

This one is debatable - personally, I'd disable this warning, as a PriorityQueue<T> should, in my opinion, use the name PriorityQueue<T>.

Do not expose List

This just means you can't publically expose the List<T> as a List<T>. If you're encapsulating the list, this should never appear. As long as you keep your list private, this warning should go away.

Should I inherit from Queue and rewrite my classes to work off of it, even though it is less efficient? If not, should I still swap out List for something else?

I suspect the problem is that you're trying to subclass List<T>, which is a bad idea. Encapsulate it as a private member, and implement the appropriate interfaces instead.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373