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.

focus on functionality<br />

We could fix code...<br />

Now it’s time to figure out what needs to be fixed. At the end <strong>of</strong><br />

Chapter 10 you took a look at the Mercury Meals code, and the<br />

prognosis was not good...<br />

394 Chapter 11<br />

// Mercury Meals class continued...<br />

// Follows the Singleton design pattern<br />

public class MercuryMeals<br />

{<br />

public MercuryMeals meallythang;<br />

private Order cO;<br />

{<br />

public boolean submitOrder(Order cO)<br />

try {<br />

MM mm = MM.establish();<br />

mm.su(this.cO);<br />

catch (Exception e)<br />

{ // write out an error message } return false; }<br />

public Order[] getOrdersThatMatchKeyword(String qk)<br />

}}<br />

Order o = new Order[];<br />

try {<br />

o = MM.establish().find(qk, qk);<br />

} catch (Exception e) {<br />

}<br />

return null;<br />

return o;<br />

No documentation on any <strong>of</strong> this class’s methods!<br />

Something that described what teh methods are<br />

supposed to do would make life a LOT easier for you<br />

Still no documentation...<br />

No wonder the s<strong>of</strong>tware gave no indication<br />

whether it was working or not (except by just<br />

hanging...) This method swallows all exceptions<br />

that are raised. This is a classic exception<br />

anti-pattern! If an exception gets raised, and<br />

you can’t deal with it locally then pass the<br />

exception up to teh caller so they can at least<br />

do something about it<br />

private String qk = "select * from order-table where keywords like %1;";<br />

public MercuryMeals() {<br />

}<br />

public static MercuryMeals getInstance()<br />

{<br />

}<br />

this.meallythang = new MercuryMeals();<br />

return this.instance;<br />

// TODO Really should document this at some point... TBD<br />

public Order createOrder {<br />

return new Order();}<br />

public MealOption getMealOption(String option)<br />

throws MercuryMealsConnectionException {<br />

}<br />

if (MM.establish().isAnyOptionsForKey(option))<br />

{ return MM.establish.getMealOption(option).[0] };<br />

return null;<br />

No real documentation on the class, other<br />

than the fact that it tries to implement<br />

the Singleton pattern...<br />

Not the most descriptive <strong>of</strong><br />

attribute names.<br />

Why is there an Order attribute? Even a<br />

few comments would help.<br />

Surely this should be a constant? And does qk<br />

make any sense as an attribute name?<br />

Why have an explicit constructor<br />

declared that does nothing?<br />

The code indentation is still all<br />

over the place! Making things<br />

very hard to read...<br />

Hang on! This class is supposed to be<br />

implementing the singleton pattern,<br />

but this looks like it creates a new<br />

instance <strong>of</strong> MercuryMeals every time<br />

this method is called...<br />

throws MercuryMealsConnectionException {<br />

Which qk is being used here? At least<br />

one <strong>of</strong> them is probably the wrong one<br />

so it’s very unlikely that this code is<br />

going to work at all...<br />

This method seems not to do anything <strong>of</strong> any real<br />

value at the moment. You could just as easily create<br />

an order without the Mercury Meals class—and as<br />

for the indentation, it’s all over the place.<br />

Hiding exceptions again! The caller <strong>of</strong><br />

this method will never have to handle a<br />

MercuryMealsConnectionException or any other<br />

exception because this method is hiding anything<br />

that goes wrong and just returning null again!<br />

Returning null is a bad practice. It’s a better idea to raise<br />

an exception that gives the caller more info to work with.<br />

Download at WoweBook.Com<br />

All <strong>of</strong> these problems were<br />

found, and this was only when<br />

you peeked into the first layer<br />

<strong>of</strong> the Mercury Meals code.

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

Saved successfully!

Ooh no, something went wrong!