Flipping a Boolean

This is a very quick post but something i see from time to time. Sometimes I run into code like this:

if (someBool == false)
{ 
      someBool = true;
} 
else
{ 
      someBool = false;
}

which works, but it looks ugly and clutters up your code. The easier way to do something like this, is to simply

someBool = !someBool;

The reason for this is simple. What is “Not false”? True. What is “Not True”. False. I am going to guess that there are some performance benefits to using this syntax but I have no research to back that up. Enjoy!

Leave a Reply

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s