I'm maintaining an old code base written in python. In particular there is a complex piece of code that from a module calls other functions from other modules that call other functions and so on. It is not OOP, just functions and modules.
I've tried to keep track where the flow begins and ends anytime I call the main function but I feel I need to draw this because I'm getting lost in the sub-calls.
What concerns me is that each function calls multiple external functions within their body to complete their task and return the value to the caller.
How can I draw this? Meaning what kind of chart/graphic would be appropriate to document this kind of behavior/code?
So, I don't think would be useful to draw an UML diagram, neither a flowchart.
A call graph, maybe?
Best Answer
I think what you're looking for here is a Sequence Diagram. These allow you to visualize the order in which various modules call eachother via the use of arrows.
Constructing one is simple:
Example
Let's assume we have the following code we want to create a sequence diagram for:
The first thing we'll draw is the entry point (
main
) connecting to the methodlong_division
. Note that this creates a box in long_division, signifying the scope of the method call. For this simple example, the box will be the entire height of our sequence diagram due to the fact that this is the only thing run.Now we call
find_largest_fit
to find the largest multiple that fits within our working number, and returns it to us. We draw a line fromlong_division
tofind_largest_fit
with another box to signify scope for the function call. Note how the box ends when the multiplier is returned; this is the end of that functions scope!Repeat a few times for a larger number and your chart should look something like this:
Notes
You can choose whether you want to label the calls with the variable names passed, or their values if you only want to document one specific case. You can also show recursion with a function calling itself.
Additionally, you can show users in here and prompt them and show their input into the system easily enough. It's a fairly flexible system that I think you'll find rather useful!