Code Refactoring 3

Code Refactoring Explaining VariablesIn this part of the code refactoring tutorial, I show you examples of how you can make your code more understandable using something called an Explaining Variable.

We also explore many short cuts people take when writing code that will cause many problems later. I want to get these basic rules out of the way so we can concentrate on refactoring to design patterns very soon in this tutorial series.

The code is available below to help you learn.

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

Code From the Video

Product.java

public class Product {

	private String name = "";
	private double price = 0.0;
	private double shippingCost = 0.0;
	private int quantity = 0;
	
	public String getName(){ return name; }
	public double getPrice(){ return price; }
	public double getShippingCost(){ return shippingCost; }
	public int getQuantity(){ return quantity; }
	
	Product(String name, double price, double shippingCost, int quantity){
		
		this.name = name;
		this.price = price;
		this.shippingCost = shippingCost;
		this.quantity = quantity;
		
	}
	
	public double getTotalCost(){
		
		double quantityDiscount = 0.0;
		
		// If your expressions become complicated it may make more sense to save
		// them in temporary variables (Explaining Variables)
		
		final boolean over50Products = (quantity > 50) || ((quantity * price) > 500);
		final boolean over25Products = (quantity > 25) || ((quantity * price) > 100);
		final boolean over10Products = (quantity >= 10) || ((quantity * price) > 50);
		
		if(over50Products) {
			
			quantityDiscount = .10;
			
		} else if(over25Products) {
			
			quantityDiscount = .07;
			
		} else if(over10Products) {
			
			quantityDiscount = .05;
			
		}
		
		/* BAD CODE 1
		if((quantity > 50) || ((quantity * price) > 500)) {
			
			quantityDiscount = .10;
			
		} else if((quantity > 25) || ((quantity * price) > 100)) {
			
			quantityDiscount = .07;
			
		} else if((quantity >= 10) || ((quantity * price) > 50)) {
			
			quantityDiscount = .05;
			
		}
		*/
		
		double discount = ((quantity - 1) * quantityDiscount) * price;
		
		return (quantity * price) + (quantity * shippingCost) - discount;
		
	}
	
	
}

Store.java

import java.util.ArrayList;

public class Store {
	
	public ArrayList<Product> theProducts = new ArrayList<Product>();
	
	public void addAProduct(Product newProduct){
		
		theProducts.add(newProduct);
		
	}
	
	public void getCostOfProducts(){
		
		for(Product product : theProducts){
			
			// You can also use an explaining variable for complicated calculations
			// It may however be better to extract this code into a method though
			// to separate it from the method
			
			// final is used to make sure the temp only has 1 value per iteration
			// It is bad practice to assign different values to a temp
			
			final int numOfProducts = product.getQuantity();  
			final String prodName = product.getName();
			final double cost = product.getTotalCost();
			
			final double costWithDiscount = product.getTotalCost() / product.getQuantity();
			final double costWithoutDiscount = product.getPrice() + product.getShippingCost();
			
			System.out.println("Total cost for " + numOfProducts + " " + prodName + "s is $" + cost);
			
			System.out.println("Cost per product " + costWithDiscount);
			
			System.out.println("Savings per product " + (costWithoutDiscount - costWithDiscount));
			
			System.out.println();
			
			
			/* BAD CODE 2
			System.out.println("Total cost for " + product.getQuantity() + " " + product.getName() + "s is $" + product.getTotalCost());
			
			System.out.println("Cost per product " + product.getTotalCost() / product.getQuantity());
			
			System.out.println("Savings per product " + ((product.getPrice() + product.getShippingCost()) - (product.getTotalCost() / product.getQuantity())) + "\n");
			*/
			
		}
		
	}
	
	public static void main(String[] args){
		
		Store cornerStore = new Store();
		
		cornerStore.addAProduct(new Product("Pizza", 10.00, 1.00, 52));
		
		cornerStore.addAProduct(new Product("Pizza", 10.00, 1.00, 26));
		
		cornerStore.addAProduct(new Product("Pizza", 10.00, 1.00, 10));
		
		cornerStore.getCostOfProducts();
		
	}
	
	// BAD CODE 3
	// Why Is it Bad to Assign Many Values to a Temp?
	
	/*
	double temp = totalCost / numberOfProducts;
	
	temp = temp + shipping;
	
	// temp may be the product price + shipping - discount
	// but will this make sense a year from now?
	
	temp = temp - discount;
	*/
	
	
	
	// BAD CODE 4
	// Don't assign values to parameters either
	/*
	public double getTotPrice(double quantity, double price, double shippingCost, double discount) {
		
		price = price + shippingCost;
		price = price * quantity;
		return price - discount;
		
	}
	*/

}

6 Responses to “Code Refactoring 3”

  1. Gerry says:

    How different would PHP be from your Java examples, Derek?

    • admin says:

      PHP and Java are considerably different from each other. PHP is a scripting language. It is best at pulling data from the server and providing it to web pages. For that purpose it is fabulous.

      Java is a general purpose language that can be used to create just about anything. The major problem with Java is that it is slow, but if you combine it with C, which I’ll do soon, it can do even more.

      Both are great languages, but php is a highly specialized language. I hope that helps

  2. Herbi says:

    Awesome!!

  3. Hafiz Waleed Hussain says:

    Good work. Thanks

Leave a Reply

Your email address will not be published.

Google+