Ideas for automatic refactoring of modules
I often have the problem that function and type definitions aggregate over
time in a single module (we can assume that a module corresponds to a
source file). At some point the source file is so big that it is barely
maintenable anymore. For example, one realizes the module contains a lot
of functions that are logically related to a handful of different topics,
and one feels that they should be in their own modules.
The idea is to have a tool that suggests ways to split up such a module.
The actual creation of new source files from the old ones could then be
made automatically.
I have the follwoing:
list of functions and data types, along with their direct dependencies on
other functions and types in the module.
from this, we can compute all dependencies for every item
we can do a topological sort, to make dependency groups. For example
(a, (b,c,d), e)
an item more left in the outer list does not depend on any ones further right
inner grouped items like (b,c,d) depend recursively on each other
The modules must form an acyclic hierarchy, i.e. it is not possible that
module A imports module B when B or one of the modules it imports already
imports A. From this it follows that (b, c, d) from the example above must
not be splitted across different modules.
Now I am somehow stuck and looking for a strategy to make sensible
suggestions based on the information found so far.
Of course, one possibility would be to split according to the
topologically sorted list of dependency groups. However, let us assume the
list starts thus:
(a, b, c, ....)
Where c depends on b and a, b on a and a on nothing. Here we could do the
following:
module ABC that defines a, b and c
module A that defines a, module BC that imports A and defines b and c
module C imports A and B and defines c, module B imports A and defines b
If we simply map dependencies between functions into dependencies between
modules we may end up with a module hierarchy that is too complex and fine
grained.
Somehow I must take further factors into account. Maybe some desired
module size, or number of imports.
Any advice, as well as pointers to existing software doing something like
this are most welcome.
No comments:
Post a Comment