SDA SE Wiki

Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering

User Tools

Site Tools


Assignment 12: Refactoring

Release date: Tuesday, 26.06.12 - Due date: Monday, 10.07.12, 23:59

Task 01: Refactoring

10 + 2 optional points

We have an implementation of a class Rectangle that stores its coordinates in two instances of the class Interval, one for the horizontal and one for the vertical extension of the Rectangle.

  1. Review the code below. Give us six ideas how to improve it. If you can name a bad smell somewhere, do it. Which refactorings would you apply? We won't count the suggestion to introduce getter-methods. (Maybe you understand later why (see 3)).
  2. Give us for four cases the code that we would get after your refactoring.
  3. Optional! Reviewing your code after executing the refactorings, could you imagine why we suggest being reluctant concerning the introduction of getter-methods?

You can find the source code for this task (with a JUnit-test) as well in your repository in the project A12T01_Refactoring.

View.java

package gui;
import model.Rectangle;

public class View {
  public Rectangle rectangle;
  public void redraw() {
    System.out.println(rectangle);
  }
}

Rectangle.java

package model;
import static java.lang.Math.*;
import gui.View;

public class Rectangle {

  private Interval horizontal;
  private Interval vertical;
  private View view;

  public Rectangle(View view, int startX, int startY, int endX, int endY) {
    if ((startX > endX) || (startY > endY))
      throw new IllegalArgumentException("Parameters in wrong order.");
    this.horizontal = new Interval(startX, endX);
    this.vertical = new Interval(startY, endY);
    this.view = view; view.rectangle = this;
  }

  /**
   * Create an empty Rectangle with no location.
   */
  public Rectangle(View view) {
    this.horizontal = null;
    this.vertical = null;
    this.view = view; view.rectangle = this;
  }
 
  public Vertex getCenter() {
    if (horizontal == null)
      return null;
    return new Vertex((horizontal.start + horizontal.end) / 2,
        (vertical.start + vertical.end) / 2);
  }

  public boolean contains(Rectangle parameter) {
    if (parameter.horizontal == null)
      return false;
    if (horizontal == null)
      return false;
    // check whether the horizontal interval
    // contains the horizontal interval
    // in the other rectangle
    // AND
    // check whether the vertical interval
    // contains the vertical interval
    // in the other rectangle
    return ((horizontal.start <= parameter.horizontal.start) 
      && (parameter.horizontal.end <= horizontal.end))
      && ((vertical.start <= parameter.vertical.start) 
      && (parameter.vertical.end <= vertical.end));
  }

  /**
   * Enlarge the Rectangle just enough so that it contains the other
   * Rectangle. If the Rectangle was empty it gets the same extension
   * as the other Rectangle.
   */
  public void accommodate(Rectangle other) {
    if ((other == null) || (other.horizontal == null))
      return;
    if (horizontal == null) {
      horizontal = new Interval(other.horizontal);
      vertical = new Interval(other.vertical);
      return;
    }
    horizontal.start = min(other.horizontal.start, horizontal.start);
    horizontal.end = max(other.horizontal.end, horizontal.end);
    vertical.start = min(other.vertical.start, vertical.start);
    vertical.end = max(other.vertical.end, vertical.end);
    view.redraw();	
  }

  public boolean equals(Object obj) {
    if (!(obj instanceof Rectangle))
      return false;
    Rectangle other = (Rectangle) obj;
    if (horizontal == null)
      return (other.horizontal == null);
    return (horizontal.equals(other.horizontal))
        && (vertical.equals(other.vertical));
  }

  public String toString() {
    return "Rectangle(" + horizontal + " x " + vertical + ")";
  }
}

The automated refactorings in Eclipse are more effective if you use some tricks:

  • Before extracting a method extract those expression you want to become parameters into local variables.
  • Before moving a part of a method to another class, you should extract this part into an own method in the current class.
  • If you need more tips: Ask on the mailing list! :-)

Task 02: Dual Choice Questions, Testing and Refactoring (optional)

4 points

Decide whether the following statements are correct or wrong. Give the answer and a short explanation (1-2 sentences) in a text file.

Evaluation: For this assignment you get 0,5 points for every correct answer with a reasonable explanation. During the final exam you get +1 point for every correct answer and -1 point for every incorrect answer. The minimal number of points for each of the section is 0, this means e.g. giving three incorrect answers in the first block does not influence the points for the second block.

Correct Wrong Statement
A test stub simulates the components called by the tested component.
The advantage of a top-down strategy for integrations testing is that you can start to test the upper layer immediately and that you don’t need to write any test stubs.
While creating test cases for Black-Box testing, you should reduce the amount of test data by identifying equivalence classes.
The smell “feature envy” means that a method reads or writes fields of other classes much more than its own. It often can be removed by moving the envious parts of the method to the other class.
Correct Wrong Statement
You need at least 2 test cases to get full branch coverage for methodA() and at least 3 test cases for methodB().
To get full path coverage you need at least 4 test cases for methodA() and 3 test cases for methodB().
If you can get full branch coverage for a method, you can also get full path coverage.
You have achieved full statement coverage of your system, if the flow of events of every evaluation scenario that you gathered during requirements elicitation appears at least once in a test.
public void methodA(){
   if (...) { ... } else { ... }
   if (...) { ... } else { ... } 
}	
  
public void methodB(){
   if (...) { 
      if (...) { ... } else { ... } 
   } else { 
     ... 
   } 
}
teaching/lectures/oosc/2012/assignment_12.txt · Last modified: 2018/05/09 01:59 (external edit)

SEWiki, © 2024