I want to use some C codes from a function, however, the function has 300+ lines of code and 20 variables, which make me really headache to deal with.
I'm just wondering whether there's such a tool that can separate(refactor) small functions from a large function automatically. Then it will save me a lot of time to deal with long functions like that.
(I don't think it's a very hard thing because this tool only need to analysis the position where a Variable was defined first time, and the position where a Variable was used at last, then give me some suggestion about where to split..)
The problem is that it is difficult for a computer to know what the purpose of a function is. If it so happens that all 20 of the variables and 300 lines of code are all needed for a task and it does not make sense to split that task up, then it is a good function. It is a little hard to imagine, but still.
Don't break something up just because it is long. Break it up because it makes sense to do so. Keep functions performing only one thing. As of now computers are not smart enough to figure out the purpose of a function and especially not smart enough to decide what are sub-sections of the function that could reasonably be split out.
Yes, there are tools than can do that. What you want is a program transformation tool that parses C into ASTs, can determine data flows within a function, and can transforms the ASTs using the dataflows as a guide, and can regenerate text from the transformed tree.
One of the issues you have to face is how to tell the tool where to split the function. What part do you want as a sub-function? Arguably you can designate any set of operations in the function (assignment, sub-expression, or statement) in the function to go into a sub-function and you need that much control in general; this can be a pretty long and detailed description (probably a set of line/column pairs). But often you want some automatic selection such as "put everything that supports only operation" into the subroutine; a tool with data flow analysis can then compute a backward slice from the operation, find out what part of the backward slice is only used for the operation, and move the corresponding code. As a practical matter you'll need to combine these ideas. In either case, the tool now knows what has to be moved. Moving designated code into a subroutine is "just" a set of standard program transformations; most compilers have internal machinery to do this.
You might even want the tool to offer interactive graphical sliced views of the code so that you can make this decision. (Grammatech offers a C slicing tool call CodeSurfer that does exactly this).
A tool that can compute such slices and do the code transformations is our DMS Software Reengineering Toolkit with its C Front End. (It isn't interactive like CodeSurfer; but then CodeSurfer can't change the code). You need to configure DMS to implement that specific task; it has all the machinery mentioned above. As a practical matter, that's likely more work than the effort it will take you to split your one function by hand.
Having said this, you are still stuck with deciding how to split the function. And that, not the tool, seems to be the actual problem you are having.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With