views:

53

answers:

2

Has somebody tried any tools for design patterns recognition in source code?

I'm looking for one that is more or less effective and expects some reasonable input (for example, an xmi file produced by a reverse engineering tool or another data that can be easily generated from the source code).

I've found some academic tools like CrocoPat, Columbus + Maisa, but haven't tried them yet.

Would appreciate if someone shares his/her experience in such experiments.

A: 

I don't know of anything that works. There is a lot of information that gets lost when you realize your patterns in code so reverse engineering them is bound to fail.

  • Almost every method call on a field can be considered to be a a bridge/adapter/staregy
  • Almost every non-void method can be considered to be a factory (including, for example, toString())
  • Almost every object with fields and a non-void return method can be considered to be a Builder
  • Etc.
Itay
What the tool will search for depends on its input I guess. For example DP Miner accepts an xml description of the pattern to search for. If you describe Factory pattern as a non-void method you'll probably get all non-void methods as a result.
Kniganapolke
+1  A: 

Design patterns don't usually appear directly in code; usually the code contains an implementation of a design pattern.

To do such pattern recognition you need:

1) The ability to reliably parse the source language 2) The ability to recognize low-level code patterns that hint at a higher level pattern 3) The ability to tie sets of low-level pattern matches together via proximity or more usually control and data flow

There aren't a lot of tools that can do this. While we have not explicitly hunted for general design patterns, we have built a tool that meets the criteria above: our DMS Software Reengineering Toolkit. DMS can parse C, Java and COBOL at the same level of precision as their compilers, can pattern match using explicit patterns, and computes control and data flow analysis for those languages.

One interesting application of DMS was to recognize the "design pattern" for producing interactive screens in a 35 million line C application. You know this design pattern: "use printf to produce fragments of the screen output". While it is easy to recognize fragments of the pattern (most printf calls are examples), the real problem is to recognize the screen image itself from a tangled pile of code that implements it. The task we accomplished was extracting an image of printed screen output (reported as an XML file containing screen literal text and XML tags representing variable-content output) by tying together the individual printf calls. The details for accomplishing this are pretty complicated; you have to find sequences of printfs that typically cross function calls and are controlled by lots of conditionals (the XML recorded the conditional part of the screens, too). The tool produced screen images for all screens printable by the code starting from main :-}

If you are hoping to find an easy solution for design pattern recognition, I think you will not have a lot of luck. But it is possible.

Ira Baxter