Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refactoring of a large C++ function

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?

like image 740
Justin Ethier Avatar asked Dec 10 '22 20:12

Justin Ethier


1 Answers

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.

like image 74
paxdiablo Avatar answered Dec 22 '22 00:12

paxdiablo