
{"id":1637,"date":"2015-05-05T23:52:02","date_gmt":"2015-05-05T23:52:02","guid":{"rendered":"http:\/\/www.codergears.com\/Blog\/?p=1637"},"modified":"2015-05-06T14:21:03","modified_gmt":"2015-05-06T14:21:03","slug":"using-the-level-metric-to-understand-an-existing-c-code-base","status":"publish","type":"post","link":"https:\/\/codergears.com\/Blog\/?p=1637","title":{"rendered":"Using the Level metric to understand an existing code base"},"content":{"rendered":"<p>When we discuss the architecture of a code base, we often qualify a piece of a given code by using terms such as high level or low level. This is common vocabulary and we all intuitively know what it means. A piece of code A (whether it is a method, a class, a namespace, or an assembly) is considered as higher level than a piece of code B if A is using B while B doesn\u2019t know about A. From this simple definition, we can order pieces of a code in our program as shown in the following diagram:<!--more--><\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/Level1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1656\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/Level1.png\" alt=\"Level1\" width=\"442\" height=\"264\" \/><\/a><\/p>\n<p>Thus, we can say that our code that doesn\u2019t use anything has a level of 0, and we can say that our code that is using only tier code or code of level 0 has a level of 1. Etc\u2026<\/p>\n<p>We have just defined a code metric that is named Level. To be computed, the Level metric just need a graph of dependencies. If you consider the graph of dependencies between the methods of your program, you can assign a Level to your methods.<\/p>\n<p>The level metric could be used to understand how a use case works. The idea is to search for the methods called by a specific method with their levels and identify the complete workflow.<\/p>\n<p>When discovering a code base we don\u2019t know exactly where a use case begin, for this reason the level is very useful to detect high level methods using some low level treatments.<\/p>\n<p>Let&#8217;s analyze\u00a0the FileZila project using <a href=\"http:\/\/www.cppdepend.com\">CppDepend <\/a>and try to explain how the level metric could be helpful to understand the code base.<\/p>\n<p>FileZilla is free; cross-platform FTP application software consists of FileZilla Client and FileZilla Server.<\/p>\n<p>The main goal of FileZilla is to Transfer files in FTP, SFTP, and encrypted FTP such as FTPS and SFTP, for this reason it uses sockets. To discover methods related to\u00a0\u00a0the transfer, we have only to search for all methods using directly or indirectly the socketaddr_in struct.<\/p>\n<p><strong>Bottom-Up<\/strong><\/p>\n<p>High level methods using socketaddr_in and their levels of use:<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/fz1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1650\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/fz1.png\" alt=\"fz1\" width=\"460\" height=\"495\" \/><\/a><\/p>\n<p>We have identified two most high level methods using the sockets, and we can search now for all methods used by them to understand how they work.<\/p>\n<p><strong>Up-Bottom<\/strong><\/p>\n<p>The OnReceive method is the entry point of the treatments after receiving data. let&#8217;s search for\u00a0the first level\u00a0methods used by it.<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/fz2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1651\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/fz2.png\" alt=\"fz2\" width=\"791\" height=\"438\" \/><\/a><\/p>\n<p>To go deeper, we can search for the methods used in all levels. To do this, we can search for all the methods used directly or indirectly by OnReceive.<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/fz3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1652\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/fz3.png\" alt=\"fz3\" width=\"452\" height=\"497\" \/><\/a><\/p>\n<p>As we can discover in the treemap view which is shown in blue color , all the methods concerned with the above CQLinq query, more than 50% of the engine methods are invoked, therefore this query is not very interesting to explore.<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/fz5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1653\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/fz5.png\" alt=\"fz5\" width=\"820\" height=\"467\" \/><\/a><\/p>\n<p>However we can filter thie result and search only for methods used by OnReceive in the Level 1 and 2 and not existing in the third party librararies.<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/fz6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1654\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/fz6.png\" alt=\"fz6\" width=\"361\" height=\"540\" \/><\/a><\/p>\n<p>This result is more suitable to understand how the OnReceive method works.<\/p>\n<p><strong>Summary:<\/strong><\/p>\n<p>The Level metric is very useful to navigate across the entire codebase from botom to up and vice versa. Which help to understand\u00a0how some methods work internally.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When we discuss the architecture of a code base, we often qualify a piece of a given code by using terms such as high level or low level. This is common vocabulary and we all intuitively know what it means. A piece of code A (whether it is a method, a class, a namespace, or &hellip; <a href=\"https:\/\/codergears.com\/Blog\/?p=1637\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using the Level metric to understand an existing code base&#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-1637","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/1637","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=1637"}],"version-history":[{"count":18,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/1637\/revisions"}],"predecessor-version":[{"id":1677,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/1637\/revisions\/1677"}],"wp:attachment":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}