6
votes
\$\begingroup\$

Resharper thinks I should change this

while (dockPanel.Contents.Count() > 0)

to the following:

while (dockPanel.Contents.Any())

Without thinking twice about it, I switched to the any version because .Any() is more readable for me. But a coworker of mine sees the change and asked why I did it. I explained that this improves readability but his objection was the following:

  • Any is an ambiguous word.
  • People may not be familiar with LINQ, so they might not be aware of what Any does at first glance.
  • Count> 0 is universal. You don't have to know C# to find out what you're trying to do.

The confusion is probably compounded by the fact that I'm working overseas. People here speak little to no English here.

Which method should I stick with?

asked Jun 28, 2013 at 14:49
\$\endgroup\$
4
  • \$\begingroup\$ "because I know ReSharper is smarter than me" I've had to disable several ReSharper's suggestions, because they actually produced code that I thought was less readable than the original. \$\endgroup\$ Commented Jun 28, 2013 at 16:09
  • \$\begingroup\$ The while (collection has elements) loop strikes me as odd. Presumably the collection is being emptied out as part of the loop body? Would it be clearer to use a foreach (item in collection) and then empty it at the end? \$\endgroup\$ Commented Jun 28, 2013 at 18:36
  • \$\begingroup\$ I find all three arguments to bogus. What I read from this is one grumped coder who can't handle to be corrected. \$\endgroup\$ Commented Dec 6, 2013 at 20:29
  • \$\begingroup\$ Note that .Count and .Count() are NOT the same thing when working with List<T>. \$\endgroup\$ Commented Jun 17, 2015 at 8:38

2 Answers 2

19
votes
\$\begingroup\$

For me, it's about intent. What does your code's business logic say if you read it in English (or your native language)?

It usually comes to me as "If there are any employees who are in the management role, then show a particular option panel".

And that means .Any(). Not .Count(). Very few times will I find myself using .Count() unless the underlying rule talks about "more than one" or "more than two" of something.

As a bonus, .Any() can be a performance enhancement because it may not have to iterate the collection to get the number of things. It just has to hit one of them. Or, for, say, LINQ-to-Entities, the generated SQL will be IF EXISTS(...) rather than SELECT COUNT ... or even SELECT * ....

answered Jun 28, 2013 at 15:24
\$\endgroup\$
3
votes
\$\begingroup\$

I agree with Resharper and I'd go for the .Any() version.

The reason is that I associate .Count() with operations in which the number of elements actually matters while in your case you just need to check if there is at least one element.

On the opposite .Any() helps you communicate that you need to iterate until you have no more elements.

answered Jun 28, 2013 at 15:34
\$\endgroup\$
3
  • 2
    \$\begingroup\$ «.Any() helps you communicate that you need to iterate until you have no more elements ». I don't think so: Doesn't any rather mean you'll stop iterating as soon as you find one match? \$\endgroup\$ Commented Jun 28, 2013 at 16:35
  • \$\begingroup\$ Poster probably meant "you need to iterate until you have no more elements that need to be filtered through". If it already has a match, then there is no need to filter through any more elements. \$\endgroup\$ Commented Jul 5, 2013 at 7:39
  • \$\begingroup\$ Any is perfect. "while there's anything there do this..." \$\endgroup\$ Commented Feb 2, 2016 at 16:58

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.