

5 Tips to Becoming a Better Programmer - Tip #3 - tp02ga
http://javaprogr.am/refactoring-tools

======
jussij
> The IDE has automatically figured out what parameters need to be passed into
> the new method that was created and passed in those variables for you.

I have never understood the "Wow" factor in having the IDE re-factor code.
Sure the IDE will manage to do it, but the end result will be anything but
ideal.

IMHO a human applying a level of intelligence and doing the refactoring by
hand will always achieve a better result.

To highlight this point the final result produced by the IDE was this:

    
    
        @Test
        public void testDealingOfCards ()
        {
          Deck deck = new Deck();
          List<Player> players = deck.getPlayers();
          Dealer.deal(deck.getDeck(), players);
         
          for (int i=0; i<players.size(); i++)
          {
            List<Card> cardsInHand = players.get(i).getCardsInHand();
            assertThat(cardsInHand.size(), is(5));
         
            outputCardDetailsToConsole(players, i, cardsInHand, deck);
          }
          assertThat(deck.getDeck().size(), is(32));
        }
         
        private void outputCardDetailsToConsole(List<Player> players, int playerNumber, 
          List<Card> cardsInHand, Deck deck)
        {
          System.out.println(players.get(playerNumber).getName() + " has the following cards:");
          for (Card aCard : cardsInHand)
          {
            System.out.println(aCard.toString());
          }
          System.out.println("The deck has " + deck.getDeck().size() + " cards in it.");
          System.out.println();
        }
        

If this code was to be truly refactored it should end up looking something
like this:

    
    
        @Test
        public void testDealingOfCards ()
        {
          Deck deck = new Deck();
          List<Player> players = deck.getPlayers();
          Dealer.deal(deck.getDeck(), players);
    
          for (int i=0; i<players.size(); i++)
          {
            outputCardDetailsToConsole(players.get(i), deck);
          }
          assertThat(deck.getDeck().size(), is(32));
        }
        
        private void outputCardDetailsToConsole(Player player, Deck deck)
        {
          List<Card> cardsInHand = player.getCardsInHand();
          assertThat(cardsInHand.size(), is(5));
        
          System.out.println(player.getName() + " has the following cards:");
          for (Card aCard : cardsInHand)
          {
            System.out.println(aCard.toString());
          }
          System.out.println("The deck has " + deck.getDeck().size() + " cards in it.");
          System.out.println();
        }

