Re: What Refactorings Would you Like for C



pingu219 wrote:
Hi I'm currently a student working on an honours-level project to
create a visual tool which will be able to represent C project files
and code as well as allow the user to perform high and certain low-
level refactorings.

At the moment I'm compiling a list of refactorings which are
applicable to C that people who program alot with structured
programming languages find useful.

So far I've got (some I got from someone else's post on another google
group):

- Rename variable/function
- Move variable/function
- Reduce scope of variable
- Publish function (make it public, put a declaration in the .h file)
- Perish function (make it static, remove it from the .h file)
- Add/Remove parameter from function
- Reorder function arguments
- Remove unnecessary includes

Extract a constant to a const local variable.
Extract a constant to a const global variable.
Extract a constant to a #define.
Convert a function into a #define macro.
Convert a #define function into a macro.
Inline a local variable (or assignment to one).
Extract a typedef from a variable declaration (including global
variable, local variable, and struct member).
Publish or perish a struct. Or a union, or a typedef.
Add 'extern' declaration for global to a .h file (or remove it).
Generate constant for length of initialized array using the
((sizeof array) / (sizeof type)) idiom.
Generate constant for length of initialized array using the
idiom where the constant has a literal value but the
compiler checks it's the right value.
Convert obnoxious for-loop into a while-loop.

And a few sillier ones:

Move pre- or post-increment operators from expression into
separate statement. (This one could easily be screwed
up and not yield equivalent code.)
Move pre- or post-increment operator into expression. (This
one could be screwed up even worse and would have to be
rejected sometimes even though it's valid at the level
of syntax.)
More generally, move anything that's an l-value (like an
assignment statement) into / out of an expression.

There are probably more. But as someone pointed out, with
the way C works, some of this could be tricky, or maybe even
impossible to do correctly all the time in the general case.
And if you can't do it in a way which is provably correct,
that's not as useful, because now you're making it easy to
do things with less time spent thinking about what it is that
you're doing.

- Logan
.



Relevant Pages

  • Re: Array definition guarantees
    ... A declaration *inside* a function with the 'static' storage class ... makes the variable have static storage duration (i.e., ... A declaration *outside* a function with the 'static' storage class, ... the variable/function is visible throughout file where ...
    (comp.lang.c)
  • Re: Array definition guarantees
    ... is there any guarantee as to the contents of each element? ... makes the variable have static storage duration (i.e., ... A declaration *outside* a function with the 'static' storage class, ... the variable/function is visible throughout file where ...
    (comp.lang.c)
  • Re: extern "C"
    ... Tim Roberts wrote: ... We need to add extern "C" to both variable/function ... Declaration only or both declaration and ...
    (microsoft.public.vc.language)