Ben Tsai

Let The Computer Do It

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.

[code lang=“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; } }[/code]

After applying the `Invert ‘if’` transformation, that logic became:

[code lang=“csharp”]if (!char.IsDigit(e.KeyChar) && e.KeyChar != ‘-’) { e.Handled = true; }[/code]

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.

Wednesday, December 7, 2011