By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,909 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,909 IT Pros & Developers. It's quick & easy.

Code Clean up (refactoring?)

P: n/a
Hi,

I am part way through developing a C++ application. I am developing it on my
own and using VC++ 6.0.

I am now at the stage where I wish to start tidying up my code.

I have lots of superfluous code that is hard to be sure about removing.

For example, I have classes that are ONLY used in methods that are never
called!

Are there any tools that will help me get rid of this stuff?

Are any available free?

What about C++ refactoring tools in general?

Thanks for any advice.

Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Dean Ware wrote:
Hi,

I am part way through developing a C++ application. I am developing
it on my own and using VC++ 6.0.

I am now at the stage where I wish to start tidying up my code.

I have lots of superfluous code that is hard to be sure about
removing.

For example, I have classes that are ONLY used in methods that are
never called!

Are there any tools that will help me get rid of this stuff?
Do you have a test suite? You could just start deleting classes one at a time
and see if any tests fail. ;-)
Thanks for any advice.


Jonathan
Jul 23 '05 #2

P: n/a
> I am part way through developing a C++ application. I am developing
it on my
own and using VC++ 6.0.

I am now at the stage where I wish to start tidying up my code.

I have lots of superfluous code that is hard to be sure about removing.
For example, I have classes that are ONLY used in methods that are never called!
IMHO, you should NEVER get to this point. Even when coding a throw
away prototype.
Are there any tools that will help me get rid of this stuff?
I wouldn't trust any of them.
Are any available free?
You get what you pay for. :)
What about C++ refactoring tools in general?


I sugguest you start with letting somebody else review your code. Good
design and coding can't be automated with a tool.

Jul 23 '05 #3

P: n/a
Having a test suite is a great idea. But the coder/designer has to use
it to determine by trial and error if a class isn't used, something is
VERY wrong. The OP should post some code, I'd love to see it.

Jul 23 '05 #4

P: n/a
Dean Ware wrote:
I am part way through developing a C++ application. I am developing it on my own and using VC++ 6.0.

I am now at the stage where I wish to start tidying up my code.
Hmm. A rather common metaphor will help here:

Refactoring is like cleaning the kitchen after each meal.

If you put it off, the first few meals take less time. However, the next few
take incrementally more time. Eventually, you must waste time with extra
tasks, such as evicting insect colonies from your kitchen. Very soon, the
cumulative cost of the meals exceeds the cost of simply making dinner and
cleaning up each day.

So, that is "refactoring" - minor tweaks to the code you just wrote, while
it's fresh in your mind, and before it gets much chance to grow buggy.
I have lots of superfluous code that is hard to be sure about removing.
Then you need unit tests that cover each feature. You may delete any line
you like, and run the tests. If they fail, you Undo to restore the line, and
try again with a different line.
For example, I have classes that are ONLY used in methods that are never
called!
How did those classes get there?
Are there any tools that will help me get rid of this stuff?

Are any available free?

What about C++ refactoring tools in general?


There are very few automated refactoring tool projects for C++. Our language
is very easy to break. Such a tool must, for example, track down pointer and
reference aliases to a variable before moving it.

I would clean your codebase like this:

Get CppUnit, or one of its many clones (including NanoCppUnit).

Start a new project, in a new folder, and start with a single empty test
case. Make it pass. (This effort demonstrates you have installed CppUnit
correctly, and attached it to <F5> correctly.)

Now find the simplest, stoopidest, lowest-level method in your old project.
Write a test case that will fail if that method doesn't work. Run the test
and ensure it fails for the correct reason.

Now import the _minimum_ part of that method which makes that test pass.

Add test cases for each aspect of each feature that you need. Never add code
without first writing a failing test to prove you need it.

Refactor the code, a little, between making each test pass. Refactor in
small steps, to remove duplication, and pass all the tests between the
smallest number of edits possible. If they fail, hit Undo until they pass.
Eventually, you will be using the original program only as inspiration.
Tests will drive your development.

This technique is called "Extract Algorithm Refactor", and it will replace
your old project with a new one that surprisingly does not have any of the
cruft you witness. And you can probably do it in 1 day per 1,000 lines of
target code.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 23 '05 #5

P: n/a
BigBrian wrote:
Having a test suite is a great idea. But the coder/designer has to use
it to determine by trial and error if a class isn't used, something is
VERY wrong. The OP should post some code, I'd love to see it.


That "trial and error" happens when you resolve duplication. Consider
Extract Method Refactor. First you write the new method. Nothing calls it
yet. You run all the tests, to show that the method's mere existence is not
(yet) harmful to the application.

Then, you delete code at one call site, and replace it with a call to the
new method. Pass the tests.

Repeat for each call site.

The "trial and error" happens after the stage "detect duplication". And I
agree that you must start there, and can't just delete lines willy nilly.
However, the "trial and error" stage still occurs.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces

Jul 23 '05 #6

P: n/a
>>>The "trial and error" happens after the stage "detect duplication".
And I
agree that you must start there, and can't just delete lines willy nilly.However, the "trial and error" stage still occurs.


Agreed. But Jonathan Turkanis's post said "just start deleting classes
one at a time and see if any tests fail", which seemed to suggest using
the test suit for "trial and error". Also, the OP seems to have the
idea that a tool can just do all of the work. I guess I was a little
annoyed at this because it seems that they're just being lazy. Writing
good code takes work, which the OP seems unwilling to do. I know I'd
never trust my codebase to some automated tool that's going to remove
classes!

Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.