I just spotted the following 2 methods in a piece of code:
public void ShowPanelWindow(bool isVisible)
{
Visibility = isVisible ? Visibility.Visible : Visibility.Collapsed;
}
public void ShowBusy(bool isBusy)
{
BusyIndicator.ShowIsBusy = isBusy;
}
And i cringed. Personally, i think it’s weird to read lines of code that say view.ShowPanelWindow(false) or view.ShowBusy(false). Instead, i’d go for something like this:
public void HidePanelWindow()
{
Visibility = Visibility.Collapsed;
}
public void ShowPanelWindow()
{
Visibility = Visibility.Visible;
}
public void LookBusy()
{
BusyIndicator.ShowIsBusy = true;
}
public void StopLookingBusy()
{
BusyIndicator.ShowIsBusy = false;
}
Sure, i’m not gonna win the fewest-lines-of-code contest but then again, we’re not participating in that contest anyway. And while my version is a little bit longer, it doesn’t take more than a few extra seconds to write that extra code, and the improved readability of the consuming code is more than worth it. After all, you do prefer reading view.HidePanelWindow() over view.ShowPanelWindow(false), no?
I’ve always liked the following approach to avoid (or at least, mimimize) bad method names. Just pretend that the classes are people and that the method names are messages between them. There is after all a reason why we referred to it as “sending a message to an object” originally in OO-terms. Give it a shot, and you’ll notice that your code will become more readable with hardly any extra effort.