Wednesday, April 10, 2013

Detect Architectural Violations Using ConQAT

In the previous blog on how to divide code into component, I have outlined simple steps. One of them is to detect and count violations (invalid calls from one component to another).

The following are simple steps to detect such violations using ConQAT:
  1. Add a new Architecture Analysis. Go to Project>New>Other 
  2. Select ConQat>Architecture Specification: 
  3. A new architecture specification file is opened. Start adding components and assign classes, packages or namespaces to it: 
  4. Following the steps in the next screen shot to (1) Add component (2) Click on the component to open the properties window (3) Press the ‘New inclusion button’ (4) Write a pattern indicating the class or package name: 
  5. Now, we should run the architecture analyzer to verify the dependencies and catch any violations. To do that, we will create a Run configuration. Open “Project>New> Run Config": 
  6. Open “blocklib” you will find a long list of analysis blocks. Select the following: 
    • For .Net code: “ILArchitectureAnalysisChain” 
    • For java code: “JavaArchitectureAnalysis” 
  7. Add the required parameters as in the next screen shot: 
    1. There are some other useful properties like:
      • exclude-dependency: to exclude the specified namespace or class pattern from analysis
      • exclude-file: to exclude the specified file names from analysis
    1. Press the ‘Launch ConQAT analysis’ link 
    2. Go to the architecture specification file and press the ‘Switch Editor Mode’ button as in the screenshot below: 
    3. Browse for the ‘architecture-assessment.xml’ file under the output directory and select it. The result of the architecture analysis is displayed as in the following screenshot: 


    Some Tips and Tricks

    • The ‘orphans view’ lists all classes which are not assigned to any component. Review this list and make sure that it does not include any classes which may impact your analysis
    • You may use the ‘orphans view’ to incrementally build you architecture, as such:
      1. Create all you components and keep them empty
      2. Run the architecture analysis, you will find that all classes are orphans. 
      3. Drag and drop classes or complete packages on to the component
      4. If the package is not part of your code and need to be excluded, right click on the package and ‘Copy pattern’: 
      5. Then, you may exclude this pattern using the exclude-dependency property
    • You may right click the violations and take a decision to allow them, if appropriate:

      Tuesday, April 9, 2013

      A Simple Method for Dividing Code into Components

      In the 3Q's method, the second stage of refactoring legacy code is 'Divide and Conquer'. In this stage, we discover and separate (or simply reorganize) the code into manageable components. Usually, this is done as per the following simple workflow:

      discover and split your code into components
      What to do while dividing your code into components
      This is rather a straightforward logical sequence of modularizing legacy code. What we are interested now is how each step is done.

      In an experiment that I have done with one team, we have separated the classes into components and After the first step, this how it looked like :)

      Analysis of violations between the newly specified components
      Red lines indicate communication between components which should not be allowed. As you see, nearly there are violations between all components! This is very normal, because these components are specified for the first time; and previously, all classes were allowed to talk to all other classes in the application code.

      But, how did we detect these violations? In a later blog: Detect Architectural Violations Using ConQAT answers this question.

      Monday, April 8, 2013

      I'm Presenting At the Agile Conference 2013

      I'm presenting at Agile2013 inshaAllah :)

      What I will present is the result of last year's work on continuous refactoring of legacy applications. Here is my session proposal details: Refactoring as a Lifeline: Lessons Learned from Refactoring.

      The Agile conference is the biggest international conference about Agile Software development featuring more than 200 sessions delivered in 5 days. 

      Sunday, April 7, 2013

      Detect Exact and Similar Code Duplicates Using ConQat

      [Update: this post is related to ConQAT version 2011.9. However, the conQat blocks provided in this post will not work on the latest version which is 2014.1. Dr. Hummel (one of ConQat developers) has kindly shared with us the set of blocks for the newer version here. As per Dr. Hummel, the equality and similarity properties (described below) will be part of all future releases]

      Part of my work on refactoring legacy code was to find a way to identify code clones (duplicates) in large data repositories; and during my experiments on refactoring, I found that it would be much faster to follow this order:
      1. Remove dead code (CSRS ~ 600 LPH, also reduces 10% of duplicate code)
      2. Remove exact clones (CSRS ~ 300-400 LPH)
      3. Remove similar clones 
      Where:
      CSRS is the Cost Size Reduction Speed, measured in LOC per Hour (LPH)
      Exact clones: are clones which are typically the same (probably they may differ in white space only)
      Similar clones: are exact ones but with renamed variables.
      The best tool I found was ConQAT, an excellent open source and very flexible tool. However, I had to adjust it a bit to enable detecting exact and similar clones.

      Detecting Exact and Similar Code Duplicates:

      If you would like to try this, download this adjusted set of blocks, then follow these steps to replace the old ConQat blocks on your machine.
      1. Unzip the file “ConQat blocks - vX.Y.rar”, and replace the following folder: <conqat eclipse folder>\ conqat-2011.9\conqat\bundles\org.conqat.engine.code_clones\blocks
      2. Press the “Enforce Full Model Rebuild”:
      3. Open ConQat Runtime view and press “New”. You will find new parameters called “equality” and “similarity”: 
      4. Add the “equality” parameter, and set the equality threshold to 1 (meaning that the clones should be 100% identical): 

      See the Result!

      This is an example of the result, only exact clones are listed:


      Works for Which Language?

      This applies on (and has been tested against):
      • Java code using the JavaCloneAnalysis block
      • .Net code using the CsCloneAnalysis block
      • C/C++ code using the StatementCloneAnalysis block. Actually, the StatementCloneAnalysis may be applied on 10 different languages. Review the ConQat documentation for more information. 
      Enjoy!