One of the solutions to your problem could be polymorphism and delegating the // Do this thing
, to the Baz
class.
Then changing the implementation of the foo
method to something like this:
private Norf foo(Baz baz)
{
// ...
// Logic on baz
// ...
baz.doThisThing();
// ...
// More logic
// ...
return norf;
}
And having multiple implementations of the Baz class.
class BlueBaz extends Baz
{
public void doThisThing()
{
Console.WriteLine("Yay, this is right. Blue is the color of men!");
}
}
class AnyOtherBaz extends Baz
{
public void doThisThing()
{
throw new Exception("Sorry, no blue color in this one. Perhaps you like a different one.");
}
}
And if you feel like you would still need an if
check in the new children of the Baz
class, in the new doThisThing
method, you can delegate the decision even further, perhaps to a Color
class.
The pattern you are seing here is called inversion of control. It basically means there is a predefined contract by a public API, mostly determined by interfaces, but sometimes determined by base (abstract) classes. The contract is set and can be used, but the implementations are chosen during runtime, when you are constructing the object graph.
About the naming convention
If you delegate the condition further down the road (or classes respectively), you will not really need to have an ifACondition
in the name of your method, because the method itself will not contain the if
you are worried about. To handle the if
, you will have a completely separate class, having the doThisThing
method but providing different implementation, different logic.
In your own case you would be left with the name doThisThing
and decide which class would be chosen by constructing the desired instance (perhaps by a factory or in a boostrap being responsible for the construction of the object graph).
Aditional note
Take into consideration this is a possible approach, but not the only one. If you do not want to have conditionals in your code, polymorphism is the way to go in OO programming. But if you are fine with the alternative router when the color is blue and are satisfied with how the code works so far, you could leave it as it is.