<?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>Shaun Abram &#187; bestpractice</title>
	<atom:link href="http://www.shaunabram.com/tag/bestpractice/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.shaunabram.com</link>
	<description>Java and Technology weblog</description>
	<lastBuildDate>Thu, 09 Feb 2012 18:06:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>OSCON Day1: The Productive Programmer, part 2</title>
		<link>http://www.shaunabram.com/oscon-day1-productive-programmer2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=oscon-day1-productive-programmer2</link>
		<comments>http://www.shaunabram.com/oscon-day1-productive-programmer2/#comments</comments>
		<pubDate>Fri, 23 Jul 2010 03:12:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Events]]></category>
		<category><![CDATA[bestpractice]]></category>
		<category><![CDATA[nealford]]></category>
		<category><![CDATA[oscon]]></category>
		<category><![CDATA[productivity]]></category>

		<guid isPermaLink="false">http://www.shaunabram.com/?p=921</guid>
		<description><![CDATA[The following are my notes from the second part of Neal Ford&#8216;s &#8220;The Productive Programmer&#8221; talk on best practices (see here for part1 on mechanics). Again, you can get the original slides form here. Part2: Practice 10 ways to improve your code 1. Composed Method The concept of &#8216;Composed Method&#8216; Comes from Smalltalk Best Practices [...]]]></description>
			<content:encoded><![CDATA[<p>The following are my notes from the second part of <a href="http://nealford.com/">Neal Ford</a>&#8216;s &#8220;<a href="http://www.oscon.com/oscon2010/public/schedule/detail/13834">The Productive Programmer</a>&#8221; talk on best practices (see here for <a href="http://www.shaunabram.com/oscon-day1-productive-programmer1/">part1 on mechanics</a>). Again, you can get the original slides form <a href="http://nealford.com/downloads/conferences/Productive_Programmer_Tutorial(Neal_Ford).pdf">here</a>.</p>
<p><span id="more-921"></span></p>
<h1>Part2: Practice</h1>
<p>10 ways to improve your code</p>
<h2>1. Composed Method</h2>
<p>The concept of &#8216;<a href="http://c2.com/ppr/wiki/WikiPagesAboutRefactoring/ComposedMethod.html">Composed Method</a>&#8216; Comes from<a href="http://c2.com/cgi/wiki?SmalltalkBestPracticePatterns"> Smalltalk Best Practices Patterns</a>.<br />
The concept involves dividing your programs into methods that perform one identifiable task. If you find yourself chunking a method (using blank lines) you should probably be refactoring</p>
<h4>Benefits of composed methods</h4>
<ul>
<li>Shorter methods are easier to test</li>
<li>Method names become documentation</li>
<li>Large number of cohesive methods</li>
<li>Discover reusable assets</li>
</ul>
<h2>2. Test Driven Development</h2>
<p>(And Test Driven Design!)<br />
TDD is the first consumer of your code &#8211; it makes you think abut how the rest of the world will use it</p>
<ul>
<li>Creates consumption awareness</li>
<li>Forces mocking of dependent objects</li>
<li>Naturally creates composed method</li>
</ul>
<p>Extroverted objects (e.g. where you create dependent objects in theconstructor) &#8216;reach out&#8217; to create other objects, i.e. adhoc object creation<br />
Introverted objects move object construction to a few simple places. Dependency Injection is a way to achieve introverted objects.</p>
<h2>3. Static analysis</h2>
<ul>
<li><a href="http://findbugs.sourceforge.net/">Findbugs</a> &#8211; byte code analysis</li>
<li><a href="http://pmd.sourceforge.net/">PMD</a> &#8211; Scans Java source code and looks for potential problems</li>
<li><a href="http://pmd.sourceforge.net/cpd.html">CPD</a> &#8211; Copy Paste Detector</li>
</ul>
<h2>4. Good citizenship</h2>
<h4>a) getters &#038; setter != encapsulation</h4>
<p>If user code has getter &#038; setters, it can break encapsulation too!<br />
Create atomic mutators for dependent fields<br />
e.g. Address should have a single setAddress method, not setCity &#038; setState</p>
<p>Instead, only have getters &#038; setter when you need them for other methods.<br />
And you shouldn&#8217;t have to test getters &#038; setters &#8211; will be tested through other code.</p>
<h4>b) Constructors</h4>
<p>Specific contract for how to create valid objects<br />
Q: How often is a blank object valid?<br />
A: Never!<br />
Therefore, don&#8217;t provide default constructor for domain objects<br />
Push back on frameworks that require default constructors</p>
<h4>c) Static methods</h4>
<p>One good use for static methods is completely black box use. No state involved i.e. for stateless, stand alone methods.<br />
But if you mix static &#038; state, you have problems.<br />
e.g. Singletons!<br />
Singletons are bad because&#8230;<br />
1) Mix responsibility (creation and logic)<br />
2) Untestable<br />
3) The object version of global variable</p>
<p>Avoid using Singletons and instead<br />
1) Create a plain class<br />
2) create a factory to create POJO of that class</p>
<h2>5. Yagni</h2>
<p>You Aint Gonna Need It!</p>
<ul>
<li>Discourages gold plating</li>
<li>Don&#8217;t indulge in speculative development &#8211; as that increases software entropy</li>
<li>Leads to frameworks, in a very bad sense e.g. <a href="http://avalon.apache.org/closed.html">Avalon</a> is framework to build frameworks</li>
</ul>
<p>Frameworks extracted from working code can be good.<br />
Frameworks created as speculative development are usually bad.</p>
<h2>6. Question Authority</h2>
<p>Test names are special<br />
Use underscores instead of Camel case (only for test method names though!)<br />
Much easier to read</p>
<p>APIS<br />
Use fluent interfaces<br />
Makes code readable to non-developers<br />
But this vialoates the JavaBean spec<br />
(need default constructor, all set methods must return void)<br />
Therefore can&#8217;t use JavaBeans spec for fluent interfaces</p>
<p>Summary &#8211; Know when to break the rules</p>
<h2>7. Slap</h2>
<p>Single Layer of Abstraction Principle<br />
Keep all lines of code in a method at the same level of abstraction<br />
Jumping abstraction layers makes code hard to understand<br />
(See a blog posting on this <a href="http://www.markhneedham.com/blog/2009/06/12/coding-single-level-of-abstraction-principle/">here</a>)</p>
<h2>8. Polyglot programming</h2>
<p>Consider leveraging existing platforms with languages targeted at specific problems and applications. In other words, use the right tool to get the job done.<br />
e.g. Java is bad at threading, consider Jaskell or Scala<br />
e.g. If there is schedule pressure, consider JRuby on Rails, or Grails<br />
e.g. For every day tasks done quickly, consider Groovy or Ruby. or <a href="http://swiby.codehaus.org/">Swiby</a> (Ruby / Swing)</p>
<p>&#8220;Stop banging rocks together &#038; get some work done!&#8221;</p>
<h2>9. Learn every Nuance</h2>
<p>Learn all the quirks<br />
For example, consider:</p>
<ul>
<li>Using Reflection (no longer slower). Can give elegant solutions to problems.</li>
<li>Regular expressions</li>
</ul>
<p>Learn the nuances of your tools, then tell the other people on your project.</p>
<h2>10. Anti Objects</h2>
<p>An antiobject is a kind of object that appears to essentially do the opposite of what we generally think the object should be doing.<br />
Consider using them if they bring elegant solutions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shaunabram.com/oscon-day1-productive-programmer2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

