Home > Enterprise >  Shorten redundant switch statements
Shorten redundant switch statements

Time:12-15

I got this block of code from a solution I google for a problem I was searching regarding spaghetti if..else statements. Is there a way to shorten this at all or a different approach to make the code more maintainable atleast.

            switch (registerControl.Valid_FullName(student.Student_Name) == true)
            {
                case true:
                    lblFullNameError.Visibility = Visibility.Hidden;
                    break;
                case false:
                    lblFullNameError.Visibility = Visibility.Visible;
                    break;
            }

            switch (registerControl.Valid_Email(student.Student_Email) == true)
            {
                case true:
                    lblEmailError.Visibility = Visibility.Hidden;
                    break;
                case false:
                    lblEmailError.Visibility = Visibility.Visible;
                    break;
            }

            switch (registerControl.Valid_Course(student.Student_Course) == true)
            {
                case true:
                    lblCourseError.Visibility = Visibility.Hidden;
                    break;
                case false:
                    lblCourseError.Visibility = Visibility.Visible;
                    break;
            }

            switch (registerControl.Valid_Password(student.Student_Password) == true)
            {
                case true:
                    lblPasswordError.Visibility = Visibility.Hidden;
                    break;
                case false:
                    lblPasswordError.Visibility = Visibility.Visible;
                    break;
            }

            switch (registerControl.Confirm_Password(student.Student_Password, student.student_ConfirmPassword) == true)
            {
                case true:
                    lblPasswordMatch.Visibility = Visibility.Hidden;
                    break;
                case false:
                    lblPasswordMatch.Visibility = Visibility.Visible;
                    break;
            }

CodePudding user response:

You can convert your switch statement to a one-liner, using ternary operator,

lblFullNameError.Visibility = registerControl.Valid_FullName(student.Student_Name)  
                  ? Visibility.Hidden : Visibility.Visible;

You can apply same logic for rest of the switch blocks.

CodePudding user response:

In addition to replacing the switch statement with the ternary operator (as shown in Prasad's answer), you can also encapsulate the logic into a method:

void ToggleError(bool isDataValid, Label errorLabel)
{
    errorLabel.Visibility = isDataValid ? Visibility.Hidden : Visibility.Visible;
}

and use it as follows:

ToggleError(registerControl.Valid_FullName(student.Student_Name), lblFullNameError);
ToggleError(registerControl.Valid_Email(student.Student_Email), lblEmailError);
...

This way, you don't have to repeat Visibility.Hidden and Visibility.Visible in every single line. It's also more maintainable: If you ever want to change the way you display error messages, you only need to modify a single method.

  • Related