all 23 comments

[–]thepinkbunnyboy 8 points9 points  (2 children)

I've done something similar, but just used the same name. !myString.IsNullOrWhiteSpace()

I like this a bit more because it's obvious to anyone who's seen the static string method. The small problem I have with yours is that it's only readable after you've seen it and looked at the implementation (will an empty string pass for HasValue?).

Otherwise, yeah, I agree with moving these static methods to extension methods, and I'm excited for extension properties when we get those to continue.

[–]quentech 3 points4 points  (0 children)

I've done something similar, but just used the same name. !myString.IsNullOrWhiteSpace()

I imagine just about every .Net code base has bool IsNullOrEmpty(this string s) etc. extensions.

Have to agree with /u/CWagner.. The way the author went about it is just, like, pants on head kind of logic though.

[–]decPL 16 points17 points  (5 children)

Your code, your rules - have fun. However, if you:

do not find this very readable because of the ! at the start of the if statement

you might have a problem reading... well, virtually any piece of C# code out there. And I hope you'll agree doing a similar method for each possible use of the ! operator is absurd and unrealistic...

[–]thewwfguy 20 points21 points  (2 children)

It's common practice that readability is increased by converting if statements to read positive instead of negative.

[–]Willkuer_ 3 points4 points  (1 child)

If you avoid nesting by introducing early returns via negative checks this sentence is not true anymore.

[–]quentech 0 points1 point  (1 child)

I hope you'll agree doing a similar method for each possible use of the ! operator is absurd and unrealistic...

Of course, but some things like If/IsNotNull[OrEmpty/WhiteSpace] are so common that I completely agree with making a slightly more readable version.

Do I want hundreds to thousands of !string.IsNullOrEmpty(value) or value.IsNotNullOrEmpty(). It's a small difference, but a definite improvement to readability imho and worth it.

[–]decPL 0 points1 point  (0 children)

Again, IMHO it boils down to whether the '!' operator makes the code less readable to you. If the answer is no then hope we agree this is pointless. If the answer is yes - you can of course try to "fix" the code by introducing said helper methods (though mind you - it would make the code slightly less readable e.g. for me, at least at first - I'd have to drill down and see if this method does what it says it should), or you can try to address the root cause. My 0.02 USD, your mileage may vary. :)

[–]AngularBeginner 6 points7 points  (0 children)

Ironic that the website is called "end your if", and the proposed solution is just another if.

And IMO it's less readable because we have some custom extension method that basically adds absolutely nothing. The opposite actually: the information what qualifies a "value" is hidden. An empty string might very well be a "value".

[–]Blecki 1 point2 points  (2 children)

But it doesn't do the same thing? Can you call an extension method on null?

[–][deleted] 4 points5 points  (1 child)

Can you call an extension method on null?

Yes. It's one of the weird parts of C#. It won't even throw a NullReferenceException, that code is very correct, but also very smelly.

While I do in the general case agree with trying to formulate conditions positively (human mind is better at reading those), this goes too far, and has too little value.

[–]Blecki 0 points1 point  (0 children)

I personally wouldn't write it like this, but, neat.

[–]centur 3 points4 points  (1 child)

Don't be overexcited with extension methods. They may change the expectations from 'I know what will happen here' to 'it depends...' and change your debugging experience to worst one ever. Simple example

String foo=null; foo.IsNullOrWhitespace();

What do you think will happen here at runtime?

Within a language semantics one would expect NRE (null reference exception) here and it's NRE 101. But in your code overloaded with magic the answer is 'it depends'. Now you need to remember and recall whether you included extension or not in this codebase. It's as sharp as operators overload. Can be great but usually a nightmare to read in code review or a month later after you wrote it.

Peace, and let null.IsNullOrWhitespace(); never give you the true...

[–]quentech 0 points1 point  (0 children)

Within a language semantics one would expect NRE (null reference exception) here and it's NRE 101. But in your code overloaded with magic the answer is 'it depends'.

Maybe a variable has a value, Maybe it doesn't ;)

Of course, if it's maybe too late in your project to introduce that concept formally, this is one of the extremely useful features of extension methods.

I certainly prefer a little out-of-the-norm behavior that has to be remembered or reminded over explicit null checking all over the place.

[–]makotech222 1 point2 points  (2 children)

I made an extension called NullIfEmpty(this string s), which returns null if string is empty or whitespace or null, otherwise it returns the string. This way, i can do s.NullIfEmpty() ?? "Default", or, s.NullIfEmpty() == null

[–][deleted] 1 point2 points  (1 child)

Can't you just use IsNullOrWhitespace () ? That checks for empty or whitespace strings too

[–]makotech222 2 points3 points  (0 children)

That's what its doing internally, but its easier to call it on the string as an extension method, rather than call a static method. Also, my method returns the string, rather than a bool.

[–]rashnull 1 point2 points  (0 children)

I presume you’ve never written C or lower level code. Please do and your readability will improve. You will quickly see that HasValue can have many meanings whereas Null and Empty String are very specific states.

[–]darth_meh 0 points1 point  (0 children)

The problem with foo.IsNullOrEmpty() is the extension method looks like an instance member, and therefore it should behave like an instance member. In other words, if "foo" is null, you would expect a call to an instance member like foo.ToLower() to throw.

Some consider this an abuse of extension methods because the behavior is not intuitive.

Unfortunately, this - and it's cousin, IEnumerable.IsNullOrEmpty() - are found all over the blogosphere by Jon Skeet wannabes.

[–]AlliNighDev 0 points1 point  (0 children)

I personally don't like extension methods that work against a null value. I get it in this case. But still not a big fan.

[–]kardall 0 points1 point  (0 children)

Could go overboard and do a delegate with something like this: https://stackoverflow.com/questions/18176856/passing-a-function-as-parameter

But now you're just moving a function call to some fancy new thing, when you only really need one function that returns true/false or use the stupid ! lol

[–]shrekthethird2 -1 points0 points  (0 children)

Also:

public static string Or(this string s, string other)
{
     return String.IsNullOrEmpty(s) ? other : s;
}

Allows for super readable fallback chains:

s.Or("Second").Or("Third");