technology

Let the computer do the logic

I was writing some code to restrict the input to a NumericUpDown and getting a little confused by the logic. I wanted to allow any digits or the minus sign for negatives. But in order to get this behavior, I need to do something when this is not true. So, rather than applying DeMorgan’s in my head, I wrote out how I was thinking about it, and let ReSharper do it for me.

:::csharp
// Restrict numeric up down to integers
private void numericUpDown_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsDigit(e.KeyChar) || e.KeyChar == ‘-‘)
{

}
else
{
e.Handled = true;
}
}

After apply the “Invert ‘if'” transformation, that became:

:::csharp
if (!char.IsDigit(e.KeyChar) && e.KeyChar != ‘-‘)
{
e.Handled = true;
}

It’s just a small, and trivial, example of letting the computer do what it’s good at and applying yourself to do what you’re good at–using tools.

Advertisements
Standard

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