views:

203

answers:

4
+4  Q: 

Code refactoring

I am about to starting to work on a project which involves refactoring and modifying existing code which is in c & c++. The code is a bloated one and is in huge volume. Ofcourse since the code needs to be modified an understanding of the code has to be developed and in a very short span of time since we have some pretty time pressed project schedule. Can anyone please suggest any open source tools which will help in acheiving the above. In short what I am looking for is tool which can:

  1. Reverse engineering tools which will help understand the design.
  2. Sequence generator tools which will help draw sequences at run time(Most probably we will be able to run the code, but not in the initial stages atleast) or through code examination.
  3. Good code browsing tools to study the existing code base.
  4. Suitable tool which can auto refactor code with minimal efforts.

Please do tell me about any experiences,preferences or favorites worth looknig out for.

[EDIT] Came up with a list of tools to help in above. Here's the list:

  1. Graphviz & DOxygen
    Generate UML class diagrams from existing code base

  2. Umlstudio (http://www.pragsoft.com/prod_umls.html)
    Creating an object model for your OO legacy code is the best approach to analysing, understanding and maintaining it. UMLStudio can automatically convert C++, Java, CORBA IDL, PHP 5, and Ada 95 code into OOA&D notation faster than any other CASE tool.

  3. codedrawer for C++(http://www.codedrawer.com/)
    The CodeDrawer converts source code to visual based diagrams. Class, struct, and any elements of source code can be shown diagrams. It also shows the logics of a fuction and a method. The CodeDrawer helps understand source code of your project

  4. Imagix for c, c++ & java (http://www.imagix.com/)
    Reverse engineering and visualization of source code lead to improved program comprehension. Speeds: Learning Unfamiliar Code Change Impact Analysis Integrating Open Source Code Code Reuse Software Maintenance

  5. agilej (http://www.agilej.com/)
    AgileJ StructureViews is a plug-in for the Eclipse Java IDE which generates highly customisable UML class diagrams on an industrial scale, ideal for agile development or exploration of any existing Java codebase.

  6. MaintainJ (www.maintainj.com)
    If you can run the code base then MaintainJ generates UML sequence and class diagrams at runtime when you run a particular use case.

  7. reverse engineering tool(http://www.neiljohan.com/projects/reverse/)
    Generates class diagrams and relations between classes from java source code.

  8. source Insight (www.sourceinsight.com) Great source browsing software

One more, Thanks to Steve Townsend
Klocwork (http://www.klocwork.com/products/insight/architect-code-visualization/)

+6  A: 

The code is a bloated one and is in huge volume. Ofcourse since the code needs to be modified an understanding of the code has to be developed and in a very short span of time since we have some pretty time pressed project schedule.

Then you have a management problem : if you already know you have little time to UNDERSTAND a lot of code, you're doomed. To understand this code, you'll have to make it run and go through it and it will take time. Tools will just give you a big map of things but will not show you the real path.

Suitable tool which can auto refactor code with minimal efforts.

You're living in wonderland.

There are tools that gives you the structural architecture of you application, but that will not really help without going through each module one by one and read the code. First the code that use the modules, then the code inside the modules.

The fact that it's C and C++ makes it even harder to define the time that it will take you as it's also relative to the knowledge you have of those languages and the level of knowledge of the people who wrote the app.

Klaim
I do understand such a code cant be refactored just by itself, needs understanding, i know that is a problem and I am asking help to overcome.Also, as for the wonderland living I am hoping if theres some tool which can refactor certain small modular sections of the code.
Als
I said you're living in wonderland to point that refactoring is a human intelligence process that can only be done with the role of each part of code in mind, that is impossible to do by a software.Or maybe you just want a renaming or code reformating tool?
Klaim
ofcourse human intelligence is needed to refactor a source code, its a developed code afterall, but i am looking for tools which would make it easier to work with a voluminous code base.Any tool suggestions for that?
Als
If you use Visual Studio, I can only recommand Visual Assist to help on the renaming/applying macros.
Klaim
+1  A: 
  1. If you have the source, technically it's not reverse engineering. You can use the very good Doxygen to generate documentation (including diagrams - install GraphViz too!) : link
  2. No idea, not sure it exists.
  3. Your web browser, along with Doxygen, if you enable source code browsing. Visual Studio, with right click for jumping to definitions, and the debugger to step through the code and gain an understanding of its working (use and abuse the Step Out command).
  4. There are tools to do refactoring, but minimal effort and 'auto refactor' are very very hard to achieve, I don't think you find tools for that.
jv42
But to generate a doxygen doccument you do need to add comments using specific formatting right?
Als
You can extract everything even if it's not documented. It will give you a first outline. Then you can add comments so the documentation will improve over time. That's IMHO a good way to 'learn a project', and it will greatly improve code 'quality', measured in comments/code ratio.
jv42
+3  A: 

The first question you should ask is 'how do I make sure any changes we make do not break the system?'. Yet your question does not mention tests at all. That's a red flag to me.

I agree with others that tools are not the solution (not likely to turn up in the form you want, not likely to be 100% trustworthy on their own).

Your goal should be to quickly identify the area(s) to be changed - I would do this via inspection and running the code (preferably in existing tests - do you have them?), and make sure you have comprehensive unit and system test coverage on them before you touch a single line. Without that base, you are going to be flying blind and deadlines are way more at risk.

At the very least, if you don't have good tests, communicate this concern to your line upfront so that if things go pear-shaped you are seen to have raised the issue.

You could look at something like this - not used myself though: Klokwork Architect

Steve Townsend
Thanks for pointing out the testing point. Really important one.I missed to mention out Yes the testing framework exists and its a functional one. And we do have to prove the new softwares worth by providing test results after the refactoring/modifications. Ofcoure nothing like running the code and inspecting it as you mentioned and we would be looking up for that.But are there any tools that will help reverse engg code to say design charts to develop overal understanding? design docs are virtually non existent :( We are not looking to skip the code inspection and understanding by debugging.
Als
@Als - what is your platform, IDE and compiler?
Steve Townsend
@Steve Basically, it is a custom http and wap software stack for mobile devices. It has a good platform abstraction layer which ensures it can be ported to various mobile platforms. Currently, we have a working version for windows simulator using win32 api and using Microsoft visual studio. We will port it further on for other platforms like Brew etc.
Als
@Als - see post edit for one possible tool.
Steve Townsend
It seemed like a lot of it was questioning the OP premises and only the last sentence was anything that the OP could actually use.Sorry about not commenting straight up, I was just checking an answer from a question I just asked.
Tomas Cokis
@Tomas Cokis - thx
Steve Townsend
@Steve Townsend - Thanks! I did some google search and came up with a few good tools, will post them in as Edit just in case to help someone sailing in same boat as me :)
Als
A: 

There are very few tools that can parse and transform C and C++ code. As a first step, parsing of these languages is considered hard all by itself. Another key problem is the preprocessor, which is used pretty abusively (e.g., not in a structured way) in C programs and makes it difficult for a parser to see the program structure, and consequently difficult for an analyzer (needed to decide when refactorings are legal) to do its analysis correctly.

Modulo these glitches, our DMS Software Reengineering Toolkit can be configured to carry out refactorings on C and C++ code. DMS has industrial strength parsers for C and C++ and uniquely can capture most preprocessor uses as part of the internal code structures (typically people trying to parse C/C++ expand the preprocessor directives away, and that isn't an option for refactoring).

We've used it to carry out massive code reorganizations on C++ code (where preprocessor abuse is minimal because C++ has a variety of other ways to configure code). We also done some automated reengineering of C systems, but with somewhat more effort to handle the abusive preprocessor uses.

What it is not is interactive. You have to plan the refactoring transformations and specify them with a pattern matching language. But it is reliable.

Ira Baxter