Code Critique
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.
There are three parts to this activity:
- analyze and comment on a small fragment of code
- analyze and comment on a complete program
- refactor the given code to improve it and write tests to verify it works after you are finished changing it
A Code Fragment
Look at this handout given previously as an assignment in CompSci 100. Start by reading it and making sure you understand the problem you are being asked to solve. Next, examine the code given as an exemplar for solving the first part of the assignment and discuss its strengths and shortcomings with your group. In your disucssion, attempt to answer the following questions:
- How clearly written is the code?
- What, if any, comments might be helpful within the code?
- Does it satisfy its role as a tutorial?
- What, if any, suggestions does this code make about the remaining parts of the assignment will be written?
- How would you test this code for bugs?
A Complete Program
Now, look at this complete Java solution to the same 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. Discuss its strengths and shortcomings with your group. In your disucssion, attempt to answer the following questions:
- Does it satisfy the requirements of the assignment?
- What, if any, comments might be helpful within the code?
- How easy do you imagine this code was to write, test, and debug?
- How would you test this code for bugs?
- What, if any, additional functions may be helpful?
- What, if any, classes may be helpful?
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.
Refactoring
Refactoring is the practice of updating a program to improve its design and maintainability without changing its current functionality significantly. An example of refactoring is creating a single function or class that replaces two or more sections of similar code because it reduces the amount of redundant code within the program and makes the code easier to debug and test. You should examine the given program and refactor it based on your critique and our discussion in class. 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.
Additionally, you should provide a set of tests that verify your modified program still works as intended. Tests can be in the form of input data files, driver programs, or other classes that verify every line of code written works as intended.
Resources
The following readings provide principles for improving the design of small sections of code.
- OO in One Sentence by A. Hunt and D. Thomas
- Open Closed Principle by R. Martin
- Code Smells by K. Beck and M. Fowler