Refactoring (Reichenbach)
Content
- Why do we change program structure?
- What are Bad Smells?
- What is Refactoring?
- How and when is refactoring useful?
- What are some common program structures that we might refactor into?
Videos and Quiz
- 1 - Introduction Links to an external site.
- 2 - Refactoring Links to an external site.
- 3 - Demos intro (VS Code + clangd and C) Links to an external site.
- 4 - Demos: running example (VS Code and Python) (refactorings: Rename, Extract Value, Replace Data Value With Object) Links to an external site.
- 5 - Demos: Extract Method refactoring (IntelliJ + Python; EMACS + jdt and Java) Links to an external site.
- 6 - Demos: Moving and Inlining (Eclipse and Java) (refactorings: Replace Magic Literal, Extract Method, Move Method, Introduce Parameter, Change Parameters, Inline Method) Links to an external site.
- 7 - Summary Links to an external site.
- 8 - Homework Assignments by Noric Couderc Links to an external site.
- Self-assessment quiz
Session structure
- There will be completely optional Refactoring office hours from 14:00-15:00 on the 3rd, 4th, and 5th of March 2021. Zoom link: https://lu-se.zoom.us/j/65769311087 Links to an external site.
Learning resources
- Notes & QAs WASP Software Engineering Course (shared Google Doc) Links to an external site.
- Slides from the class (2019 edition) Links to an external site.
- Fowler's Refactoring catalogue Links to an external site.
- Wikipedia on Design Patterns Links to an external site.
- Wikipedia on Bad Smells Links to an external site.
- University of Kent refactoring catalogue for functional programming languages Links to an external site.
- Alternative functional refactoring catalogue Links to an external site.
- Book: Refactoring: Improving the Design of Existing Code, by Martin Fowler
- Book: Design Patterns, by Gamma, Helm, Johnson, and Vlissides
- How To Write Unmaintainable Code Links to an external site., by Roedy Green
- Introduction to Object-Oriented Programming Links to an external site., by Derek Molloy, Dublin City University
Grading criteria
To pass this module, you will perform refactorings in a code repository that you are working on for one of the other modules. Each refactoring must be committed to the repository as a single commit.
You can choose a combination of simple and complex refactorings, but the total refactoring score must be at least 16 points. A simple refactoring is worth one point and a complex one is worth three points. See the lists below for acceptable refactorings. You must include at least two complex refactorings.
Keep in mind:
- Changed 15 March: You should submit a list of commits and diffs on canvas. The reason for that is that requesting access to that many git repositories is not practical (Noric needs to create an account on every gitlab instance students use).
- Noric (your grader) will review your commits.
- Each refactoring must be submitted in a single commit.
- The commit must name the refactoring (try to follow the naming scheme of the refactoring catalogues as much as possible).
- You must submit a list of the commit identifiers and diffs for the refactoring commits to Canvas by Friday the 2nd of April.
How do you submit?
If your commits messages contain the word "Refactoring", you can submit the result of the command git log -p --grep "Refactoring"
(-p
prints diffs along with the commit information, --grep
filters the commits whose message matches a string). Otherwise, you can print the diffs for specific commits with the command git show -p <COMMIT1> <COMMIT2> ...
for the commits you want to include in your submission. Redirecting the output of this command to a file should give you what we want, you can then copy paste this text as your submission on canvas.
Refactorings
For other refactorings than the ones listed below, check with Noric whether they count as Complex or Simple. We will update this list as new decisions are made.
Complex refactorings (3 points each):
- (Object-Oriented) Replace conditional with polymorphism Links to an external site.
- (Object-Oriented) Replace inheritance with delegation Links to an external site.
- Extract class/interface/method/function/parameter/variable (see Video 5 Links to an external site.)
- Extract superclass and analogous refactorings (e.g., "extract intermediate typeclass" in Haskell / "extract intermediate trait" in Rust)
- (Object-Oriented) Pull up method/field
- (Object-Oriented) Push down method/field
- Introduce assertion
- Remove flag parameter Links to an external site.
- Encapsulate Variable/Field
- Consolidate Conditional Expression Links to an external site.
- Decompose Conditional Links to an external site.
- Replace code with library use
- Substitute algorithm
Simple refactorings (1 point each):
- Rename method/variable/field/package (see Video 4 Links to an external site.)
- (Object-Oriented) Move method/field (see Video 6 Links to an external site.)
- Move function/declaration
- Extract/replace magic constant/literal (see Video 6 Links to an external site.)
- Inline method (see Video 6 Links to an external site.)
- Remove dead code
Non-refactorings (0 points):
- Functional changes.
- Bug fixes.
- Adding new features/options.
- File/directory renaming.
- Changes to build system, build script, toolchain.
- Changes to data only.
- Renaming output files; only affecting string literals in the code.
- A commit that only adds code is usually not a refactoring (unless it is an assertion or the code replaces some implicit behaviour)
For more information about these refactorings, refer to e.g. Catalog of Refactorings Links to an external site..
Alternative project
If you are already a master refactorer and bored by this project, you can opt to re-implement and evaluate the system from Does automated refactoring obviate systematic editing? Links to an external site. (ICSE'15), simplified as needed to fit into a one-month timeframe, or propose a refactoring-related research or engineering project to the instructor (e.g., implementing and evaluating a language server Links to an external site. with refactoring support for a language that lacks such a tool).