Assignment 9: Code Critique and Refactoring Code

Due: Before class Wednesday.

Objective: This activity is intended to help you to learn to critically read code, understand its purpose, and identify ways to improve it. Along the way, you should start to build a sense of what makes one piece of code better than another given a specific set of design goals. You should also get more comfortable using the Eclipse Refactoring tools.

The Bin-Fitting Problem

Bin-packing is a classic computer science problem with many real-world applications. The idea is to fit items with some characteristic, like weight or size, into containers called bins so as to minimize the number of bins. In the version of the problem you will work on, the items being fit or packed are files, which are specified by their size. Think of the files as mp3 files or video files. Their size is given in Kbytes, e.g., a file size of 420713 represents about 420 MB (Megabytes), which is a little less than half a Gigabyte or GB.

The bins are disks. You can think of them as CDs or DVDs. You're trying to minimize the number of disks used to contain all the files being fit or packed. In this problem each disk has a capacity of 1 GB, i.e., 1,000 MB.

The input to the program is a file of file-sizes. The output is a report of the total of all the file sizes in GB and the number of disks used to store all the files.

Heuristics

In general, worst-fit heuristics store each file on the disk with the most free space. If there is no disk with enough free space, another disk is started. For in-order, the files are examined in the order in which they are given. For worst-fit decreasing order, files are examined in sorted order from the largest file to the smallest file, in terms of size.

For example, if the file sizes given are 700,000 200,000 800,000 100,000 150,000 then, using the worst-fit in-order heuristic, the following three disks will be used to store the files

  1. 700,000 200,000
  2. 800,000 100,000
  3. 150,000

Using the worst-fit in-decreasing-order heuristic, the same data above will yield the following two disks (which happens to be the minimum):

  1. 800,000 150,000
  2. 700,000 200,000 100,000

Note that you could construct examples where each heuristic would create an optimal number of disks.

Understanding the Program

Look at the complete Java solution to the bin-fitting problem. Start by reading it and making sure you understand the given code. Next, examine the code from the perspective of how it may have been debugged and tested and how it may be extended in the future. Think about its strengths and shortcomings. Specifically, answer the following questions:

While studying this code, please note any good things about the code that you might keep in the final version as well as problems with the code.

You will turn these notes into a short report that is submitted with the assignment.

Refactoring

You should examine the given program and refactor it based on your critique and our discussion in class. Remember the goals of refactoring: improving its design, maintainability, readability, testability, and debuggability (?!). You may create (and comment) any new functions or classes you want to help improve the program; however, you should justify each change you make by explaining specifically how it improves the code in your Code Critique.

Testing

Additionally, you should provide a set of JUnit tests as well as a driver program that verify your modified program still works as intended.

To help you get started, a test case was provided for you in the tar file.

One of the reasons that I explicitly require testing for this assignment--besides that you should be testing!!--is that poor design often reveals itself when you're trying to write test cases and you realize that it's difficult.

Iteration

After refactoring your code, go back through your code, thinking about the questions that were asked in Understanding the Program. Are there additional changes you should make?

Extending

Finally, add three additional bin packing algorithms to your refactored code:

Display the results for all heuristics.

Extra credit (10 pts)

Change the program to print the algorithm that produces the smallest sets of disks required to fit all of the files. In case two algorithms produce the same number of disks, the first algorithm tried should be the one printed.

Submission

Code Critique

In a text file called CRITIQUE, describe what you like and don't like about the code. Describe how you addressed the parts that you don't like and why you chose that approach. (Note the original questions in Understanding the Code section above.) Justifications that refer specifically to principles discussed in class will be given more credit than those that use terms like "clearly/obviously", "good/stinky", or "like/hate". Finally, talk about if you're done refactoring. If you had more time, what additional refactoring would you do?

Jar File

Create a Jar file from your project and copy it into an assign9 directory. Your code critique can be inside the assign9 directory or inside the Jar file itself.

Grading Criteria (125)

Your grade will be based on