Platinum Partner
java,tips and tricks

Refactor Out Code Smell With JDeodrant

I got to know about JDeodorant- a tool for identifying bad smells in code and helping it to refactor. I got curious and downloaded its Eclipse plugin, I then picked the first bad smell code which Martin Fowler explains in his book: “Refactoring: Improving the design of existing code”. I tried my hand at refactoring a long method by Extract Method refactor move. Here’s the long method:

public String statement(){
  double totalAmount = 0;
  int frequentRenterPoints = 0;
  String result = "Rental record for "+ getName() + "\n";
  for(Rental each : _rentals){
    double thisAmount = 0;
    switch (each.getMovie().getPriceCode()) {
      case Movie.REGULAR:
        thisAmount += 2;
        if ( each.getDaysRented() > 2)
          thisAmount += (each.getDaysRented() - 2 ) * 1.5;
        break;
      case Movie.NEW_RELEASE:
        thisAmount += each.getDaysRented() *3;
        break;
      case Movie.CHILDREN:
        thisAmount += 1.5;
        if (each.getDaysRented() > 3 )
          thisAmount += (each.getDaysRented() - 3)* 1.5;
        break;
    }
    frequentRenterPoints++;
    if ( (each.getMovie().getPriceCode() == Movie.NEW_RELEASE)
          && each.getDaysRented() > 1 )
      frequentRenterPoints ++;
    result += "\t" + each.getMovie().getTitle() + "\t" +
        String.valueOf(thisAmount) + "\n";
    totalAmount += thisAmount;
  }
  result += "Amount owed is "+ String.valueOf(totalAmount)+"\n";
  result += "You earned " + String.valueOf(frequentRenterPoints) +
      " frequent renter points";
  return result;
}

So I use the JDeodorant plugin and run the Long Method: Bad Smell, it correctly identifies the thisAmount calculation as a long method, as shown in the image:

Now I try to incorporate the suggested refactor and it leads to the code where the thisAmount calculation is moved into a method getAmount:

private void getAmount() {
  for (Rental each : _rentals) {
    double thisAmount = 0;
    switch (each.getMovie().getPriceCode()) {
      case Movie.REGULR:
        thisAmount += 2;
        if (each.getDaysRented() > 2) {
          thisAmount += (each.getDaysRented() - 2) * 1.5;
        }
        break;
      case Movie.NEW_RELEASE:
        thisAmount += each.getDaysRented() * 3;
        break;
      case Movie.CHILDREN:
        thisAmount += 1.5;
        if (each.getDaysRented() > 3) {
          thisAmount += (each.getDaysRented() - 3) * 1.5;
        }
        break;
    }
  }
}

and the original code is altered as:

//older code
thisAmount = getAmount(); //new line added after refactor
for(Rental each : _rentals){
  frequentRenterPoints++;
//code here moved to getAmount method
//older code
  if ( (each.getMovie().getPriceCode() == Movie.NEW_RELEASE)
      && each.getDaysRented() > 1 )
    frequentRenterPoints ++;

The identification of the parts which could be refactor was correct but the refactor done wasn’t as expected. Ideally the variables which the block of code was referring to should have moved to the method param list and the code identified for refactor copied as is. And also as this refactor was around the use of a certain variable “thisAmount”, the extracted method should have returned the calculated value. So I might have to explore the way the refactoring is done.

But to start with:
- It helps in identifying bad smells – I tried with the one which was obvious, not sure about the not so obvious ones.
- One can consider the refactoring advice but not always will the judgement of a tool be better than human experience. So take it with a pinch of salt.

I would like to explore this as and when I get time, but I would also have to read the ideas behind the bad smell identification and refactoring suggestions before I can move along with the tool.

Just in case someone has already tried using this tool, please feel free to drop in the feedback.

And an interesting name for the project: JDeodorant- to drive away the bad smell :)

 

 

 

Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}