Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Code Clean up (refactoring?)

Reply
Thread Tools

Code Clean up (refactoring?)

 
 
Dean Ware
Guest
Posts: n/a
 
      03-03-2005
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.



 
Reply With Quote
 
 
 
 
Jonathan Turkanis
Guest
Posts: n/a
 
      03-03-2005
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


 
Reply With Quote
 
 
 
 
BigBrian
Guest
Posts: n/a
 
      03-03-2005
> 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.

 
Reply With Quote
 
BigBrian
Guest
Posts: n/a
 
      03-03-2005
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.

 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      03-03-2005
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


 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      03-03-2005
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



 
Reply With Quote
 
BigBrian
Guest
Posts: n/a
 
      03-03-2005
>>>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!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Visual C++'s :"clean solution" doesn't clean the solution? plenty900@yahoo.com C++ 8 05-31-2008 04:57 PM
How do I clean a virus within an inbox or just clean only that infectedattachment or LOCATE AND delete just that attachment ? Vinayak Firefox 1 08-14-2006 06:19 PM
Need clean code sample for HttpWebRequest =?Utf-8?B?U3RldmU=?= ASP .Net 3 06-18-2005 08:14 AM
Clean up my Hard drive before selling it. Clean Registry? what else? baaab Computer Support 5 05-10-2005 08:30 AM
Clean code vs. efficiency Christopher Benson-Manica C++ 17 05-12-2004 07:09 PM



Advertisments