Skip to content

Code Smell: Long Method in "BillyBurger" #5

@DJavierReyesM

Description

@DJavierReyesM

Hello again. I was checking something, and notice something: In the Hamburger class (where the code smell Data Clumps was reviewed) there is a method called itemisedHamburger (), which allows to calculate the total price to pay for the hamburger and its extras. The way in which the method to calculate the total to pay is implemented tends to have a method with very long code which reuses code in a bad way by having to validate each of the additional ones. You can refactor your code by ussing Substitute Algorithm. With this technique, the size of the code would be greatly reduced by not having to reuse code in a bad way through validations through if blocks for each of the additional ones, avoiding repeating the code unnecessarily. To consolidate the change in the first instance, a field is added as a list that will allow storing the additional ones and in turn the number of additional ones is deleted along with their methods to only leave the aforementioned list and an addAditional () method is added so additional ones are added to the list, thus avoiding having many methods with similar algorithms:

The field for the additionals (Don't forget to initializate it) and my proppose for the code:

  private ArrayList<HamburgerAdditional>listAdditional;
  public void addAddition(HamburgerAdditional addition) {
	if(this.listAdditional.size()>3) {
		System.out.println("The Standard Hamburguer can only have 4 additionals");
	}
	else {	
    this.listAdditional.add(addition);
	}
}

public double itemisedHamburger() {
    double subTotal = this.basePrice;
    System.out.println("Total price of "+ this.name+ " burger made from "
            + this.breadRollType+ " and "+ this.meat+ ":");
    System.out.println("base price: " + this.basePrice);
 
    for(HamburgerAdditional x: this.listAdditional) {
    	if(x != null) {
    		subTotal += x.getPrice();
            System.out.println("additional " + x.getName()+ ": "+ x.getPrice()
                    + " subtotal: "+ (double) Math.round(subTotal * 100) / 100);		
    	}  	
    }
    return Math.round(subTotal * 100)/100;

}

Have a nice day

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions