Cleaning up code, perhaps by moving a field from one class to another, pushing some code up or down a hierarchy.
Most often it is the process of splitting out repetitive code to turn it into its own separate method. An
automated tool such as a SCIDs to help you do might work like this:
- I have a method which has some code that I would like to pull out into its own method.
- I highlight the offending code.
- I select Extract Method from a popup menu
- The Refactoring Browser asks me to name the method and automatically creates it and inserts the highlighted
code.
- In the current method, the highlighted code is replace by an invocation to the newly created method.
Ideally, in a perfectly written computer program, with a perfectly designed computer language, with a perfect
SCID/ IDE, it should be possible to change any line/element of the
program in isolation without having to make any supporting changes elsewhere. The program should
still function in a reasonable way. For example, you should be able to change the allowable bounds on a field
variable in one line of code (which might result in it internally being promoted from int to long), without having to manually adjust any
other code. The basic principle is you should have to tell a computer any one fact, or decision in only one
place. For example, you should have to tell a program which colour scheme to use in precisely one and only one
place, and by “one place” I mean one line of code. Real life computer languages and IDE (Integrated Development Environment)s
put up huge
roadblocks to getting anywhere close to perfection.
Java is a very refactoring-friendly language. You can change the type of a variable, add or remove a parameter to a method,
Change something from static to instance or the reverse. Move code around. Split code up. Rename. You can do these things
with a refactoring tool such as the one in IntelliJ, but even if it or you screw up or fail to complete the changes, as
soon as you compile,
the error mesages will warn you of other things you must change to be consistent. Pretty much, when you get a clean compile, your code will work.
IntelliJ
I use Jetbrains IntelliJ IDEA as my IDE, which supports many canned refactorings with
a few mouseclicks. Some of the most useful are:
- Extract Method: I highlight a block of code that might be many line or just an expression. It then turns it
into a method, figures out what the parameters need to be, and what type it returns. It then replaces the code
with a call to the new method. It then finds similar code snippets and, with permission, also converts them to
calls the new method.
- Introduce constant: converts literals to named constants. You have to give it some help to decide which
instances to convert.
- Change signature: add, remove, rename, reorder the parameters of a method and automatically modify all
invocations to the new rules.
- Global rename: my most common refactoring. Some people would not consider it a refactoring, but I consider
it the most important tidying activity partly because it drives the decision of what code belongs where.
IntelliJ renames with knowledge of Java syntax, so you don’t accidentally rename unrelated local
variables with the same name. Lets you review which comments to rename. Lets you also rename similarly named
variables and method in the same pop.
- Pull up: move a method or field from subclass to its superclass and make necessary adjustments. It will let
you control just how much of its attached baggage of fields and methods you move with it.
- Push down: move a method or field from a superclass to one of its subclasses.
- Generify: add generics to a class.
It is a whole new way of programming. By letting the computer handle the details of the transformations, you can
rapidly rearrange code without breaking it. You tend then to hold yourself to an ever higher standard. It is a
step on the road to a SCID (Source Code In Database) where you don’t transform its deep structure to view it in alternate ways.
Algebra
If you recall your grade 7 algebra, you learned to refactor. You learned to convert 7a + 7b to
7(a + b). You look for commonality then split it out so that it appears only once.
This is the same thing you do to refactor Java. You look for common code. Then you split it out so there is no repetition of it.
You can have two classes implement a common interface, or one class inherit from another or two classes inherit from a common base class,
possibly abstract. You write the common code in using only the fields and methods of the common interface or class.
Books
 |
recommend book⇒Refactoring : Improving the Design of Existing Code |
| by: | Martin Fowler, Kent Beck (Contributor), John Brant (Contributor), William Opdyke, Don Roberts. |
978-0-201-48567-7 | hardcover |
| | (born: 1963 age: 48) |
B000OZ0N4Y | kindle |
| publisher: | Addison-Wesley |
| published: | 1999-07-08 |
|
| Greyed out stores probably do not have the item in stock |
 |
recommend book⇒Code Complete : A Practical Handbook of Software Construction |
| by: | Steve McConnell |
978-1-55615-484-3 | paperback |
| publisher: | Microsoft Press |
| published: | 1993-05-14 |
| Gives lots of practical advice on optimising. He talks mostly about C with examples in Fortran, Pascal, Basic and Ada. He doesn’t talk about Java. It didn’t exist when the book was written. |
|
| Greyed out stores probably do not have the item in stock |
 |
recommend book⇒Refactoring Workbook |
| by: | William C. Wake |
978-0-321-10929-3 | paperback |
| publisher: | Addison-Wesley |
| published: | 2003-09-06 |
| has exercises and real world examples. An emminently practical book. |
|
| Greyed out stores probably do not have the item in stock |