What is the name of this template?
Private someSub()
If someBoolean = True Then Exit Sub
' do some great work because someBoolean is False
End Sub
I know there is a name for that. The idea is to test something, and if that's not what you want, you stop processing the code. I thought it was called the "escape pattern", but Google does not confirm this name.
+2
source to share
4 answers
Hmm ... I've heard it's called "early exit" (albeit mostly in the context of loops), but I would consider it not so much a pattern as a technique.
As an aside, you can simplify your code by removing "= True" in your conditional statement.
Private someSub()
If someBoolean Then Exit Sub
' do some great work because someBoolean is False
End Sub
+4
source to share
It invokes a guard offer and is typically used to perform actions such as validating method inputs or ensuring that the state of an object is in an appropriate state before processing continues. Here's a typical example:
public void DoMethod(MyObject item, int value)
{
if (item == null || value == 0)
return;
// Do some processing...
}
+3
source to share