Using a Graph Database for Better Understanding of Unfamiliar Code

Using a Graph Database for Better Understanding of Unfamiliar Code

Like Dustin, I worked on a project for the AppMap Hackathon. I was particularly interested in understanding how to make the visualizations generated from recordings in AppMaps easier to understand.

The basic structure of code can be somewhat hidden by its use of trivial helper classes. Theses are classes with simple functions (getters, setters, predicates, etc) that don’t use other classes. I wanted to see if there was a way to use a graph database to help identify such classes in an unfamiliar code base.

I put together a simple JavaScript program that uses the CallTree class from the AppMap model code to a graph in a Neo4j database. For each function in the call tree, it creates a node for the class that contains the function. It creates an edge from that class to the classes containing each of the functions called.

Once that’s in place, a cypher query shows all the “leaf” classes, i.e. those that don’t call methods in other classes:

MATCH(n)
WHERE NOT (n)-[:CALLS]->()
RETURN n
ORDER BY n.defined_class
LIMIT 10;

This query returns 10 classes that don’t have a CALLS relationship with any other classes.

These are the simple classes I was looking for, and excluding them from AppMaps definitely improved the visualizations.



Originally posted on Dev.to

AppMap logo Get AppMap