01.02.2013 Views

Software Development Cross Solution - Index of - Free

Software Development Cross Solution - Index of - Free

Software Development Cross Solution - Index of - Free

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

etter code through testing<br />

Testing produces better code<br />

We’ve been working on testing, but writing tests first has done more than just<br />

test our system. It’s caused us to organize code better, keeping production<br />

code in one place, and everything else in another. We’ve also written simpler<br />

code—and although not everything in the system works yet, the parts that do<br />

are streamlined, without anything that’s not absolutely required.<br />

And, because <strong>of</strong> the tight coupling between our system’s business logic and<br />

database code, we implemented a design pattern, the strategy pattern. Not<br />

only does this make testing easier, it decouples our code, and even makes it<br />

easy to work with different types <strong>of</strong> databases.<br />

So testing first has gotten us a lot <strong>of</strong> things:<br />

OrderProcessor<br />

- mDBAccessor : DBAccessor<br />

+ setDBAccessor(DBAccessor) : void<br />

+ processOrder(orderInfo : OrderInformation) : Receipt<br />

300 Chapter 8<br />

Well-organized code. Production code is in one place; testing code is<br />

in another. Even implementations <strong>of</strong> our database access code used for<br />

testing are separate from production code.<br />

Code that always does the same thing. Lots <strong>of</strong> approaches to<br />

testing result in code that does one thing in testing, but another in<br />

production (ever seen an if (debug) statement?). TDD means<br />

writing production code, all the time.<br />

Loosely coupled code. Tightly coupled systems are brittle and<br />

difficult to maintain, not to mention really, really hard to test. Because<br />

we wanted to test our code, we ended up breaking our design into a<br />

loosely coupled, more flexible system.<br />

Ever heard your computer science pr<strong>of</strong>essor or lead architect talking about<br />

low coupling and high cohesion? This is what they were talking about. We<br />

have low coupling because <strong>of</strong> our use <strong>of</strong> interfaces and the strategy pattern,<br />

and we’ve got high cohesion by having our database and business logic code<br />

concentrated into separate but well defined classes.<br />

Remember the single responsibility<br />

principle?<br />

OrderProcessor has the business logic to<br />

handle an order, and doesn’t worry about<br />

databases. So it’s got high cohesion.<br />

Because <strong>of</strong> the interface approach<br />

<strong>of</strong> the strategy pattern, you’ve<br />

reduced the coupling between the<br />

OrderProcessor and your DB code.<br />

TestDBAccessor<br />

Download at WoweBook.Com<br />

+ getGC(gcId : int) :GiftCard<br />

+ saveGC(card : GiftCard) :void<br />

+ saveOrder(orderInfo : OrderInformation)<br />

Our test uses a testingspecific<br />

implementation<br />

<strong>of</strong> DBAccessor, but the<br />

order processor runs the<br />

same code, because <strong>of</strong><br />

our strategy pattern, in<br />

testing or in production.<br />

<br />

DBAccessor<br />

+ getGC(gcId : int) :GiftCard<br />

+ saveGC(card : GiftCard) :void<br />

+ saveOrder(orderInfo : OrderInformation)<br />

MySqlDBAccessor<br />

These accessors worry about<br />

database access, and only database<br />

access. That’s high cohesion.<br />

+ getGC(gcId : int) :GiftCard<br />

+ saveGC(card : GiftCard) :void<br />

+ saveOrder(orderInfo : OrderInformation)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!