I am writing an application and I got to this point:
private void SomeMethod()
{
if (Settings.GiveApples)
{
GiveApples();
}
if (Settings.GiveBananas)
{
GiveBananas();
}
}
private void GiveApples()
{
...
}
private void GiveBananas()
{
...
}
This looks pretty straight-forward. There are some conditions and if they are true the methods are being called. However, I was thinking, is it rather better to do like this:
private void SomeMethod()
{
GiveApples();
GiveBananas();
}
private void GiveApples()
{
if (!Settings.GiveApples)
{
return;
}
...
}
private void GiveBananas()
{
if (!Settings.GiveBananas)
{
return;
}
...
}
In the second case, each of the methods guards itself, so even if any of those methods GiveApples
or GiveBananas
is called from outside SomeMethod
, they are going to be executed only if they have the correct flag in Settings.
Is this something that I should actually consider as a problem?
In my current context, it is very unlikely that those two methods will be called from outside this method, but no one can ever guarantee that.