1

I was wondering if I can shorten this:

bool Check()
 {
 return textBox1.Text.All(char.IsDigit) ? true : Falsepath();
 }

 bool Falsepath()
 {
 MessageBox.Show("The data you entered is incorrect","Error",MessageBoxButtons.OK);
 return false;
 }    

To something like this:

    bool Check()
        { 
        return textBox1.Text.All(char.IsDigit) ? true : (sender, e) => 
                {
                MessageBox.Show("The data you entered is incorrect", "Error", MessageBoxButtons.OK); 
                return false;
                };
        }

Of course, this second code I entered is not correct, but I'm using it as an example.

So, can I execute a code while checking something or do I have to use separate funtions?

Andi Abrudan
  • 104
  • 4

3 Answers3

2

You could write:

bool Check()
{
    return textBox1.Text.All(char.IsDigit) ? 
           true : 
           ((Func<bool>)(() =>
           {
               MessageBox.Show("The data you entered is incorrect", "Error", MessageBoxButtons.OK);
               return false;
           }))();
}

But it is terrible, please don't do it!...

Sadly in C# you have to explicitly tell the compiler the type of an anonymous function. This makes everything more complex. See the cast to (Func<bool>)? In Javascript you wouldn't need it and in fact in that language it is a common pattern. In C# it isn't because it is unreadable and ugly.

Note the final () to execute the anonymous method.

Note that in this particular case you could write:

bool Check()
{
    return textBox1.Text.All(char.IsDigit) ? 
           true : 
           MessageBox.Show("The data you entered is incorrect", "Error", MessageBoxButtons.OK) == DialogResult.Abort;
}

so call the MessageBox.Show() and compare its result in a way that the comparison is false.

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • Clever, and terrifying. What is the `()` syntax called in C#? It isn't an IIFE I would imagine, since that's a Javascript concept. – David L May 09 '15 at 16:03
  • @DavidL It is the standard method execution. If there was a parameter, it would be `(foo)`. That part is the same in JS. – xanatos May 09 '15 at 16:04
  • Ahh, right, I see it now. You're right, it IS unreadable. The syntax completely confused me. – David L May 09 '15 at 16:05
  • @DavidL It confuses even non-expert JS programmers :-) See for example http://stackoverflow.com/a/9091416/613130 for an even more evil version in Javascript with the `!` :-) – xanatos May 09 '15 at 16:08
  • @David L () => Lambda expression () are the parameters – Toumash May 09 '15 at 16:08
1

Do you really need the the ternary operator?

bool Check()
{
    if (textBox1.Text.All(char.IsDigit))
    {
        return true;
    }
    else
    {
        MessageBox.Show("The data you entered is incorrect","Error",MessageBoxButtons.OK);
        return false;
    }
}
Kapol
  • 6,383
  • 3
  • 21
  • 46
0

It's pain to read and maintain such code. On top level you have ternary operator, which is loaded into the brain (working memory) of developer. Then you add lambda expression, which also should be loaded. Then you add some functionality which notifies user.

So I need to keep in brain that I'm showing error dialog inside lambda function which is part of ternary operator which checks whether all chars somewhere are digits. And all this staff happens in method call context where you checking something (you are already in the middle of some functionality).

Average person can keep about 7 things in the working memory. If you add more information, you start to forget previous data. Why make methods so complicated if you can keep them simple? Simple methods will allow you to keep more high-level context in the brain.

Another issue is confusing method name, which should only check something. It is not supposed to notify user or do other actions. And give meaningful names to methods and controls.

bool IsSocialSecurityNumberValid(string ssn)
{
    return ssn.All(char.IsDigit);
}

And call this method:

if (!IsSocialSecurityNumberValid(ssnTextBox.Text))
   MessageBox.Show("SSN should contain only digits", "Error", MessageBoxButtons.OK);
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • Thing is, this check IS actually for a social security number. I'm just trying to give meaningful feedback to the user (instead of displaying a message box that says "You data you entered is incorrect") to a form that has 10 textboxes – Andi Abrudan May 09 '15 at 16:28
  • @AndiAbrudan sorry, didn't get you – Sergey Berezovskiy May 09 '15 at 16:31
  • I'm saying I have a lot of stuff going on on the form. I'm just trying to be user friendly and give meaningful feedback, not display a message when something is wrong and let the user figure out what he entered incorrectly – Andi Abrudan May 09 '15 at 16:38
  • @AndiAbrudan still don't get you. It does not matter you have 1 textbox or 100. Complex code will not reduce number of textboxes – Sergey Berezovskiy May 09 '15 at 16:41