Programming Tools: Refactoring

A look at two open-source refactoring tools, Eclipse for Java and Bicycle Repair Man for Python.
Conclusion

The benefits of using tools to do code refactoring include:

  • They take a fair amount of drudgery out of refactoring code.

  • They calculate which variables need to appear in the parameter list of the extracted methods, barring the problem above.

  • They make renaming variables/classes/functions/methods almost painless.

I realize I haven't discussed why you should refactor your code, or anyone else's for that matter. Often, there are both technical and managerial reasons to do refactoring. Using prototypes for production purposes is one of the bugaboos of programming, and we know the problems this causes. However, refactoring may be a way out of this cul-de-sac. Perhaps, you can use the benefits of refactoring to justify a rewrite to management.

Postscript

I am puzzled by the differences in the interest between refactoring and metrics. I have found no objective way of measuring the effect of code refactoring. It seems that we simply know that the refactored code is "better".

I am also bothered by my response to one comment on last month's column. The writer mentioned that Function Points are an accepted metric for measuring complexity. They also provide good, consistent metrics for estimating project costs. I made an error of omission in not mentioning function points. On further reflection, I still don't see them as effective code metrics. Function points most often are used during specification and design of systems. They also are labor-intensive. I know of no product out there that can do a proper function point analysis on an existing code base. Please correct me if I am wrong.

Resources

Sources for Information about Refactoring

Open Source Plugin for jEdit

An Older Java Refactoring Tool

Java freeware, usable as standalone or plugin for a number of editors and IDEs; recent and extensive.

Pymetrics Home Page

Refactoring: Improving the Design of Existing Code by Martin Fowler, Don Roberts, John Brant, William Opdyke (ISBN 0-201-48567-2), Addison-Wesley.

Refactoring to Patterns by Joshua Kerievsky, ISBN 0-321-21335-1, Addison-Wesley

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Bicycle Repair Man and Eclipse

JLawhead's picture

It is also worth mentioning that Bicycle Repair Man is integrated into Eclipse through the excellent PyDev plug-in: http://pydev.sourceforge.net/

PyDev tries to offer all the great relevant tools available when working in Java to Python developers.

I also agree with the author that while the definition of refactoring can be abstracted to a very high architecture level most programmers use the term "refactor" to mean removing all of the hacks and shortcuts in code that were inserted because of a short timeline for a software demonstration, release, or for testing with components created by other developers on a team.

Bicycle Repair Man and Eclipse

JLawhead's picture

It is also worth mentioning that Bicycle Repair Man is integrated into Eclipse through the excellent PyDev plug-in: http://pydev.sourceforge.net/

PyDev tries to offer all the great relevant tools available when working in Java to Python developers.

I also agree with the author that while the definition of refactoring can be abstracted to a very high architecture level most programmers use the term "refactor" to mean removing all of the hacks and shortcuts in code that were inserted because of a short timeline for a software demonstration, release, or for testing with components created by other developers on a team.

Definition of refactoring

Dan Razzell's picture

Here is another instance where I think computer science is at risk of being obscured instead of aided by technology.

The article addresses refactoring as if it were concerned exclusively with code. It's not. Refactoring is primarily about architecture and only secondarily about code. In other words, the focus is on design more than implementation.

Peter Deutsch says, and I agree, that:

Interface design and functional factoring constitute the key intellectual content of software and are far more difficult to create or re-create than code.

Most of the work of refactoring a system consists of revising the relationships between its functional components, indeed adding and eliminating components as well. These are changes to structure, not details of implementation.

It needn't be a software system. It would be perfectly reasonable to refactor an automobile engine, for example, so that the timing chain might be driven off the clutch assembly rather than the generator assembly, or in a more extreme case the cylinder layout may change from inline to vee. In a broad sense, the engine is functionally unchanged. That is, it still meets its original design requirements. However, its structure may have changed radically.

When you refactor a system, you're taking a fresh look at its design tradeoffs. It's really a "big picture" exercise in which basic assumptions of modularity, flow of control, and data abstraction are reexamined. This may answer the author's misgivings about how we're supposed to "know that the refactored code is 'better'." It wouldn't be the code that's better but the design, and of course that is a matter best debated at the design level.

What then is the benefit of "refactoring" tools, since indeed many only pertain to code and not to design? Well, to belabor the engine analogy, a boring machine would be in a comparable sense an "engine refactoring" tool. It doesn't actually do any refactoring itself, but it allows you a way to implement the design changes which are the result of your refactoring efforts.

Definition of refactoring

Anonymous's picture

Your point is well taken. However, this column is about programming tools. My inclination is to be as concrete as I can. Since the title of this column is programming as opposed to design, and tools as in software that I can use to improve my programming experience, I chose to use the much narrower definition.

That said, if you are aware of any refactoring tools for the design portion of a system's life cycle, please let me know. I would be really interested.

Reg. Charney

Definition of refactoring

Dan Razzell's picture

I think your best bet for that approach would be to look at UML refactoring tools. A good overview can be found in Martin Fowler, Refactoring: Improving the Design of Existing Code.

This is not an endorsement of UML as the basis of refactoring, by the way, because I believe that misses the point. Refactoring is substantially a conceptual exercise. Like optimization, the biggest gains require an understanding of the problem domain. On the other hand, if UML is already part of your process, or if you're willing to make it one, then refactoring at that level would be completely appropriate. It certainly gets you closer to understanding the design issues of a project than if you were to go straight to the code.

Again, you have a valid point

R. Charney's picture

Again, you have a valid point. Do you or do any of our readers know of tools that would work on the UML description of a system?

Reg. Charney

M-x tags-query-replace

Anonymous's picture

enough said :-)

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix