The case goes as following: You have a Boolean property called FullScreenEnabled. You enter some method, and the code within this method is executed iff FullScreenEnabled is true. Which of the 2 approaches below do you use in your everyday programming:
private bool FullScreenEnabled { get; set; }
// Check if FullScreenEnabled is false and return;
private void Case1()
{
if (FullScreenEnabled == false)
{
return;
}
// code to be executed goes here!
}
// Surround the code by an if statement.
private void Case2()
{
if (FullScreenEnabled)
{
// code to be executed goes here!
}
}
private void MyMethod(bool arg){
if(arg)
return;
//do stuff
};
(for voting)
I generally prefer the first version (bailing at the start of the method). It leads to less nesting, which slightly increases readability. Should you decide you don't need to check for the condition in the future, it's also easier to remove the if condition in the first version, especially if you have several such checks. Plus, it could be easily be written in a single line: if (!FullScreenEnabled) return;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With