<?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>OcpSoft &#187; Best Practices</title> <atom:link href="http://ocpsoft.com/cat/best-practices/feed/" rel="self" type="application/rss+xml" /><link>http://ocpsoft.com</link> <description>&#34;Simple Software&#34;</description> <lastBuildDate>Tue, 27 Jul 2010 15:07:50 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0</generator> <item><title>Static Analysis tools &gt; Experience</title><link>http://ocpsoft.com/best-practices/static-analysis-tools-experience/</link> <comments>http://ocpsoft.com/best-practices/static-analysis-tools-experience/#comments</comments> <pubDate>Tue, 12 May 2009 12:12:03 +0000</pubDate> <dc:creator>Derek</dc:creator> <category><![CDATA[Best Practices]]></category> <category><![CDATA[Dreyfus model]]></category> <category><![CDATA[JTest]]></category> <category><![CDATA[PMD]]></category> <category><![CDATA[Static Analysis]]></category><guid
isPermaLink="false">http://ocpsoft.com/?p=51</guid> <description><![CDATA[When developers choose to enforce Static Analysis tools (i.e. JTest or PMD) they might have the best intentions at heart, but are actually making the code worse in the long run.  The value of the programmer&#8217;s experience and expertise on the code is not considered. Static analysis tools are automated programs that run a set [...]]]></description> <content:encoded><![CDATA[<p>When developers choose to enforce Static Analysis tools (i.e. JTest or PMD) they might have the best intentions at heart, but are actually making the code worse in the long run.  The value of the programmer&#8217;s experience and expertise on the code is not considered.</p><p>Static analysis tools are automated programs that run a set of rules against source code to produce metrics, and give advice on best practices.</p><p>So why does using these tools lead to bad code?  <span
id="more-51"></span></p><h3>The Heartache</h3><p>You&#8217;re trying to finish up some down-to-the-wire coding to get it over to your Testers, and your deadline is approaching.  You have this one last class to change and it is a legacy class.  This class was written before the static analysis tools were forced to be run on the code base.  You go in to make a simple change like adding a null pointer check.  You now execute the static analysis tool on the code and you have 30 violations &#8212; from incorrect naming of fields to Cyclomatic Complexity greater than the threshold.  You have 2 options here: A) You take the time to fix all the violations, and leave the code better than you got it or B) You take the lazy / easier solution and try and beat the rules &#8212; take the easy way out and work around them.  What do you choose?</p><h3>Reaction to the pain</h3><p>Depending on what option you chose, you left the code beautiful, a little bit better, or in shambles and worse than you received it.  By choosing option A you decided to be Pragmatic and make the code fix the Cyclomatic Complexity.  Now being the pragmatic programmer you are you need to consider unit testing.  You take the time to understand the violating method and make the appropriate refactoring.  Now you hope the developer before you would have tested this method.  A lot of the times this is not the case or if it is the case, did they write a good test? Did they write a test just to pass the code coverage standard?  Again you have options, you can either take the time to use TDD or you make the refactoring without the test and hope you don&#8217;t break anything.</p><p>If you chose option B, you decided to hack the code to bypass the rule.  By circumventing the system you are making the code worse, harder to understand, and more likely broken.  You figured out that by extracting pieces of the Cyclomatic Complexity method into other methods bypasses the rule.  This doesn&#8217;t truly fix the Cyclomatic Complexity it just hides it from the tool.  Again you have to worry about test or not to test.</p><p>Lets look at another less extreme example, you&#8217;re now forced to fix a rule that is complaining because the code uses the &#8216;!&#8217; operand more than max two times mandated by the rule.  You might change the code to &#8220;== false&#8221; or flip the condition blocks so that it results in true and use the else to catch the false.  This does work on a case by case basis.  However sometimes flipping the if expression to check for true first makes the code harder to read.   By making this change it loses the developers intention.  You are probably making the code worse by trying to beat the system or making the rule happy.  Is the code still easily understood by making this change?  Taking the Dreyfus Model into account will help us determine why a developer would respond in these different ways.</p><h3>Dreyfus Model</h3><p>By using the Dreyfus Model we can rate developers into 5 different categories: Novice, Advanced Beginner, Competent, Proficient, and Expert.  Novices are driven by rules and need guidance to complete tasks.  While the Experts are passed rules and use their intuition to drive their decisions.  Something to note about Experts is they can&#8217;t explain why they do the things the way they do.</p><p>A Novice that is driven by rules will likely fix the problem without hesitation or they could just be lazy and don&#8217;t want to push themselves to do the exploratory fix.  Now with Experts they will question the rule violation against their code.  Experts will not just blindly fix the error if they believe their code is correct / a work of art.  This is not their ego talking, but more their experience / intuition / instinct on the subject.  Not all developers are made equal, so why do we think code should be? The Expert will likely not be able to verbally prove their reason for wanting the violation to be suppressed &#8212; to them, it&#8217;s just clearer and better their way.  By not trusting their instinct you are degrading them.  They are likely going to strike and push back against this rule, toying with the Enforcer.  Forcing rules on the Expert is an incorrect way to treat them and their knowledge.  Rule violations shouldn&#8217;t be blindly enforced but taken on a case by case basis.</p><h3>Learning Tool</h3><p>I do believe that Static Analysis tools make a great learning tool towards best practices.  Novices, Advanced Beginners could get a lot of benefit from seeing what they did incorrectly when they violated a rule, and learning from it as they progress in their career.</p><p>A lot of time you are doing maintenance on code someone else wrote before the tool was enforced to be run.  Now you are held accountable for the fixing the rule when you just came into this class to add a new field.  First off this leads to developers to not wanting to make code changes even though they know it is for the best.  Also you are now held accountable from someone else&#8217;s debt.  The original developer might have moved to another team, and hasn&#8217;t seen the debt they have incurred.  Now the original developer can&#8217;t learn from this experience.  Hopefully when a rule violation is found against someone else&#8217;s code it will be shown to them so they get to see the debt they incur.  Developers should be taught to write good code in the first place, but without good mentors that is going to be hard.  Especially when the Experts can&#8217;t explain why they do the things they do.</p><p>I don&#8217;t think that Static Analysis tools should replace code reviews and agile pair programming.  Code reviews should still be done, and require at least another set of eyes on each line of code written.  This will help foster the mentorship and help less experienced programmers with a more experienced developer.</p><h3>Conclusion</h3><p>Use Static Analysis tools as a guidelines not a mandate.  Use these tools as training guides for younger developers, and for the advice they are built to give.  Enforce rules on a case by case basis not at an overall perspective.  By enforcing these tools it causes developers to rebel, make the code worse, and not to want to make code changes because they are going to be responsible for fixing the rule that they might have good reason to disagree with &#8212; passionately.</p><p>Please let us know how have you dealt with these tools or situations in the past.  What pain have you felt from these tools?</p><p><em>References:</em></p><p>Dreyfus model:  <a
href="http://www.infoq.com/presentations/Developing-Expertise-Dave-Thomas">Developing Expertise: Herding Racehorses, Racing Sheep</a> by Dave Thomas</p> ]]></content:encoded> <wfw:commentRss>http://ocpsoft.com/best-practices/static-analysis-tools-experience/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>Universal Context of Computer Programming</title><link>http://ocpsoft.com/best-practices/universal-context-of-computer-programming/</link> <comments>http://ocpsoft.com/best-practices/universal-context-of-computer-programming/#comments</comments> <pubDate>Tue, 16 Dec 2008 23:13:09 +0000</pubDate> <dc:creator>Lincoln</dc:creator> <category><![CDATA[Best Practices]]></category> <category><![CDATA[Releases]]></category><guid
isPermaLink="false">http://ocpsoft.com/?p=33</guid> <description><![CDATA[The only thing that stays the same, in our field of Computer Science, is change itself. Do not try to predict what will happen in the future; instead know that the future will bring change, and that you will need to adapt to it. Design systems which are capable of change, and you will be [...]]]></description> <content:encoded><![CDATA[<p
style="margin-bottom: 0in;">The only thing that stays the same, in our field of Computer Science, is change itself. Do not try to predict what will happen in the future; instead know that the future will bring change, and that you will need to adapt to it.</p><p
style="margin-bottom: 0in;">Design systems which are capable of change, and you will be much more ready for the future.</p><p
style="margin-bottom: 0in;"><span
id="more-33"></span></p><h3><em>Always:</em></h3><ul><li>Program by Intention – 		“begin 	by focusing on motivation or desire”</li><li>Analyze the the varying objects and forces in your system, encapsulate them</li><li>Favor aggregation/delegation over inheritance</li><li>Seek the Open-Closed principle &#8211; 		“open to extension, closed to modification.”</li><li>Design to interfaces &#8211; 		“couple 	to interfaces, not implementation”</li><li>Encapsulate the constructor &#8211; 		use Object.getInstance()</li><li>Encapsulate variation &#8211; &#8220;what is hidden from you, can be changed”</li><li>Think about how you test a design, 	when you create it.</li><li>When choosing between two designs 	with similar cost/risk, consider how easy/difficult it would be to 	refactor from each to the other, should the need arise.</li></ul><h3><em>Think:</em></h3><p>Think about the forces of the problem (Do we want to use this pattern / design?)</p><ol><li>Contextual forces – How will the environment around you and your product affect your system/design?</li><li>Implementation forces – Will this design work? Is it worth it? Is there a better way?</li><li>Consequences – What will the results of our actions be? What are the trade-offs?</li></ol><p>Think about how you test a design, when you create it. When choosing between two designs with similar cost/risk, consider how easy/difficult it would be to refactor from each to the other, should the need arise.</p><p
align="center">&#8220;Good coupling is <em>Intentional</em>, bad coupling is unnecessary or <em>Accidental</em>&#8220;</p><h3><em>Never:</em></h3><ul><li>Never allow Redundancy &#8211;  		&#8220;Redundancy is <em>DEBT</em>; you always pay for it later&#8221;</li><li>Never use ‘new Object()&#8217; 	notation &#8211; 	Use <a
href="http://www.springframework.org">dependency injection</a> / factories / encapsulation.</li></ul><h3>Coupling Forces:</h3><ol><li>Identity &#8211; 		One type coupled to 	the fact that another type exists.</li><li>Representational &#8211; 	One type 	coupled to the interface of another.</li><li>Inheritance &#8211; 		Coupling to a 	superclass. Changes propagate downward.</li><li>Subclass &#8211; 		Knowledge that an 	object is part of a polymorphic set&#8230; &#8220;knowing the implementation 	of another.&#8221;</li></ol> ]]></content:encoded> <wfw:commentRss>http://ocpsoft.com/best-practices/universal-context-of-computer-programming/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>My father speaks on logging</title><link>http://ocpsoft.com/best-practices/my-father-speaks-on-logging/</link> <comments>http://ocpsoft.com/best-practices/my-father-speaks-on-logging/#comments</comments> <pubDate>Tue, 09 Dec 2008 22:23:59 +0000</pubDate> <dc:creator>Lincoln</dc:creator> <category><![CDATA[Best Practices]]></category> <category><![CDATA[Logging]]></category><guid
isPermaLink="false">http://ocpsoft.com/?p=32</guid> <description><![CDATA[After reading a recent article on logging, and when you should and shouldn&#8217;t do it, I asked my father for his views. He has about 25 years of experience in both small companies and large corporations, and got me thinking about some things that I hadn&#8217;t before. Here is his message: &#8212;- &#8220;This is a [...]]]></description> <content:encoded><![CDATA[<p>After reading a recent article on logging, and when you should and shouldn&#8217;t do it, I asked my father for his views. He has about 25 years of experience in both small companies and large corporations, and got me thinking about some things that I hadn&#8217;t before.</p><p><span
id="more-32"></span></p><h3>Here is his message:</h3><p
style="text-align: center;">&#8212;-</p><p
style="text-align: justify; padding-left: 30px; padding-right: 30px;"><em>&#8220;This is a subject about which there are (obviously) religious wars.  If you don&#8217;t have it when you need it, you regret it, because you end up spending days or months adding it, to find some problem.</em></p><p
style="text-align: justify; padding-left: 30px;padding-right: 30px;"><em>If you have it, its use needs to be controlled.  Program flow tracing, or logging variable and argument values, (DEBUG level IMO) is something that one should be able to turn on and off at will (potentially at a subsystem, transaction type, service call, class hierarchy, or function/method call level), and should go to separate log files.  The cost of this level of logging can be huge (in both performance and system resources&#8230; CPU _and_ DASD, as well as analysis resources).</em></p><p
style="text-align: justify; padding-left: 30px;padding-right: 30px;"><em>Logging inputs and outputs of services, where transactions MUST succeed, is a very good idea, if one is to be able to recover from system errors, and one does not have another &#8220;back channel path or reconciliation interface&#8221; that guarantees you can recover.</em></p><p
style="text-align: justify; padding-left: 30px;padding-right: 30px;"><em>When you have business requirements for measuring and reporting and system and component performance, sometimes down the the transaction level, you need a mechanism for doing this that is FAST, failure tolerant, AND does not involve locking exclusions.  The mechanism we developed, involved dropping messages into an MQ Series Q, and then having a listener (Message Delivery Bean) on the Q, pick up the messages and write them  to a database, which is then dumped nightly and sent to an analitic data store.  Data is keep in the operational database (for triage purposes for 45 days on average). If the MQPUT operation fails, the system reports an exception (logged to file system, and sent to an operations monitoring console), and then continues.</em></p><p
style="text-align: justify; padding-left: 30px;padding-right: 30px;"><em>Simple systems need less logging than more complex systems.  And don&#8217;t forget that logging at least service inputs and outputs, can be a good way to prove to a client L2 or L3 triage team that inputs to your service were bad, or proving that the outputs were good.  Similarly for called services (as a client).  Logging the outputs and inputs, enables one to prove a services was called correctly, but generated bad results or failed.  Needless to say, all logs for any transactions, need unique transaction identifiers (TraceIDs) typically, UUIDs a good choices for this.&#8221;</em></p><p
style="text-align: center;">&#8212;-</p><h3>It runs in the family:</h3><p>My father(and mother) has always challenged me to evaluate all sides of a situation, and in the end, I believe his most important message is that every system has different requirements, and every system will require different levels/degrees of logging. You need to analyze what is right for <em>you</em>, because if you mimic someone else&#8217;s system blindly, you will surely be missing some chances to get a logging system in place that provides what you need without becoming needlessly cumbersome.</p><p>I&#8217;ve tried to take this to heart when working on all of my projects with OcpSoft, and my own pet project of <a
href="http://ocpsoft.com/prettyfaces" target="_self">PrettyFaces</a> for JSF; needless to say, it&#8217;s hard what we do. There&#8217;s a lot to think about, and in order to be at our best, we have to continuously analyze as many angles as possible. Logging is no exception.</p> ]]></content:encoded> <wfw:commentRss>http://ocpsoft.com/best-practices/my-father-speaks-on-logging/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Replacing Commented Code with Delegated Code</title><link>http://ocpsoft.com/java/replacing-commented-code-with-delegated-code/</link> <comments>http://ocpsoft.com/java/replacing-commented-code-with-delegated-code/#comments</comments> <pubDate>Sat, 02 Aug 2008 00:49:26 +0000</pubDate> <dc:creator>Lincoln</dc:creator> <category><![CDATA[Best Practices]]></category> <category><![CDATA[Java]]></category><guid
isPermaLink="false">http://ocpsoft.com/?p=6</guid> <description><![CDATA[Today&#8217;s subject is a well commented square root approximation method. Imagine that this method is buried deep in a very messy Java class. How can we make sure that this code is reusable and that our comments don&#8217;t become out of date as our code changes? /** * Approximate the square root of n, to [...]]]></description> <content:encoded><![CDATA[<p>Today&#8217;s subject is a well commented square root approximation method. Imagine that this method is buried deep in a very messy Java class. How can we make sure that this code is reusable and that our comments don&#8217;t become out of date as our code changes?</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Approximate the square root of n, to within the specified tolerance,
 * using the Newton-Raphson method. This method takes two arguments:
 * @param Double n The number to be square-rooted
 * @param Double tolerance the error tolerance
 * @return Double result of square root operation
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Double</span> approximateSquareRoot<span style="color: #009900;">&#40;</span><span style="color: #003399;">Double</span> n, <span style="color: #003399;">Double</span> tolerance<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">Double</span> root <span style="color: #339933;">=</span> n <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Math</span>.<span style="color: #006633;">abs</span><span style="color: #009900;">&#40;</span>root <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>n <span style="color: #339933;">/</span> root<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> tolerance<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        root <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0.5</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>root <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>n <span style="color: #339933;">/</span> root<span style="color: #009900;">&#41;</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;">return</span> root<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p><span
id="more-6"></span>It’s my philosophy that if you find yourself writing a comment that describes what a method does, either the method does too much, or you should use that comment AS the name of the method. Try something like this:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Double</span> newtonRaphsonSqRoot<span style="color: #009900;">&#40;</span> <span style="color: #003399;">Double</span> number, <span style="color: #003399;">Double</span> tolerance <span style="color: #009900;">&#41;</span></pre></div></div><h3>Method Delegation:</h3><p>If you need to conform to a standard interface, then delegate! <a
href="http://http://en.wikipedia.org/wiki/Delegation_%28programming%29" target="_blank">Delegation</a> is your best friend.</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Double</span> approxSquareRoot<span style="color: #009900;">&#40;</span> <span style="color: #003399;">Double</span> number <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">return</span> newtonRaphsonSqRootApprox<span style="color: #009900;">&#40;</span> number, <span style="color: #cc66cc;">0.0001</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>Also&#8230; if you don&#8217;t want to add a private delegated method, then consider abstracting that behavior into an Operation class that performs the square root.</p><h3>Object Delegation:</h3><p>This strategy even lets you change behavior of your square root operations at runtime, at configuration time, and it lets you extend the behavior to use other algorithms without changing your client code. Just make sure that you only expose your interface, because as soon as your clients know about the implementing class, you defeated the purpose of delegating in the first place.</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> SquareRoot
<span style="color: #009900;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Double</span> perform<span style="color: #009900;">&#40;</span><span style="color: #003399;">Double</span> input<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Approximate the square root of n, to within the specified tolerance,
 * using the Newton-Raphson method.
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NewtonRaphsonApproximation <span style="color: #000000; font-weight: bold;">implements</span> SquareRoot
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Double</span> tolerance <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0.001</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Double</span> perform<span style="color: #009900;">&#40;</span> <span style="color: #003399;">Double</span> input <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">Double</span> root <span style="color: #339933;">=</span> n <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Math</span>.<span style="color: #006633;">abs</span><span style="color: #009900;">&#40;</span>root <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>n <span style="color: #339933;">/</span> root<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> tolerance<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            root <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0.5</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>root <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>n <span style="color: #339933;">/</span> root<span style="color: #009900;">&#41;</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;">return</span> root<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>See, we can even throw in a brief comment where it makes sense to store it.</p><h3>The Factory:</h3><p>Look! Now if you want to change the algorithm that calculates your square roots, all you have to do is create a new class that implements SquareRoot, and configure a <a
href="http://en.wikipedia.org/wiki/Factory_method_pattern" target="_blank">Factory</a> to provide you with an instance of that class. The possibilities are limitless, really. You can select which type of square root you want to use via a configuration file, dynamic logic, anything:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//where OperationFactory decides which type of SquareRoot to provide.</span>
&nbsp;
SquareRoot squareRoot <span style="color: #339933;">=</span> OperationFactory.<span style="color: #006633;">getSquareRoot</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">Double</span> result <span style="color: #339933;">=</span> squareRoot.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">123.2</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>Now it doesn&#8217;t matter that we are using <a
href="http://en.wikipedia.org/wiki/Newton%27s_method" target="_blank">NewtonRaphson</a> or <a
href="http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Taylor_series" target="_blank">TaylorApproximation</a> implementations of SquareRoot. And you still have clear understandable code under the covers.</p><h3>Final Thoughts:</h3><p>There are some who will say in response to this article, &#8220;you now have extra classes and methods that are making your code harder to understand,&#8221; and I say to them:</p><p><em>It all depends on the problem you are solving.</em></p><p>If you are using the newtonRaphsonSquareRoot() method in one place in your entire program, then you might be OK to leave it the way it is, sure. But as soon as you start using that method in other places, you might want to consider making a class to contain it anyway. Don&#8217;t just copy and paste your code. Cut and paste in a new class!</p><p>The next phase, the Factory, would come when you start using different types of SquareRoot approximations. Factories help hide the details of creation and implementation from the client. Hiding implementation is almost always good.</p><p>My article is a response to: <a
href="http://blog.uncommons.org/2008/07/25/no-your-code-is-not-so-great-that-it-doesnt-need-comments/" target="_blank">this blog</a>. Comment away, so to speak.</p> ]]></content:encoded> <wfw:commentRss>http://ocpsoft.com/java/replacing-commented-code-with-delegated-code/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> </channel> </rss>
<!-- Served from: ocpsoft.com @ 2010-07-29 17:04:16 by W3 Total Cache -->