Walk the streets of your codebase: inFusion, Code City and a MOOSE

Moose is a framework built in Smalltalk for code analysis. Here’s a brief guide to get it up and running (at least on OS X). One very interesting application which is built on top of it is called CodeCity built by Richard Wettel. The instructions below are how you go about installing and using CodeCity. The picture above is a code city generated from a project I worked on.

The size of the building is determined by the number of methods, the width and length by the number of attributes and the colour by the number of lines of code (lighter blue is more lines of code). The “districts” are packages. You can see straight away from this view that we have one large “CBD” which is a result of us having a single package called “model” rather than more granular, domain related packages (.acccount, .customer, etc) which would be my preference.

A second immediate observation is that we have a syscraper; what is this? In this case, its actually a stub class, which shouldnt even be in the production codebase. Straight away we have found some value from the visualisation. I tend to believe that this may be a result of our IDE tools (such as CTRL+SHIFT+T or CTRL+N which allow us to browse classes by name). Whilst great for encouraging Ubiquitous language I think this view of the codebase is very focused and heads down, and when concentrating on delivery, perhaps we need to come up from the detail level every now and again. Its why I enjoy the package view in IntelliJ in which multiple source folders together and presents classes as top level “leaves” in the tree so that its easy to see the structure of your packages, compare to eclipse.

I recently printed out the city and stuck it on the wall and was pleasantly surprised by the interest it generated by passers by, so I think its a really useful tool for showing the team something which is inherently hard to “see”.

These instructions are for a java project.

1) Download inFusion

Although its commercial software you can download it and use it for free. It will read in your source code and save it to a file in Moose (Famix) format. This can then be used by CodeCity.

You can download inFusion here. On its own, inFusion is a useful metrics tool and provides some interesting visuals (see below) which I won’t cover in detail here. A good reference is Michele Lanzas’ work Object-oriented metrics in practice.

You can run inFusion as a gui to explore your codebase, or if you just want to get to code city, you can run it as a batch file.

To run the ui just go into

$: cd /Applications/inFusion/tools
$: ./inFusion.sh

or (windows)

C:> inFusion.bat

To export the codebase in famix format (must be 2.1):

$: cd /Applications/inFusion/tools
$: ./java2mse.sh $SOURCE_DIR famix21 $TARGET_FILE

or (Windows)

C:> cd C:\development\external\inFusion\tools\inFusion
C:> java2mse.bat $SOURCE_DIR famix21 $TARGET_FILE

You may need to chmod +x the file.

On windows you can run the .bat file.

2) Load your source code

(theres a folder icon on the right). This takes a while and eventually presents you with a html view of your project stats in the bottom window.

An example of the output is shown here:

To do anything interesting, you right click (or two finger-click on a macbook trackpad) over “java” this will be whatever the name of your root directory is, maybe “src”.

One option there is to Run tool -> Famix 2.1 exporter. This is the format you want to export for code city. Its important that its 2.1

Again you might have to chmod +x the script.

Notice that you can also run another extremely cool tool, called DuDe which analyses duplicate code fragments in your code base.

3) Now your ready for code city

Download it from here.

There are versions for windows os x and linux.

The os x app is very handy and simply runs.

4) Import your MSE file

“Import Model(s) from …” “an MSE file” button.

5) View the city!

There are some keyboard shortcuts which werent obvious to me (help -> key mapping). You do W to “zoom in”, “S” to zoom out, “A” to move the whole scene left and “D” to move the whole scene right.

You can then navigate around using the arrow keys. If you press CTRL with the arrows, you rotate around the sphere of observation.

Some other cool features are to toggle contextual edges on (its a menubar icon, hover over them). You can configure contextual edges with incoming or outgoing invocations and then select an object and see “wires” indicating where the connections are.

Other features:

Make a snapshot of the picture as png
Import a set of MSE files and see an evolving city
View class blueprint of the classes

Enjoy your city!

Share