What is the best way of writing code to exit a function in case condition is not satisfied?
e.g I have a function
-(IBAction) moreDetails
{
if (condition)
//condition not satisfied...exit function
else
continue with the function
}
Can i simply write return ?
Yes. "return" returns immediately from the current method/function. If the function/method returns a value then you need to provide a return value: "return NO, return 3, return @"string", and so on.
I generally prefer this structure:
void f()
{
if ( ! conditionCheck )
return;
// long code block
}
to this:
void f()
{
if ( conditionCheck )
{
// long code block
}
}
because fewer lines are indented
Yes - you should use return. Because your method returns void, no need for anything else. I'd write more, but there's not much else to it :)
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