views:

191

answers:

4
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 this. The idea is to check something and if it isn't what you want then you stop code processing. I thought it was called "escape pattern", but Google isn't confirming that name.

+9  A: 

guard clause:

http://c2.com/cgi/wiki?GuardClause

eyston
This is exactly what I was thinking. Thank you for your brief and concise response, which is unlike my commented complement in return.
MADCookie
+4  A: 

Hmm...I've heard it called "early exit" (though mostly in the context of loops), but I'd consider it not so much a pattern as a technique.

As an aside, you could simplify your code by removing the "= True" in your conditional.

Private someSub()   
    If someBoolean Then Exit Sub
    ' do some great work because someBoolean is False
End Sub
Beska
Agreed, I font think this warrants being called a "Pattern" as its a fairly conscise technique
Neil N
+3  A: 

It's called a guard clause, and is typically used to do things like validate input to methods or ensure that the state of an object is in a fit state before continuing processing. Here's a typical sample:

public void DoMethod(MyObject item, int value)
{
  if (item == null || value == 0)
    return;

  // Do some processing...  
}
Pete OHanlon
+1  A: 

Just to mention - this is much safer with someBoolean passed in as an argument to a call

DroidIn.net