<?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/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>FuglyCode &#187; Fugly Code</title>
	<atom:link href="http://www.fuglycode.com/category/fugly-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fuglycode.com</link>
	<description>\fʌ-gli koʊd\ n: How not to code!</description>
	<lastBuildDate>Tue, 06 Apr 2010 04:14:22 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Multiple Nested If Statements</title>
		<link>http://www.fuglycode.com/2010/04/05/multiple-nested-if-statements/</link>
		<comments>http://www.fuglycode.com/2010/04/05/multiple-nested-if-statements/#comments</comments>
		<pubDate>Mon, 05 Apr 2010 17:00:55 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Fugly Code]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[fugly]]></category>
		<category><![CDATA[if]]></category>
		<category><![CDATA[nested]]></category>

		<guid isPermaLink="false">http://www.fuglycode.com/?p=298</guid>
		<description><![CDATA[This week&#8217;s fugly code comes from my past experiences as a C developer, and is actually one of the first pieces of advice my Team Lead gave me when I joined his group.
You see, I was working on this function that returned a value based on a few incoming parameters, and the lead admitted he [...]]]></description>
			<content:encoded><![CDATA[<p>This week&#8217;s fugly code comes from my past experiences as a C developer, and is actually one of the first pieces of advice my Team Lead gave me when I joined his group.</p>
<p>You see, I was working on this function that returned a value based on a few incoming parameters, and the lead admitted he was having difficulty reading the code.  It looked something like the following snippet:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p298code4'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2984"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code" id="p298code4"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> myFunction<span style="color: #009900;">&#40;</span> <span style="color: #993333;">int</span> a<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> b<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> c <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #808080; font-style: italic;">/* Initialize return value */</span>
  <span style="color: #993333;">int</span> returnValue <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* Determine return value */</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> a <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #339933;">||</span> b <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #339933;">||</span> c <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #808080; font-style: italic;">/* Provided values are invalid, return -1 */</span>
    returnValue <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">else</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> a <span style="color: #339933;">&gt;</span> b <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      returnValue <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> b <span style="color: #339933;">&gt;</span> c <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      returnValue <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> c <span style="color: #339933;">&gt;</span> a <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      returnValue <span style="color: #339933;">=</span> <span style="color: #0000dd;">3</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
      returnValue <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Do more stuff here</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* Return the value */</span>
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span> returnValue <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Lets take a minute to review this function&#8230;</strong></p>
<ul>
<li>At line 1, we begin our function and accept 3 integers</li>
<li>At line 4, we create a variable to hold the value our function will return upon completion</li>
<li>At line 7, we enter an if statement and check to see if any of the provided values are 0</li>
<li>If any of the values are 0, then we set our return value to -1</li>
<li>Otherwise, we enter our else block and determine our return value</li>
<li>At line 31, we have code that does other important stuff</li>
<li>Finally, at line 36, we return our return value</li>
</ul>
<p>&nbsp;</p>
<p><em>Note: the &#8220;other stuff&#8221; code at line 31 was not reproduced as we don&#8217;t really care what it does in this example, but we do need to know that it is there.</em></p>
<p>My Lead&#8217;s beef with the function above mostly had to do with lines 7 through 33.  Specifically, he deplored the fact that the if statement was the length of the function, and also questioned the reasoning behind letting the function continue its flow if the parameters were determined to be incorrect. (For example, if we add code to line 34, it will be run regardless of the parameters being valid or not.)</p>
<p>He explained that, although nested if statements could be useful from time to time, they should generally be avoided as they make code difficult to maintain.</p>
<p>So I asked him how I could avoid nested if statements, and he proposed that I simply return the -1 value at line 10, instead of assigning it to a variable and returning it later on.  This would simplify the function, and would remove the function-long if statement:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p298code5'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2985"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code" id="p298code5"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> myFunction<span style="color: #009900;">&#40;</span> <span style="color: #993333;">int</span> a<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> b<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> c <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #808080; font-style: italic;">/* Initialize return value */</span>
  <span style="color: #993333;">int</span> returnValue <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* Validate Parameters */</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> a <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #339933;">||</span> b <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #339933;">||</span> c <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #808080; font-style: italic;">/* Provided values are invalid, return -1 */</span>
    <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* Determine return value */</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> a <span style="color: #339933;">&gt;</span> b <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    returnValue <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> b <span style="color: #339933;">&gt;</span> c <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    returnValue <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> c <span style="color: #339933;">&gt;</span> a <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    returnValue <span style="color: #339933;">=</span> <span style="color: #0000dd;">3</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">else</span>
  <span style="color: #009900;">&#123;</span>
    returnValue <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Do more stuff here</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* Return the value */</span>
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span> returnValue <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And there we go.  By immediately returning the value -1, we can remove the else block and avoid putting the whole function in an if statement.  </p>
<p>This might not seem like much right now, but one day you might run into a very large function with multiple lines of code and multiple if statements that persist throughout the function.  And on that day you&#8217;ll hit your forehead with your hand and wonder why the function&#8217;s author didn&#8217;t read FuglyCode.com <img src='http://www.fuglycode.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  </p>
<p><strong>Function Optimization</strong></p>
<p>This function could be even more optimized.  Can you figure out how?</p>
<p>Try rewriting this function to make it as optimal as possible and then compare what you wrote with my code (click on the down arrow to expand the code box).  Feel free to share your code in the comments if its any different from mine!</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p298code6'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2986"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code" id="p298code6"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> myFunction<span style="color: #009900;">&#40;</span> <span style="color: #993333;">int</span> a<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> b<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> c <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #808080; font-style: italic;">/* Validate Parameters */</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> a <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #339933;">||</span> b <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #339933;">||</span> c <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #808080; font-style: italic;">/* Provided values are invalid, return -1 */</span>
    <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* Call new doMoreStuff function */</span>
  doMoreStuff<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/* Return a value */</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> a <span style="color: #339933;">&gt;</span> b <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span> <span style="color: #0000dd;">1</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> b <span style="color: #339933;">&gt;</span> c <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span> <span style="color: #0000dd;">2</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> c <span style="color: #339933;">&gt;</span> a <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span> <span style="color: #0000dd;">3</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span> <span style="color: #0000dd;">0</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> doMoreStuff <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #808080; font-style: italic;">/* Move the do more stuff into its own function *
   * to increase reusability and readability */</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.fuglycode.com/2010/04/05/multiple-nested-if-statements/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<series:name><![CDATA[FuglyCode of the Week]]></series:name>
	</item>
		<item>
		<title>Error Handling vs Method Overloading</title>
		<link>http://www.fuglycode.com/2010/03/22/error-handling-vs-method-overloading/</link>
		<comments>http://www.fuglycode.com/2010/03/22/error-handling-vs-method-overloading/#comments</comments>
		<pubDate>Mon, 22 Mar 2010 17:00:54 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Fugly Code]]></category>
		<category><![CDATA[exceptions]]></category>
		<category><![CDATA[fugly]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.fuglycode.com/?p=152</guid>
		<description><![CDATA[Error handling in programming languages allows software developers to build safeguards into their code to protect against possible exceptions or errors during runtime.  Method overloading, on the other hand, allows developers to build multiple methods of the same name, but that act differently depending on their method signatures (ie: different parameters).
Lets take the following [...]]]></description>
			<content:encoded><![CDATA[<p>Error handling in programming languages allows software developers to build safeguards into their code to protect against possible exceptions or errors during runtime.  Method overloading, on the other hand, allows developers to build multiple methods of the same name, but that act differently depending on their method signatures (ie: different parameters).</p>
<p>Lets take the following snipped of FuglyCode for example:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p152code9'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1529"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code" id="p152code9"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> Tester<span style="color: #009900;">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> myObject <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> myString<span style="color: #339933;">;</span>
  <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainteger+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Integer</span></a> myInt<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">try</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Save the string</span>
    myString <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> <span style="color: #009900;">&#41;</span> myObject<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Do more String stuff...</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> e <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// The object was not a string</span>
    <span style="color: #666666; font-style: italic;">// Save the integer</span>
    myInt <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainteger+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Integer</span></a> <span style="color: #009900;">&#41;</span> myObject<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Do more Integer stuff...</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">return</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p style="text-align: center;"><em>Special thanks to <strong>jpuly</strong> for submitting this code.  <a href="http://www.fuglycode.com/contact-us/" target="_self">Submit your fugly code here</a>.</em></p>
<p>Before we begin analyzing the fuglyness of this code, lets go over it to make sure we all understand what seems to be going on:</p>
<ul>
<li>At line 1, we enter our Tester method and receive an object</li>
<li>At line 7, we attempt to save our object to a String variable</li>
<li>At line 11, we catch any exception of type <em>Exception</em> (pretty much, we catch any exception)</li>
<li>At line 15, we assume that the exception was caused by saving the object to a string, so we save our object to an Integer variable</li>
</ul>
<p></p>
<p>One of the biggest problems with this code is that, although we accept any Exception type, we assume that the only reason our code would throw an exception would be because of the String cast on line 7.  What if the myObject parameter is a String, and code does something on line 9 that throws an exception?</p>
<p>Obviously the exception will be caught and the catch block will be run.  Because myObject is a string, however, the Integer cast on line 15 will throw an exception, and since we don&#8217;t have any code to handle and exception thrown at that line, our program will crash and burn.</p>
<p>So what could you do?  Well, you could begin by using a much more specific Exception type.  In fact, changing line 11 to &#8220;catch ( ClassCastException e )&#8221; would ensure that we only enter the catch block if the String cast on line 7 failed, although it wouldn&#8217;t protect against other exceptions being thrown, or against myObject being something other than a String or an Integer.</p>
<div id="attachment_225" class="wp-caption alignright" style="width: 201px"><a href="http://www.fuglycode.com/wp-content/uploads/2010/03/kiss.jpg"><img class="size-full wp-image-225" title="Keep It Simple, Stupid!" src="http://www.fuglycode.com/wp-content/uploads/2010/03/kiss.jpg" alt="Keep It Simple, Stupid!" width="191" height="88" /></a><p class="wp-caption-text">KISS!</p></div>
<p>In this particular situation, we need to take a step back and look at the method as a whole.  What are we trying to accomplish here?  Does the code have to be so complicated? (Remember KISS!)</p>
<p>Truth is, our Tester method is using error handling to make a decision (if myObject is a String, do something, otherwise, do something else).  Not only is this poor software design, but it fails to capitalize on the programming language&#8217;s built in ability to overload methods.</p>
<p>So, lets scrap the error handling altogether, and split this method into two similar methods with differing signatures:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p152code10'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15210"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p152code10"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> Tester<span style="color: #009900;">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> myString <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Do String stuff... (optionally wrap in try/catch block)</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> Tester<span style="color: #009900;">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainteger+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Integer</span></a> myInt <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Do Integer stuff... (optionally wrap in try/catch block)</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And there we go!  Now we have two methods with the same names (meaning our calling code doesn&#8217;t have to change).  We also specify our parameter types, meaning that the compiler will not allow us to call Tester with anything other than either an Integer or a String.  And as a final and optional touch, we can add error handling at lines 2 and 7, in case these lines risk throwing an exception.</p>
<p>If you have any questions, feel free to post them in the comments section below.</p>
<p>If you would like to ask your question privately, you can send it using the <a href="http://www.fuglycode.com/contact-us/">Contact Us form</a>.</p>
<p>Do you have any FuglyCode you would like to share?  If so, then <a href="http://www.fuglycode.com/contact-us/">submit it</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fuglycode.com/2010/03/22/error-handling-vs-method-overloading/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<series:name><![CDATA[FuglyCode of the Week]]></series:name>
	</item>
	</channel>
</rss>

