Exiting early

I came across a few posts this evening regarding exit methods; just wondering what the opinion is on these 2 examples:

Dim ResultCode As Integer

If Condition = True Then
  ResultCode = 0
  Exit
Else
  ResultCode = 1
  Exit
EndIf

Return ResultCode

And then this:

If Condition = True Then Return 0 Else Return 1 EndIf

Is there a preferred way ?

The second example is far easier to read and understand. It also probably generates less code when compiled.

If Condition Then Return 0 Else Return 1 EndIf

This is even more concise.

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.

Whatever works better for you.

there is no need to specify exit in a IF/THEN/ELSE… it just becomes unnecessary overhead and confusion

Or even more concise:

Return If(condition, 1, 0)

Ah yes, the new If()
Good example of where that is useful.

[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 :frowning:
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…

How about that. I’ve never seen that used like that, but there you go.

[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

Just found an example :slight_smile:

[quote=153873:@Norman Palardy]Exit unfortunately has a dual nature :frowning:
http://documentation.xojo.com/index.php/Exit[/quote]

I found this earlier too, thats why i went looking to begin with.

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 

or

 return (x = y) 

I forget when it came into being but it was a VERY long time ago

I never use it and prefer return and leave exit for loops etc

[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. :slight_smile:

[quote=153879:@Norman Palardy]I forget when it came into being but it was a VERY long time ago

I never use it and prefer return and leave exit for loops etc[/quote]

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.

Used everywhere. Specially filling data fields like (not tested, just for example):

Dim Status As Text = If (ShipDate.SecondsFrom1970<Date.Now.SecondsFrom1970, “Shipped”, “To be shipped”)

This type of constructions are known as ternary operations in many languages, but here is presented as a “conditional expression”.

Seems obvious for some, but let’s go. :slight_smile: 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

Return ResultCode[/code]