Lab #1: Refactoring
"Fundamentally, programming is about naming things correctly." -Professor AckleyGoals
- JAR Import vs. Add to Build Path
- Refactor Commands
- If time: Write worthwhile tests
Needs
- Lab1NeedsRefactoring.jar
- (Alt+Shift+R)...(Option+Shift+R for Mac???) - Highlights a single word for you to edit and refactors each occurrence of it in your project. This can be done for method names, variables, interfaces, classes etc.
- A bunch more for reference: http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fref-menu-refactor.htm
Instructions
'Refactoring' is changing the design of code WITHOUT changing what the code does.
Most of the time, when given another's code, you will never want to edit but rather just access its methods for your own use. In this case you would add the .jar to the build path of your project and thus just have access to the un-editable .class files.
To do this, Right click on Java project -> Build Path -> Configure Build Path... -> "Libraries" tab -> Add External JARs... and point to the .jar file to add.
This allows for the .class files within the .jar file to be in the scope of your project and can be imported using the import keyword within any file that wishes to use it. (ex. All JARs in the JRE System Library, DeathBox.jar, GWT.jar etc.)
In a more rare case, you would like to import the actual .java contents of the .jar file and have the ability to edit the files directly. This would only be done if you are given a .jar that has .java files attached AND you would be allowed (and have the specific need) to edit what the original author's code did.
To do this, Right click on a Java project -> Import... -> Archive File (under General) -> Select the .jar to import. You also have the opportunity to just import specific file types into your project instead of the entire contents. For this lab, I would like you to just import .java files since you will actually be refactoring and overwriting them. This can be done by clicking the Filter types... button.
//TODO
Read through the code given to you in the .jar file. Figure out what it's doing. (There are a few hints if you look at and run Main.main())
The naming of EVERYTHING is horrendous.
After you figure out what each method/class/variable is doing/meant for, refactor and comment (using Javadoc) accordingly. For ex: If the body of a method "public int foo(int i)" was:
return 2 + i;
you might rename it "add2".
Don't be afraid to create new methods as well for readability etc.
*Note: If you have the time, create a JUnit test suite for the newly named "Implemented.java" file.
This should be done to the following files:
- Implemented.java
- Interface.java
- Main.java
- Thing.java
Export 4 newly refactored .java files into a JAR file named "Lab1.jar" and turn-in using online interface. MAKE SURE THE "Export Java source files and resources" CHECKBOX IS CHECKED, OTHERWISE I WILL NOT BE ABLE TO SEE YOUR CODE.