Understanding dependencies – a smooth intro
Beside all the navigation, editing, and refactoring goodness that IDEA brings to the table, there is one feature that I really adore and find under-utilized by most teams: the dependency structure matrix (DSM).
It seems that many developers shy away because they expect it to be difficult, while in fact it is easy to understand and remember with just a little bit of intro. Here it comes.
Prerequisites: your project must compile. That’s it.
How to start: from the Analyze menu choose Analyze Dependency Matrix.
This is what you see (this is the code from my personal wiki – yes, it has only 5 classes. The code is under github.com/Dierk/MittieWiki)

Now, what does this mean? Well, it actually shows the layering of your architecture – not how you want it to be but how it actually is.
How would you depict the architectural layers in a document? Probably as some kind of pyramid with the common functionality as the fundamental bottom layers and users of the infrastructure on top of it. The DSM is exactly that!
If you want, you can imagine the pyramid being slightly pushed to the left such that the left edges align. Then you have exactly the lower-left triangle of the DSM. The upper layers depend on what is below them. They use the common base functionality.
The common base layers use nothing, therefore they go to the bottom. The uppermost layers are used by nobody.
For the moment, we don’t look at the numbers since we only seek to understand the structure.
The structure of the DSM shows that EventController (second line) uses classes below it. But which ones? Simply click on the EventController label and you will see this picture:

Every class that EventController actually uses gets an orange marker. Do yo see the one at CookService? From that we can tell that EventController uses CookService and no other project class!
CookService seems to be an interesting class. So let’s click on its label:

Do you see the green markers? They tell that CookService is used by EventController, FindController, and PageController.
From this intro you can infer that green markers should always be above the current selection and orange ones below.
Understanding the numbers
The numbers quite obviously show how often a class uses some other class. But which cell contains this information? Well, if you think about it, there is only one possible way of arranging numbers in cells that makes sense. Let’s revisit

The blue cross covers the number 5 saying that EventController uses CookService 5 times.
I mean, you only use classes below yourself, that means a cell that tells how often you use it must necessarily be placed below you, right? And where else should it be than in the row of the used class? And it is quite obviously in the column that responds to the using class.
The columns are not labeled explicitely but they are in same sequence as the rows and therefore you can simply use the diagonal as a “mirror”.
Fazit: usages along with usage counts are shown in the column below the diagonal!
With this knowledge we can anticipate what happens when we select the other controller classes. They equally show 5 usages of CookService.
Now, think in the opposite direction. Who uses our class?
EventController uses CookService 5 times. In other words: CookService is used by EventController 5 times. And the same is true for FindController and PageController. That’s three times the used-by count of 5 in the same row. This means: used-by along with its counts is shown in the row of the used class!
In cases where the situation is less obvious, you can select a single cell (here where the 5 is in) to see the whole picture:

So much for the intro.
A very comprehensive description with many use cases and screencasts is under http://www.jetbrains.com/idea/features/dependency_analysis.html . You will see that this intro helps you understanding it. Once you have understood the concept, you will easily grasp package dependencies, module dependencies, and cyclic references.
The DSM has become an indispensable tool for my consulting work. Whenever I visit a new project, it is my first tool to fire up. There is so much to tell from this simple matrix.
After a while, you start to see patterns like a horizontal line of dependencies (as in CookService) to show a service or utility class or a line just below the diagonal to show a strict layering, breaches of layering, and so on.
A word of caution
Static analysis never shows the whole truth. Especially, it doesn’t show semantic dependencies. Just recently I heard a friend advocating the use of dependency injection to avoid unwanted dependencies in the DSM. It is very easy to fool oneself with that approach if the semantic dependency is not resolved at the same time.
Actually, I’d rather like to see my unwanted dependencies in the DSM than hiding it with tricks (DI, AOP, “Object” in signatures, “def” in Groovy).
I hope this little intro motivated you to give the DSM a try.
keep groovin’
Dierk
@mittie
dzone: http://www.dzone.com/links/r/intellij_idea_tips.html