!is tiny, is false is not!

A confusing blog title like this is to remind why using ! in programming sometimes confuses other readers of your program. Let alone confusing people, sometimes due to big screens, rushing code review, different fonts etc., it is easy to miss ! in your statement.

I have been practicing empty string checks in C# using following convention for better readability.

if(string.IsNullOrWhiteSpace(myValue) == false) doSomething();

However, recently I switched to following. I believe this is much easier to read.

if(string.IsNullOrWhiteSpace(myValue) is false) doSomething();

Most probably you are still using the below and this is not incorrect.
It is just a matter of taste…

if(!string.IsNullOrWhiteSpace(myValue)) doSomething();

Detailed deep dive can be found on official documentation:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: