
{"id":1691,"date":"2015-05-10T13:39:47","date_gmt":"2015-05-10T13:39:47","guid":{"rendered":"http:\/\/www.codergears.com\/Blog\/?p=1691"},"modified":"2015-05-10T13:43:56","modified_gmt":"2015-05-10T13:43:56","slug":"exploring-existing-code-architecture-using-dependency-graph","status":"publish","type":"post","link":"https:\/\/codergears.com\/Blog\/?p=1691","title":{"rendered":"Exploring existing code architecture using dependency graph"},"content":{"rendered":"<p>Dependency graph\u00a0offers a wide range of facilities to help user exploring an Existing Code Architecture. In this article you&#8217;ll learn how to benefit from these features in order to achieve most popular Code Exploration scenarios:<\/p>\n<ul>\n<li>Call Graph<\/li>\n<li>Class Inheritance Graph<\/li>\n<li>Coupling Graph<\/li>\n<li>Path Graph<\/li>\n<li>All Paths Graph<\/li>\n<li>Cycle Graph<\/li>\n<li>Large Graph visualized with Dependency Structure Matrix<\/li>\n<\/ul>\n<p><!--more--><\/p>\n<p><a href=\"http:\/\/www.cppdepend.com\">CppDepend <\/a>will be used to generate these graphs.<\/p>\n<p><strong>Call Graph<\/strong><\/p>\n<p>To\u00a0generate any call graph you might need with a two steps procedure.<\/p>\n<ul>\n<li style=\"color: #555555;\"><span style=\"font-weight: bold;\">First<\/span>: Ask for direct and indirect callers\/callees of a type, a field, a method, a namespace or a project. The effect is that the following CQLinq query is generated to match all callers or callees asked.<img decoding=\"async\" id=\"img4\" class=\"img-responsive\" src=\"http:\/\/cppdepend.com\/Doc\/VS_Arch\/CallGraph1.png\" alt=\"\" \/><\/li>\n<\/ul>\n<p><span style=\"color: #555555;\">Notice that, in the CQLinq query result, the metric\u00a0<\/span><i>DepthOfIsUsing\/DepthOfIsUsedBy<\/i><span style=\"color: #555555;\">\u00a0shows depth of usage (1 means direct, 2 means using a direct user etc&#8230;). The CQLinq query can easily be modified to only match indirect callers\/callees with a certain condition on depth of usage.Notice also that callers\/callees asked are not necessarily of the same kind of the concerned code element. For example here we ask for methods that are using directly or indirectly a type.<\/span><\/p>\n<ul>\n<li style=\"color: #555555;\"><span style=\"font-weight: bold;\">Second<\/span>: Once the CQLinq query matches the set of callers\/callees that the user wishes, the set of matches result can be exported to the Dependency Graph. This has for effect to show the call graph wished.<img loading=\"lazy\" decoding=\"async\" id=\"img5\" class=\"img-responsive\" title=\"\" src=\"http:\/\/cppdepend.com\/Doc\/VS_Arch\/CallGraph2.png\" alt=\"\" width=\"697\" height=\"440\" \/><\/li>\n<\/ul>\n<p><span id=\"ctl00_MyContentPH_Label9\" style=\"font-weight: bold; color: #496ca0;\">Class Inheritance Graph<\/span><\/p>\n<p>To display a Class of Inheritance Graph, the same two steps procedure shown in the precedent section (on generating a Call Graph) must be applied.<\/p>\n<ul>\n<li style=\"color: #555555;\"><span style=\"font-weight: bold;\">First<\/span>: Generate a CQLinq query asking for the set of classes that inherits from a particular class (or that implement a particular interface). Here, the following CQLinq\u00a0query is generated:<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"img6\" class=\"img-responsive aligncenter\" src=\"http:\/\/cppdepend.com\/Doc\/VS_Arch\/Inherit1.png\" alt=\"\" width=\"399\" height=\"600\" \/><\/p>\n<ul>\n<li style=\"color: #555555;\"><span style=\"font-weight: bold;\">Second<\/span>: Export the result of the CQLinq query to the Dependency Graph to show the inheritance graph wished.<img decoding=\"async\" id=\"img7\" class=\"img-responsive\" src=\"http:\/\/cppdepend.com\/Doc\/VS_Arch\/Inherit2.png\" alt=\"\" \/><\/li>\n<\/ul>\n<p><span id=\"ctl00_MyContentPH_Label11\" style=\"font-weight: bold; color: #496ca0;\">Coupling Graph<\/span><\/p>\n<p><span id=\"ctl00_MyContentPH_Label12\">It might be needed to know which code elements exactly are involved in a particular dependency. Especially when one needs to anticipate the impact of a structural change. In the screenshoot below, the CppDepend Info panel describes a coupling between 2 projects.<\/span><\/p>\n<p>From pointing a cell in the dependency matrix, it says that X types of an project A are using Y types of an project B. Notice that you can change the option\u00a0<i>Weight on Cell<\/i>\u00a0to\u00a0<i># methods<\/i>,\u00a0<i># members<\/i>\u00a0or\u00a0<i># namespaces<\/i>, if you need to know the coupling with something else than types.<\/p>\n<p><img decoding=\"async\" id=\"img8\" class=\"img-responsive\" title=\"\" src=\"http:\/\/cppdepend.com\/Doc\/VS_Arch\/Coupling1.png\" alt=\"\" \/><\/p>\n<p>Just left clicking the matrix cell shows the coupling graph below.<\/p>\n<p><img decoding=\"async\" id=\"ctl00_MyContentPH_Image2\" class=\"img-responsive\" src=\"http:\/\/cppdepend.com\/Doc\/VS_Arch\/Coupling2.png\" alt=\"\" \/><\/p>\n<p>A coupling graph can also be generated from an edge in the dependency graph. Here, you can adjust the option\u00a0<i>Edge Thickness<\/i>\u00a0to something else than\u00a0<i># type<\/i>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"img9\" class=\"img-responsive\" title=\"\" src=\"http:\/\/cppdepend.com\/Doc\/VS_Arch\/Coupling3.png\" alt=\"\" width=\"792\" height=\"492\" \/><\/p>\n<p><span id=\"ctl00_MyContentPH_Label13\" style=\"font-weight: bold; color: #496ca0;\">Path Graph<\/span><\/p>\n<p><span id=\"ctl00_MyContentPH_Label14\">If you wish to dig into a path or a dependency cycle between 2 code elements, the first thing to do is to show the dependency matrix with the option\u00a0<i>Weight on Cells:<\/i>\u00a0<span style=\"font-weight: bold;\">Direct &amp; indirect depth of use<\/span>.<\/span><\/p>\n<p>Matrix blue and green cells will represent\u00a0<i>paths<\/i>\u00a0while black cells will represent\u00a0<i>dependency cycles<\/i>. For example, here, the Info panel tells us that there is a path of minimal length 7 between the 2 types involved.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"img10\" class=\"img-responsive\" title=\"\" src=\"http:\/\/cppdepend.com\/Doc\/VS_Arch\/Path1.png\" alt=\"\" width=\"778\" height=\"442\" \/><\/p>\n<p>Just left clicking the cell shows the path graph below.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"ctl00_MyContentPH_Image1\" class=\"img-responsive aligncenter\" src=\"http:\/\/cppdepend.com\/Doc\/VS_Arch\/Path2.png\" alt=\"\" width=\"162\" height=\"470\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span id=\"ctl00_MyContentPH_Label24\" style=\"font-weight: bold; color: #496ca0;\">All Paths Graph<\/span><\/p>\n<p><span id=\"ctl00_MyContentPH_Label25\">In certain situations, you&#8217;ll need to know about all paths from a code element A to a code element B. For example, here, the Info panel tells us that there is a path of minimal length 2 between the 2 types involved.<\/span><\/p>\n<p><img decoding=\"async\" id=\"img14\" class=\"img-responsive\" src=\"http:\/\/cppdepend.com\/Doc\/VS_Arch\/AllPaths1.png\" alt=\"\" \/><\/p>\n<p>Here&#8217;s the CQLinq query generated to search for the all paths<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"ctl00_MyContentPH_Image5\" class=\"img-responsive aligncenter\" src=\"http:\/\/cppdepend.com\/Doc\/VS_Arch\/AllPaths2.png\" alt=\"\" width=\"499\" height=\"582\" \/><\/p>\n<p>Finally exporting to the graph the 12 types matched by the CQLinq query, shows all paths from A to B.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"img15\" class=\"img-responsive aligncenter\" src=\"http:\/\/cppdepend.com\/Doc\/VS_Arch\/AllPaths3.png\" alt=\"\" width=\"658\" height=\"452\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span id=\"ctl00_MyContentPH_Label1\" style=\"font-weight: bold; color: #496ca0;\">Cycle Graph<\/span><\/p>\n<p><span id=\"ctl00_MyContentPH_Label2\">As we explained in the previous section, to deal with dependency cycle graphs, the first thing to do is to show the dependency matrix with the option\u00a0<i>Weight on Cells:<\/i>\u00a0<span style=\"font-weight: bold;\">Direct &amp; indirect depth of use<\/span>. Black cells then represent cycles.<\/span><\/p>\n<p>For example, here, the Info panel tells us that there is a dependency cycle of minimal length 5 between the 2 types involved.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"img11\" class=\"img-responsive\" title=\"\" src=\"http:\/\/cppdepend.com\/Doc\/VS_Arch\/Cycle1.png\" alt=\"\" width=\"698\" height=\"437\" \/><\/p>\n<p>Just left clicking the cell shows the cycle graph below.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"ctl00_MyContentPH_Image3\" class=\"aligncenter\" src=\"http:\/\/cppdepend.com\/Doc\/VS_Arch\/Cycle2.png\" alt=\"\" width=\"319\" height=\"489\" \/><\/p>\n<p>We&#8217;d like to warn that obtaining a clean &#8217;rounded&#8217; dependency cycle as the one shown above, is actually more an exceptional situation than a rule.<\/p>\n<p>Often, exhibiting a cycle will end up in a not &#8217;rounded&#8217; graph as the one shown below. In this example, the minimal length of a cycle between the 2 types involved (in yellow) is 12. Count the number of edges crossed from one yellow type to the other one, and you&#8217;ll get 12. You&#8217;ll see that some edges will be counted more than once.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"ctl00_MyContentPH_Image4\" class=\"img-responsive aligncenter\" src=\"http:\/\/cppdepend.com\/Doc\/VS_Arch\/Cycle3.png\" alt=\"\" width=\"470\" height=\"550\" \/><\/p>\n<p><span id=\"ctl00_MyContentPH_Label15\" style=\"font-weight: bold; color: #496ca0;\">Large Graph visualized with Dependency Structure Matrix<\/span><\/p>\n<p>Here, we&#8217;d like to underline the fact that when the dependency Graph becomes unreadable, it is worth switching to the dependency Matrix.<br \/>\nBoth dependency Graph and dependency Matrix co-exist because:<\/p>\n<ul>\n<li style=\"color: #555555;\">Dependency Graph is intuitive but becomes unreadable as soon as there are too many edges between nodes.<\/li>\n<li style=\"color: #555555;\">Dependency Matrix requires time to be understood, but once mastered, you&#8217;ll see that\u00a0<span style=\"font-weight: bold;\">the Dependency Matrix is much more efficient than the Dependency Graph to explore an existing architecture<\/span>. More information on the Dependency Matrix readability can be found in\u00a0<a style=\"color: #555555;\" href=\"http:\/\/codebetter.com\/blogs\/patricksmacchia\/archive\/2009\/08\/24\/identify-code-structure-patterns-at-a-glance.aspx\" target=\"_blank\">Identify Code Structure Patterns at a Glance<\/a><\/li>\n<\/ul>\n<p><span id=\"ctl00_MyContentPH_Label16\">To illustrate the point, find below the same dependencies between 77 namespaces shown through Dependency Graph and Dependency Matrix.<\/span><\/p>\n<p><img decoding=\"async\" id=\"img12\" class=\"img-responsive\" title=\"\" src=\"http:\/\/cppdepend.com\/Doc\/VS_Arch\/LargeGraph.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" id=\"img13\" class=\"img-responsive\" title=\"\" src=\"http:\/\/cppdepend.com\/Doc\/VS_Arch\/LargeMatrix.png\" alt=\"\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dependency graph\u00a0offers a wide range of facilities to help user exploring an Existing Code Architecture. In this article you&#8217;ll learn how to benefit from these features in order to achieve most popular Code Exploration scenarios: Call Graph Class Inheritance Graph Coupling Graph Path Graph All Paths Graph Cycle Graph Large Graph visualized with Dependency Structure &hellip; <a href=\"https:\/\/codergears.com\/Blog\/?p=1691\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Exploring existing code architecture using dependency graph&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-1691","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/1691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1691"}],"version-history":[{"count":10,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/1691\/revisions"}],"predecessor-version":[{"id":1700,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/1691\/revisions\/1700"}],"wp:attachment":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}