Code Clean up (refactoring?)

Discussion in 'C++' started by Dean Ware, Mar 3, 2005.

  1. Dean Ware

    Dean Ware Guest

    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.
     
    Dean Ware, Mar 3, 2005
    #1
    1. Advertising

  2. 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
     
    Jonathan Turkanis, Mar 3, 2005
    #2
    1. Advertising

  3. Dean Ware

    BigBrian Guest

    > 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.
     
    BigBrian, Mar 3, 2005
    #3
  4. Dean Ware

    BigBrian Guest

    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.
     
    BigBrian, Mar 3, 2005
    #4
  5. Dean Ware

    Phlip Guest

    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/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Mar 3, 2005
    #5
  6. Dean Ware

    Phlip Guest

    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/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Mar 3, 2005
    #6
  7. Dean Ware

    BigBrian Guest

    >>>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!
     
    BigBrian, Mar 3, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shuisheng
    Replies:
    4
    Views:
    352
    Jim Langston
    Mar 18, 2007
  2. Dennis Yurichev

    c code refactoring or optimizing

    Dennis Yurichev, Mar 27, 2007, in forum: C++
    Replies:
    3
    Views:
    364
    Jim Langston
    Mar 28, 2007
  3. Dennis Yurichev

    c code refactoring or optimizing

    Dennis Yurichev, Mar 27, 2007, in forum: C Programming
    Replies:
    6
    Views:
    441
    Ira Baxter
    Mar 28, 2007
  4. Stefan Ram
    Replies:
    0
    Views:
    444
    Stefan Ram
    Jan 18, 2008
  5. Replies:
    8
    Views:
    544
Loading...

Share This Page