Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this pattern called?

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.

like image 234
MADCookie Avatar asked Nov 26 '25 23:11

MADCookie


2 Answers

guard clause:

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

like image 173
anonymous Avatar answered Nov 28 '25 14:11

anonymous


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
like image 21
Beska Avatar answered Nov 28 '25 14:11

Beska