Consider Enums Instead of Constants

Using constants can be error prone. You always need to validate that the value being passed is valid.

<pre class="prettyprint"><span class="kwd">class</span> <span class="typ">Suit</span> <span class="pun">{</span>        
    <span class="kwd">public</span> <span class="kwd">static</span> <span class="kwd">final</span> <span class="kwd">int</span><span class="pln"> CLUBS </span><span class="pun">=</span> <span class="lit">1</span><span class="pun">;</span>
    <span class="kwd">public</span> <span class="kwd">static</span> <span class="kwd">final</span> <span class="kwd">int</span><span class="pln"> SPADES </span><span class="pun">=</span> <span class="lit">2</span><span class="pun">;</span>
    <span class="kwd">public</span> <span class="kwd">static</span> <span class="kwd">final</span> <span class="kwd">int</span><span class="pln"> HEARTS </span><span class="pun">=</span> <span class="lit">3</span><span class="pun">;</span>
    <span class="kwd">public</span> <span class="kwd">static</span> <span class="kwd">final</span> <span class="kwd">int</span><span class="pln"> DIAMONDS </span><span class="pun">=</span> <span class="lit">4</span><span class="pun">;</span>
<span class="pun">}</span></pre>
<pre class="prettyprint"><span class="kwd">void</span><span class="pln"> method</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> suit</span><span class="pun">)</span> <span class="pun">{</span>
    <span class="kwd">if</span> <span class="pun">(</span><span class="pln">suit </span><span class="pun"><</span> <span class="lit">1</span> <span class="pun">||</span><span class="pln"> suit </span><span class="pun">></span> <span class="lit">4</span><span class="pun">)</span> <span class="pun">{</span>
        <span class="kwd">throw</span> <span class="kwd">new</span> <span class="typ">IllegalArgumentException</span><span class="pun">(</span><span class="str">"Invalid suit value: "</span> <span class="pun">+</span><span class="pln"> suit</span><span class="pun">);</span>
    <span class="pun">}</span>
    <span class="pun">...</span>
<span class="pun">}</span></pre>

What happens if we add a suit? What happens if we remove a suit? All the client code needs to be updated!

</pre>
<pre class="prettyprint"><span class="kwd">enum</span> <span class="typ">Suit</span> <span class="pun">{</span><span class="pln">     
    CLUBS</span><span class="pun">,</span><span class="pln"> SPADES</span><span class="pun">,</span><span class="pln"> HEARTS</span><span class="pun">,</span><span class="pln"> DIAMONDS
</span><span class="pun">}</span></pre>
<pre class="prettyprint"><span class="kwd">void</span><span class="pln"> method</span><span class="pun">(</span><span class="typ">Suit</span><span class="pln"> suit</span><span class="pun">)</span> <span class="pun">{</span>
    <span class="typ">Validate</span><span class="pun">.</span><span class="pln">notNull</span><span class="pun">(</span><span class="pln">suit</span><span class="pun">);</span>
    <span class="pun">...</span>
<span class="pun">}</span></pre>
<pre>

Client code:

</pre>
<pre class="prettyprint"><span class="pln">method</span><span class="pun">(</span><span class="typ">Suit</span><span class="pun">.</span><span class="pln">CLUBS</span><span class="pun">);</span></pre>
<pre class="prettyprint"><span class="kwd">import</span> <span class="kwd">static</span> <span class="pun">....</span><span class="typ">Suit</span><span class="pun">.*;</span>
    <span class="pun">...</span><span class="pln">
    method</span><span class="pun">(</span><span class="pln">CLUBS</span><span class="pun">);</span>
    <span class="pun">...</span></pre>
<pre>

Enums can also define their own instance variables and methods, so you can remove the if-else blocks from the clients and instead push specific behaviors or values to the enums.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s