2

I have an array of strings and I wish to find out if that array does not contain a certain string. I can use the not operator (!) in conjunction with the Contains method like so:

if (!stringArray.Contains(searchString))
{
    //do something
}

The not operator (!) might be overlooked when scanning the code so I was wondering if it was considered bad practice to create an Extension method in an attempt to enhance readability:

public static bool DoesNotContain<T>(this IEnumerable<T> source, T value)
{
    return !source.Contains<T>(value);
}

So now the code could read:

if (stringArray.DoesNotContain(searchString))
{
    //do something
}

Is this sort of thing frowned upon?

Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48
  • 1
    This attracted an incredible number of answers before I could even assemble one coherent thought: Avoid unnecessary complexity. Besides, if you're concerned people will overlook the `!` operator, surely throwing in generics and extension methods is not the solution. – Cody Gray - on strike Dec 01 '10 at 17:00
  • Because this question is subjective in nature, should this have been made a community wiki? I could not see how to do that. Do you have to have a certain level of reputation before you can make a community wiki? – Chris Dunaway Dec 01 '10 at 17:11

12 Answers12

4

Personally, I wouldn't make an extension method for something so simple. I understand that you're trying to keep it readable but most C# developers should catch the ! operator. It's heavily used and even beginners usually recognize it.

Jeff LaFay
  • 12,882
  • 13
  • 71
  • 101
  • I agree with this, the ! operator is something that should not really be overlooked, but also agree this is a personal preference. – jimplode Dec 01 '10 at 16:50
4

Keep the !. This is where a comment above the line would help readability.
(I suspect ! is more efficient)

//If the word is NOT in the array then...

Another point is to whether you are dead-set on using an Array? There is something (that you may or may not know about) called a HashSet.

If your sole purpose is to examine whether or not a string is in a list, you are essentially looking at set arithmetic.

Unless you are using the array for something other than finding out whether a certain term is in it or not, try using a HashSet...much faster.

funkymushroom
  • 2,079
  • 2
  • 26
  • 39
  • I need to do a case-insensitive search of the array, so I am using the overload that allows me to pass in an IEqualityComparer which seems to be an extension method of IEnumerable. Would I still derive any benefit in using HashSet in this instance? – Chris Dunaway Dec 01 '10 at 17:03
  • With a Hashset you would use the method: `Contains(T, IEqualityComparer)` so you can specify what type of comparer you want to use. More details here:http://msdn.microsoft.com/en-us/library/bb359438.aspx. Also, I just realized, HashSet does not store duplicates. If this is okay, then it is ideal for you. – funkymushroom Dec 01 '10 at 18:46
3

Seems unnecessary, !source.Contains<T>(value); is pretty readable. Also, using the existing Contains function means that your code will be more portable (i.e., it won't be dependent on your extension method being present).

Donut
  • 110,061
  • 20
  • 134
  • 146
3

I would definitely use the !stringArray.Contains(string). This what 99.9% of all developers use. DoesNotContain would confuse me at least.

BlueVoodoo
  • 3,626
  • 5
  • 29
  • 37
3

I think your question is based on a bit of a faulty premise. Namely that developers will read past the ! in your code. The ! boolean operator is a very well known operator in a large number of popular programming languages (C, C++, C#, Java, etc ...). Anyone who is likely to read past the ! on a regular basis probably shoudln't be checking in code without a heavy review before hand.

It feels like you`re saying the following

I want people to code in C# but I don't trust them to read it hence I'm going to create a new dialect in my code base with extension methods.

Why stop with the ! operator? It seems just as likely that they would miss the + in += expression or read a | as a ||.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
2

Never seen DoesNot* methods in .NET framework, so I think your problem with ! is overestimated.

Lex Lavnikov
  • 1,239
  • 9
  • 18
2

I guess this is a personnal choice more than a good/bad practice. IMO I like the extension methods since its more declarative thus more readable, at first glance you know exactly what it does. Just my 2 cents

pdiddy
  • 6,217
  • 10
  • 50
  • 111
2

This sounds like a bad idea, now the consumers of your code have to know about two methods (DoesNotContain and Contains) instead of just one. In general I would avoid XXNotXX methods.

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
1

I would personally make an extension method for that if I was going to be using that quite frequently within the project. If it is a one off then i wouldnt bother, but its not really bad practise.

The reason I would do it is because the if() has more context at a glance as to what is going on. Ok granted anyone with a brain cell would know what the current statement is doing, but it just reads nicer. Everyone will have their own preference then...

I made an extension method for formatting strings just to make the code flow better...

Grofit
  • 17,693
  • 24
  • 96
  • 176
1

When !something doesn't work, then fall back to something == false.

Evgeny Lazin
  • 9,193
  • 6
  • 47
  • 83
Darren Kopp
  • 76,581
  • 9
  • 79
  • 93
1

I prefer option 1 over option 2. Extension methods are very cool and are great to use for such things as conversions or comparisons that are used frequently. However, Microsoft does recommend to use extension methods sparingly.

dretzlaff17
  • 1,699
  • 3
  • 19
  • 24
1

I really would consider extension methods which does nothing else than negating an expression as bad practice.

What about:

if (stringArray.Contains(searchString) == false)
{
    //do something
}
codymanix
  • 28,510
  • 21
  • 92
  • 151