Code Refactoring

Code RefactoringWelcome to the beginning of my Code Refactoring tutorial. Code refactoring is used to improve code design to make it easier to understand and extend. Writing understandable code will allow others to easily modify it and in the end you’ll also find you write code faster because of code refactoring.

I explain what it is in the video below as well as cover the refactoring bad smells and 2 code refactorings to get you ready for what is coming. The code used follows the video.

If you like videos like this, it helps to tell Google

Code Refactoring Code from the Video

// Demonstrate the Creation Method replacement of Constructors
// Code Refactoring

public class FootballPlayer {
	
	private double passerRating; // Specific to QBs
	private int rushingYards; // Specific to RBs & QBs
	private int receivingYards; // Specific to RBs & WRs 
	private int totalTackles; // Specific to DEF
	private int interceptions; // Specific to DEF
	private int fieldGoals; // Specific to Kickers
	private double avgPunt; // Specific to Punters
	private double avgKickoffReturn; // Specific to Special Teams
	private double avgPuntReturn; // Specific to Special Teams
	
	/* 
	
	FootballPlayer(double passerRating, int rushingYards){
		
		this.passerRating = passerRating;
		this.rushingYards = rushingYards;
		
	}
	
	FootballPlayer(int rushingYards){
		
		this.rushingYards = rushingYards;
		
	}
	
	Can't do this because the signature must be different
	 
	FootballPlayer(int receivingYards){
		
		this.receivingYards = receivingYards;
		
	}
	*/
	
	private FootballPlayer(double passerRating, int rushingYards,
			int receivingYards, int totalTackles, int interceptions,
			int fieldGoals, double avgPunt, double avgKickoffReturn,
			double avgPuntReturn){
		
		this.passerRating = passerRating;
		this.rushingYards = rushingYards;
		this.receivingYards = receivingYards;
		this.totalTackles = totalTackles;
		this.interceptions = interceptions;
		this.fieldGoals = fieldGoals;
		this.avgPunt = avgPunt;
		this.avgKickoffReturn = avgKickoffReturn;
		this.avgPuntReturn = avgPuntReturn;
		
	}
	
	public double getPasserRating() { return passerRating; }
	
	public static FootballPlayer createQB(double passerRating, int rushingYards){
		
		return new FootballPlayer(passerRating, rushingYards, 0, 0, 0, 0, 0.0,
				0.0, 0.0); 
		
	}
	
	public static FootballPlayer createWR(int rushingYards, int receivingYards){
		
		return new FootballPlayer(0, rushingYards, receivingYards, 0, 0, 0, 0.0,
				0.0, 0.0); 
		
	}
	
	public static FootballPlayer createKicker(int fieldGoals, double avgPunt){
		
		return new FootballPlayer(0, 0, 0, 0, 0, 38, 37.8,
				0.0, 0.0); 
		
	}
	
	public static void main(String[] args){
		
		// The creation methods 
		// Explain what type of player is being made
		// Are easier to understand (especially the attributes)
		
		FootballPlayer aaronRodgers = FootballPlayer.createQB(108.0, 259);
		
		FootballPlayer calvinJohnson = FootballPlayer.createWR(11, 1964);
		
		FootballPlayer sebastianJanikowski = FootballPlayer.createKicker(31, 33.0);
		
		System.out.println("Aaron Rodgers Passer Rating: " + aaronRodgers.getPasserRating());
		
	}

}

// Demonstrate chain constructors
// Code Refactoring

public class FootballPlayer2 {
	
	private String playerName ="";
	private String college = "";
	private double fortyYardDash = 0.0;
	private int repsBenchPress = 0;
	private double sixtyYardDash = 0.0;
	
	public String getPlayerName() { return playerName; }
	public String getCollege() { return college; }
	public double get40YdDash() { return fortyYardDash; }
	public int getRepsBenchPress() { return repsBenchPress; }
	public double get60YdDash() { return sixtyYardDash; }
	
	
	
	// Too much duplication in constructors
	/*
	public FootballPlayer2(String playerName, String college, 
			double fortyYardDash, double sixtyYardDash){
		
		this.playerName = playerName;
		this.college = college;
		this.fortyYardDash = fortyYardDash;
		this.sixtyYardDash = sixtyYardDash;
		
	}
	
	public FootballPlayer2(String playerName, String college, 
			double fortyYardDash, int repsBenchPress){
		
		this.playerName = playerName;
		this.college = college;
		this.fortyYardDash = fortyYardDash;
		this.repsBenchPress = repsBenchPress;
		
	}
	
	public FootballPlayer2(String playerName, String college, 
			double fortyYardDash, int repsBenchPress, double sixtyYardDash){
		
		this.playerName = playerName;
		this.college = college;
		this.fortyYardDash = fortyYardDash;
		this.repsBenchPress = repsBenchPress;
		this.sixtyYardDash = sixtyYardDash;
		
	}
	*/
	
	// Create a general catch all constructor
	
	public FootballPlayer2(String playerName, String college, 
			double fortyYardDash, int repsBenchPress, double sixtyYardDash){
		
		this.playerName = playerName;
		this.college = college;
		this.fortyYardDash = fortyYardDash;
		this.repsBenchPress = repsBenchPress;
		this.sixtyYardDash = sixtyYardDash;
		
	}
	
	public FootballPlayer2(String playerName, String college, 
			double fortyYardDash, int repsBenchPress){
		
		this(playerName, college, fortyYardDash, repsBenchPress, 0.0);
		
	}
	
	public FootballPlayer2(String playerName, String college, 
			double fortyYardDash, double sixtyYardDash){
		
		this(playerName, college, fortyYardDash, 0, sixtyYardDash);
		
	}
	
	public static void main(String[] args){
		
		FootballPlayer2 jamellFleming = new FootballPlayer2("Jamell Fleming", "Oklahoma", 4.53, 10.75);
		
		System.out.println(jamellFleming.getPlayerName());
		System.out.println(jamellFleming.getCollege());
		System.out.println(jamellFleming.get40YdDash());
		System.out.println(jamellFleming.getRepsBenchPress());
		System.out.println(jamellFleming.get60YdDash());
		
	}

}

Code Refactoring Presentation

What is Refactoring

Refactoring makes code understandable and easy to extend
Refactoring makes it quicker to create complicated systems

Why Should you Refactor

You remove excess code so it is easier to understand & modify
By refactoring code you can better understand others code
You eliminate the having to remember what old code does
You can write code quicker

What Are Bad Smells?

Bad smells are common design problems
Bad design is normally unclear, complicated or duplicated

The Bad Smells

Duplicated Code
Long Methods
Complex Conditional Statements
Primitive Obsession
Indecent Exposure
Solution Sprawl
Alternative Classes with Different Interfaces
Lazy Classes
Large Classes
Switch Statements
Combinatorial Explosions
Oddball Solutions

How Will I Teach Refactoring

Cover a common problem
Cover its solution
Explain design patterns used if needed
Provide the solution in code form
Provide a checklist that can be used to quickly jump to refactoring solutions

Creation Problems

Creation Method
Replace Constructors with Creation Methods
Which constructor should be called?
Wish constructors had descriptive names?
Constructors can’t have the same attribute signatures

Avoid Duplication & Chain Constructors

More constructors, More problems
General purpose constructors save the day

14 Responses to “Code Refactoring”

  1. Sid Rajan says:

    Is Android Next?

    • admin says:

      I’m technically teaching everything I think some one needs to make great games. After refactoring I’ll cover algorithms and then I’ll move into 2D / 3D graphics. Then I’ll show how to make Java games and then I’ll move them over to Android and teach C and OpenGL with Android.

  2. Marcus says:

    Hey Derek,
    just wanted to say thanks so much for these vids. I have been doing full time for a few months now, and your tutorials are totally fantastic. Presentation, explanation and content all 5 star!

  3. sauchem says:

    Derek — I appreciate so much your tutorial. I got a new job to work for a big corporation because I used your method during the job interview. It’s speak less of how to say THANK YOU!

    • admin says:

      I am very happy that you took the time to tell me that I helped in some way. Thank you very much and I wish you all of the best with your new job πŸ™‚

  4. Gerry says:

    Thank you for this series, Derek. I have only watched the first video and learned much!
    Keep up the good work.

  5. Hafiz Waleed Hussain says:

    Good work. Thanks.

  6. Ali says:

    Hey, The tutorials are very useful and I am thankful to you.

  7. raj kumar says:

    i need java code before and after refactoring sir

Leave a Reply

Your email address will not be published.

Google+