Code Clean up (refactoring?)

D

Dean Ware

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.
 
J

Jonathan Turkanis

Dean said:
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
 
B

BigBrian

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.
 
B

BigBrian

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.
 
P

Phlip

Dean said:
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.
 
P

Phlip

BigBrian said:
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.
 
B

BigBrian

The "trial and error" happens after the stage "detect duplication".
And I
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top