Avoiding EXIT Statement

ayozzhero

Well-known member
Joined
Apr 6, 2005
Messages
186
Location
Malaysia
Programming Experience
1-3
I learned that using EXIT statement is best to be avoided. However, I do not see the way how it should be implemented without putting so many codes to replace the EXIT statement.

Given a very simple example for a save button procedure:
VB.NET:
if user types nothing
	notify user that nothing is changed
	do nothing
end if

if what user types is the same as the existing one
	notify user that nothing is changed
	do nothing
end if

if user types restricted words
	notify user
	do nothing
end if

apart from that
ask user for confirmation

if user clicks ok
	save the form
	reset form
	notify user
else
	do nothing
end if

From what I see, the easiest way is to put Exit Sub for do nothing. This way, the execution stops straight away because no further process is necessary. If I want to avoid the Exit Sub, I would have to put a boolean/variable as replacement, and do some more codes to handle the logic.

In such a simple procedure like this, it might not be a big hassle to create a True/False condition (boolean), but imagine a procedure that has so many logics. However, putting Exit Sub in in so many places raises cost in other ways. It is hard to trace the culprit of an error since 'one entry has many doors to exit'.

How do I balance between them, or what practice should I take actually. Any opinion is most welcomed.

Thank you.
 
Doesn't this accomplish the same thing:
VB.NET:
if user types nothing
	notify user that nothing is changed
else if what user types is the same as the existing one
	notify user that nothing is changed
else if user types restricted words
	notify user
else
	ask user for confirmation

	if user clicks ok
		save the form
		reset form
		notify user
end if
Exit statements exist for a reason, so you should not necessarily avoid them altogether, but they are inefficient so it is better to avoid them if possible. Also, you should use Return instead of Exit Sub because it is more efficient. Only use Exit to break out of a loop for which there is no better way. Basically, I think it would always be possible to avoid using Exit, but in some cases it would mean nesting so many If statements that the code would be quite ugly. In that case I'd use an Exit, but for most others I avoid it.
 
Yes, it accomplish the same thing. But as you said, it looks ugly for having so many nested if. Currently, if I am to write so many ElseIf, I would prefer Exit Sub.
 
I have no issue with ElseIf. That is not nested If statements. This is nested If statements:
VB.NET:
If condition1 Then
	If condition2 Then
		If condition3

		End If
	End If
End If
That gets difficult to manage because your code can end up being indented quite a bit and it can become difficult know exactly where you are within the If hierarchy. I think ElseIf statements are an efficient and relatively elegant way to accomplish certain tasks. If you are determined to structure your code like you original post then, as I said, you should use Return rather than Exit Sub because the IL produced is more efficient.
 
Back
Top