Album Cover

Legacy Code Java

Close Preview
Sometimes the private method will be so complex that it's really an entity in its own right, and definitely warrants microtesting. In this case:
  • Extract the complex code into a new class, using public API's where the action is.
  • Microtest that class.
  • When you are ready, use a private member of the current class to point to the new class.
In the following example, getHandTotal() lives in the game class (Blackjack), but performs complex logic on a List class.

public class Blackjack…
	private List playerHand;
	private List dealerHand;
public bool didPlayerWin() { return getHandTotal(playerHand) > getHandTotal(dealerHand); }
private int getHandTotal(List hand) { int total = 0; Iterator iCard = hand.iterator(); while (iCard.hasNext()) { Card card = (Card)iCard.next(); total += card.value; } return total; }

public class Blackjack…
	private Player player;
	private Player dealer;
public bool didPlayerWin() { return player.getHandTotal() > dealer.getHandTotal(); }
public class Player… private List hand;
public int getHandTotal() { int total = 0; Iterator iCard = hand.iterator(); while (iCard.hasNext()) { Card card = (Card)iCard.next(); total += card.value; } return total; }
(Reading...)
| Tweet...
Page rating: | Rate this Page