<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Better Preconditions &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/better-preconditions/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Fri, 06 Jan 2023 16:44:39 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>Better Preconditions &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>Better Preconditions: v0.1.0</title>
		<link>https://myshittycode.com/2015/05/13/better-preconditions-v0-1-0/</link>
					<comments>https://myshittycode.com/2015/05/13/better-preconditions-v0-1-0/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Wed, 13 May 2015 13:10:11 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Better Preconditions]]></category>
		<category><![CDATA[Java]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=711</guid>

					<description><![CDATA[<p>DEPENDENCY Introduction The goal of Better Preconditions is to provide a set of Java APIs that allows developers to create succinct, yet readable and testable preconditions. Why Write Preconditions? Let&#8217;s assume we have the following code:- Although this example is simple and trivial, every developer that looks at this code will interpret this API differently. [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2015/05/13/better-preconditions-v0-1-0/">Better Preconditions: v0.1.0</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">DEPENDENCY</h2>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;dependency&gt;
  &lt;groupid&gt;com.github.choonchernlim&lt;/groupid&gt;
  &lt;artifactid&gt;better-preconditions&lt;/artifactid&gt;
  &lt;version&gt;0.1.0&lt;/version&gt;
&lt;/dependency&gt;
</pre></div>


<h2 class="wp-block-heading">Introduction</h2>



<p>The goal of Better Preconditions is to provide a set of Java APIs that allows developers to create succinct, yet readable and testable preconditions.</p>



<h2 class="wp-block-heading">Why Write Preconditions?</h2>



<p>Let&#8217;s assume we have the following code:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; title: ; notranslate">
public void save(final String name, final LocalDate birthDate) {
    dao.save(new Entity(name.toUpperCase(), birthDate));
}
</pre></div>


<p>Although this example is simple and trivial, every developer that looks at this code will interpret this API differently. For example:-</p>



<ul class="wp-block-list">
<li>Can name be blank?</li>



<li>What if we pass in a null value for name?</li>



<li>Can birth date be null?</li>



<li>Can birth date be after today&#8217;s date?</li>
</ul>



<p>The truth of the matter is we cannot create an API that handles every possible scenario. Otherwise, we will never get our products out the door.</p>



<p>Thus, it is better to safeguard our API with a set of preconditions to ensure our fellow developers (or even you) know what this API needs before it performs the real work. Further, it provides a living documentation that would never go stale. Here&#8217;s an example of what the preconditions might look like:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; title: ; notranslate">
public void save(final String name, final LocalDate birthDate) {
    // precondition 1: name cannot be blank
    // precondition 2: birth date cannot be null
    // precondition 3: birth date cannot be after today&#039;s date

    dao.save(new Entity(name.toUpperCase(), birthDate));
}
</pre></div>


<h2 class="wp-block-heading">What&#8217;s wrong with Guava Preconditions?</h2>



<p>The biggest advantage of using Guava Preconditions is its flexibility. The biggest disadvantage of it is also its flexibility. Here&#8217;s an example written with Guava Preconditions:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; title: ; notranslate">
public void save(final String name, final LocalDate birthDate) {
    checkArgument(!nullToEmpty(name).trim().isEmpty(), &quot;Name cannot be blank&quot;);
    checkNotNull(birthDate, &quot;Birth date cannot be null&quot;);
    checkArgument(!birthDate.isAfter(LocalDate.now()), &quot;Birth date cannot be after today&#039;s date&quot;);

    dao.save(new Entity(name.toUpperCase(), birthDate));
}
</pre></div>


<p>A couple of observations:-</p>



<ul class="wp-block-list">
<li>Too verbose, which makes the code very messy. If we start inserting the raw values into the error messages, it becomes even messier.</li>



<li>We use <b>checkArgument(..)</b> and <b>checkNotNull(..)</b> most of the time. When an exception is thrown, we either get <b>IllegalArgumentException</b> or <b>NullPointerException</b>. This makes the API very confusing to unit test if there are many preconditions.</li>
</ul>



<h2 class="wp-block-heading">Introducing the power of Better Preconditions</h2>



<p>Let&#8217;s rewrite the example above with Better Preconditions:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; title: ; notranslate">
public void save(final String name, final LocalDate birthDate) {
    expect(name, &quot;Name&quot;)
            .not().toBeBlank()
            .check();

    expect(birthDate, &quot;Birth Date&quot;)
            .not().toBeNull()
            .not().toBeAfter(LocalDate.now(), &quot;Today&#039;s Date&quot;)
            .check();

    dao.save(new Entity(name.toUpperCase(), birthDate));
}
</pre></div>


<p>A couple of observations:-</p>



<ul class="wp-block-list">
<li>Short and succinct.</li>



<li>Each thrown exception provides very useful error message for debugging purpose. For example, if birth date is after today&#8217;s date, the error message would be <b>Birth Date [ 2015-02-01 ] must not be after Today&#8217;s Date [ 2015-01-01 ]</b></li>



<li>When one of the preconditions fails, a specific exception is thrown. For example:- <ul><li>blank name throws <b>StringBlankPreconditionException</b>null birth date throws <b>ObjectNullPreconditionException</b>birth date after today&#8217;s date throws <b>JodaTimeAfterPreconditionException</b></li></ul><p>This makes it simpler to unit test because we can easily catch specific exception without any doubts.</p></li>
</ul>



<h2 class="wp-block-heading">Conclusion</h2>



<p>Lastly, play around with it. This post barely scratches the surface of what Better Preconditions can do for you. If it works for you, great. If it doesn&#8217;t work for you and you are still interested to use it, create an issue at my GitHub page so that I can fix it. Due to my current real world workload, I&#8217;m using the 80/20 rule&#8230; the provided APIs should solve 80% of the problem.</p>



<p>To learn more about Better Preconditions, visit <a href="https://github.com/choonchernlim/better-preconditions" target="_blank" rel="noopener">https://github.com/choonchernlim/better-preconditions</a></p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2015/05/13/better-preconditions-v0-1-0/">Better Preconditions: v0.1.0</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2015/05/13/better-preconditions-v0-1-0/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">711</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching using Disk

Served from: myshittycode.com @ 2026-02-22 10:08:11 by W3 Total Cache
-->