Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<p>The <a href="http://www.c2.com/cgi/wiki?BouncerPattern">Bouncer Pattern</a> describes usage of a method whose sole purpose is to either throw an exception (when particular conditions hold) or do nothing. Such methods are often used to defensively guard pre-conditions of a method.</p> <p>When writing utility methods, you should always guard against faulty input arguments. When writing internal methods, you may be able to ensure that certain pre-conditions always hold by having sufficient unit tests in place. Under such circumstances, you may reduce the desirability to have guards on your methods.</p> <p>Groovy differs from other languages in that you frequently use the <code>assert</code> method within your methods rather than having a large number of utility checker methods or classes.</p> <h3>Null Checking Example</h3> <p>We might have a utility method such as:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> class NullChecker { static check(name, arg) { if (arg == null) throw new IllegalArgumentException(name + " is null") } } </pre></td></tr></table> <p>And we would use it like this:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> public void doStuff(String name, Object value) { NullChecker.check("name", name); NullChecker.check("value", value); // do stuff } </pre></td></tr></table> <p>But a more Groovy way to do this would simply be like this:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> public void doStuff(String name, Object value) { assert name != null, 'name should not be null' assert value != null, 'value should not be null' // do stuff } </pre></td></tr></table> <h3>Validation Example</h3> <p>As an alternative example, we might have this utility method:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> public class NumberChecker { static final NUMBER_PATTERN = /\d+(\.\d+(E-?\d+)?)?/ static isNumber(str) { if (!str ==~ NUMBER_PATTERN) throw new IllegalArgumentException(/Argument '$str' must be a number/) } static isNotZero(number) { if (number == 0) throw new IllegalArgumentException('Argument must not be 0') } } </pre></td></tr></table> <p>And we would use it like this:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> def stringDivide(String dividendStr, String divisorStr) { NumberChecker.isNumber(dividendStr) NumberChecker.isNumber(divisorStr) def dividend = dividendStr.toDouble() def divisor = divisorStr.toDouble() NumberChecker.isNotZero(divisor) dividend / divisor } println stringDivide('1.2E2', '3.0') // => 40.0 </pre></td></tr></table> <p>But with Groovy we could just as easily use:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> def stringDivide(String dividendStr, String divisorStr) { assert dividendStr =~ NumberChecker.NUMBER_PATTERN assert divisorStr =~ NumberChecker.NUMBER_PATTERN def dividend = dividendStr.toDouble() def divisor = divisorStr.toDouble() assert divisor != 0, 'Divisor must not be 0' dividend / divisor } </pre></td></tr></table>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced