developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See the Winners!


Developer Jobs

Be a Commerce Partner
Build a Server Rack
KVM Switches
Calling Cards
Memory
Find Software
Rackmount LCD Monitor
Car Donations
Web Hosting Directory
PDA Phones & Cases
Domain registration
GPS Devices
Phone Cards
Online Shopping
Promote Your Website

 

Click Here
Web Devs:
Moonlight as a Game Developer and Win Cool Prizes by Accepting the RIA Run Challenge

Now, your mission--should you choose to accept: Take your shot at gaming stardom if you think you might have what it takes to build a cool RIA game and you could win an Xbox 360 or other fabulous prizes. Hurry! You only have until May 15, 2008 to enter. »

 
Article:
Leveraging Your Flash Development with Silverlight

You're not giving up Flash any time soon (and we don't blame you.) But if you could get your Flash application working in Silverlight, why wouldn't you? We show you the tools and techniques required to have your rockin' Flash application rolled for Silverlight. Learn more here. »

 
Article:
What Does it Take to Build the Best RIA?

With the proliferation of Rich Interactive Application (RIA) platform choices out there, you no longer have to take a one-size-fits-all approach to developing your next RIA application. Knowing the strengths (and weaknesses) of each platform can help you to decide the best RIA for your next application. »

 
Developer News -
Sun Latest to Help App Vendors Get 'SasSy'    April 24, 2008
Ubuntu's 'Hardy' Cozy With Windows    April 24, 2008
The $4.6B Business of The Social    April 22, 2008
Office 2007 Fails The OOXML Test    April 22, 2008
Free Tech Newsletter -

Project Management Guide: Developing a Web Site. Best Practices, Tips and Strategies. Download Exclusive eBook Now.

Considering Test-After Development
By Jeff Langr

Go to page: 1  2  3  Next  

In this article, I'll build a solution twice. First, I'll write the code for a "reverser" method, a simple bit of code that reverses the order of words in an input string. I'll then use the stricter technique known as test-driven development to re-build the solution.

Test-After Development

In test-after development (TAD), I use my skills as an experienced developer to write my code. I might sometimes write a test prior to development. Predominantly, however, I will first code a solution, and then look to refactor as necessary. I might code additional tests once my solution is in place, but that's my prerogative.

A first test, if I bother to code one, represents a shot at the whole ball of wax. For this example, my first test reverses a full sentence, a test case that will require the final, real logic to get it to pass. Or, at least what I think is the final required logic.

import static org.junit.Assert.*;
import org.junit.*;

public class ReverserTest {
   @Test
   public void reverse() {
      Reverser reverser = new Reverser();
      String text = "This is a short sentence";
      assertEquals("sentence short a is This",
                   reverser.reverse(text));
   }
}

The test takes me about 45 seconds to write. I have to think a bit about the reverse-order loop logic as I code a solution. My total time to get the test to pass is about 120 seconds.

public String reverse(String text) {
   StringBuilder buffer = new StringBuilder();
   String[] words = text.split(" ");
   for (int i = words.length - 1; i >= 0; i--) {
      buffer.append(words[i]);
      if (i > 0)
         buffer.append(' ');
   }
   return buffer.toString();
}

After coding, I reflect on my solution. I recognize that I should probably worry about the case where words are separated by more than a single space character. I code a solution without worrying about the tests. I'm confident that adding a call to the trim method, a small, simple change, will suffice.

public String reverse(String text) {
   StringBuilder buffer = new StringBuilder();
   String[] words = text.split(" ");
   for (int i = words.length - 1; i >= 0; i--) {
      buffer.append(words[i].trim());
      if (i > 0)
         buffer.append(' ');
   }
   return buffer.toString();
}

Ahh. Ready to ship, right? No. My experience tells me that something isn't quite right with the solution. My gut tells me to write an additional test. The test says that the string returned by the reverser method should have eliminated additional spaces.

import static org.junit.Assert.*;
import org.junit.*;

public class ReverserTest {
   private Reverser reverser;

   @Before
   public void initialize() {
      reverser = new Reverser();
   }

   @Test
   public void reverse() {
      String text = "This is a short sentence";
      assertEquals("sentence short a is This",
                   reverser.reverse(text));
   }

   @Test
   public void reverseWithExtraSpaces() {
      String text = "This is a  short sentence";
      assertEquals("sentence short a is This",
                   reverser.reverse(text));
   }
}

Time to write the test: about 30 seconds (including refactoring to a common setup method). The test fails. The actual response returns the extra space between "short" and "a."

Expected: sentence short a is This
Actual:   sentence short  a is This

Racking my brain for a few seconds, I remember that the split method takes a regular expression. I need to split on any grouping of one or more spaces, not just a single space:

public String reverse(String text) {
   StringBuilder buffer = new StringBuilder();
   String[] words = text.split("\\s+");    // <- change
   for (int i = words.length - 1; i >= 0; i--) {
      buffer.append(words[i].trim());
      if (i > 0)
         buffer.append(' ');
   }
   return buffer.toString();
}

Well, now that I think about it a bit harder, the call to trim is no longer necessary. I might not have noticed. And if I hadn't built a test, I probably wouldn't bother removing this redundancy even if I had noticed it.

Think time + solution time: 30 seconds.

Both tests pass. Ship it!

It was a good thing that I used my experience to remember to write a test for such a trivial change. Are there other tests that I need to write? Maybe I should look at the code a bit longer...

Go to page: 1  2  3  Next  

Next article: Testability and Design


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Architecture & Design Archives

Data Sheet: IBM Information Server Blade
Flash Demo: Learn how IBM Information Server Blade is easy to manage, highly scalable and efficient.
Best Practices for Developing a Web Site. Checklists, Tips & Strategies. Download Exclusive eBook Now.
Whitepaper: Embeddable Content Platform for OEM's
Is it time to make your move to the multi-threaded and parallel processing world? Find out!



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES