1

Refactoring a large C ++ function

 2 years ago
source link: https://www.codesd.com/item/refactoring-a-large-c-function.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Refactoring a large C ++ function

advertisements

At work we have a legacy process written in Visual C++ that basically consists of a single 5000 line function. In essence the program is just one big case statement with similar cut-and-pasted code handling a fair amount of the logic for case. Obviously we would like to refactor this code to extract these cases out into separate functions (or objects) and to eliminate any cut-and-pasted code.

My question is - are there any suggestions for going about a refactoring effort of this size? Are there any automated tools that could streamline the process?


My first step would be to take some of the larger cases and first push them out into separate functions. That will reduce the visual clutter for a start and make it easier for you to do the next phase.

Secondly, identify the commonality of the different cases and create generalized functions to call in their stead. Up to a point. If you go too far, you'll have a generalized function that's every bit as bad as your current switch statement :-)

I've never seen a tool that can do even half the job of the spongy thing inside your skull. I'd suggest just using that.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK