I’m not sure Exit works there as it’s meant to exit loops, but despite that
I like the first method unless it’s a really basic, simple method. I find that early exits can lead to hard to find bugs when you forget that it’s exiting early higher in the code.
[quote=153863:@Karen Atkocius]If Condition Then
Return 0
Else
Return 1
EndIf
This is even more concise.[/quote]
I should have presented it this way to be more concise, yep. Just did it by hand on my ipad. Thats what i was thinking about but with several conditions being checked.
[quote=153868:@Geoff Perlman]Or even more concise:
Return If(condition, 1, 0)
Ive never seen this style used before Geoff, any working examples ?
[quote=153864:@Kem Tekinay]I’m not sure Exit works there as it’s meant to exit loops, but despite that
[/quote]
Exit unfortunately has a dual nature http://documentation.xojo.com/index.php/Exit
I had read something a long time ago that stated there is only one point of entrance to the function and there should only be one return from the function… While Geoff’s code is the way I would do it in the newer versions of Xojo, those that are using older versions could do something like
DIM returnValue As Integer = 0
if (Condition) then returnValue = 1
Return returnValue
This way you never have to figure out where the function is returning from and your default value is set in the declaration statement…
[quote=153874:@shao sean]I had read something a long time ago that stated there is only one point of entrance to the function and there should only be one return from the function… While Geoff’s code is the way I would do it in the newer versions of Xojo, those that are using older versions could do something like
[/quote]
fail early
fail often
fail loudly
the one entry one exit point dictum leads you to some horrid constructions
[quote=153878:@Tomas Jakobs]Why do you not use booleans when you are using 1 and 0 and doing if…then… stuff?
could be much shorter then:
return Condition
or
return not Condition
It was my poor example from writing the code on my ipad quickly. The real scenario actually needs to check multiple conditions. I shall give myself a good talking to for suggesting boolean comparison when that was not the case.
Seems obvious for some, but let’s go. Your example:
Function f() As Integer
Dim ResultCode As Integer
If Condition = True Then
ResultCode = 0 // here it continues after "End If"
Else
ResultCode = 1 // here it continues after "End If"
End If
Return ResultCode // We still "in" the function after the IF THEN. :)
End Function
[quote=153882:@Stephen Thomas]Is there a way to leave an If… Then… Else without leaving the function ?
Only way i could think of so far is to put in a label afterwards then jump to it, but thats not a way i am keen on.[/quote]
Me either
But writing the conditional so you dont need exit is probably a good idea at any rate
You’ll notice that EXIT says
Exit [For | Do | While]
or
Exit [ For loopVariable]
or
Exit [ Sub | Function ]
But NOWHERE does it mention “EXIT IF”
No this is correct
And the code AS ORIGINALLY POSTED … never returns anything but 0
[code]Dim ResultCode As Integer
If Condition = True Then
ResultCode = 0
Exit // this behaves like a return but note RETURN would force you to have a RETURN X - exit doesnt Myabe it should?)
Else
ResultCode = 1
Exit // this behaves like a return but note RETURN would force you to have a RETURN X - exit doesnt Myabe it should?)
EndIf
Return ResultCode[/code]
Put a break point on the return result code and you will never hit it
Both code paths have an EXIT which leaves the function without executing the return
So this use of exit does NOT work any
To correct it would be
[code]Dim ResultCode As Integer
If Condition = True Then
ResultCode = 0
Else
ResultCode = 1
EndIf