How many times we have written such code to enable/disable controls, based on some conditions:
if (string.IsNullOrEmpty(textBoxFind.Text))
{
buttonFindNext.Enabled = false;
buttonReplace.Enabled = false;
buttonReplaceAll.Enabled = false;
}
else
{
buttonFindNext.Enabled = true;
if (string.IsNullOrEmpty(textBoxReplace.Text))
{
buttonReplace.Enabled = false;
buttonReplaceAll.Enabled = false;
}
else
{
buttonReplace.Enabled = true;
buttonReplaceAll.Enabled = true;
}
}
When the same code can be expressed in a more clear and clean format:
bool isFindTextPresent = !String.IsNullOrEmpty(textBoxFind.Text); bool isReplaceTextPresent = !String.IsNullOrEmpty(textBoxReplace.Text); buttonFindNext.Enabled = isFindTextPresent; buttonReplace.Enabled = (isFindTextPresent && isReplaceTextPresent); buttonReplaceAll.Enabled = (isFindTextPresent && isReplaceTextPresent);