{"id":363,"date":"2018-03-02T14:33:23","date_gmt":"2018-03-02T14:33:23","guid":{"rendered":"http:\/\/cppdepend.com\/blog\/?p=363"},"modified":"2023-05-31T16:17:53","modified_gmt":"2023-05-31T16:17:53","slug":"tracking-cpp-code-smells-database-approach","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/tracking-cpp-code-smells-database-approach\/","title":{"rendered":"Tracking C++ Code Smells: Database Approach"},"content":{"rendered":"<p>Static analysis is not only about directly finding bugs, but also about finding bug-prone situations that can decrease code understanding and maintainability.\u00a0Static analysis can handle many other properties of the code:<!--more--><\/p>\n<ul>\n<li><strong>Code metrics<\/strong>: for example, methods with too many loops, if, else, switch, case\u2026 end up being non-understandable, hence non-maintainable. Counting these through the code metric\u00a0Cyclomatic Complexity\u00a0is a great way to assess when a method becomes too complex.<\/li>\n<li><strong>Dependencies<\/strong>: if the classes of your program are entangled, effects of any changes in the code becomes unpredictable. Static analysis can help to assess when classes and components are entangled.<\/li>\n<li><strong>Immutability<\/strong>: types that are used concurrently by several threads should be immutable, else you\u2019ll have to protect state read\/write access with complex lock strategies that will end up being un-maintainable. Static analysis can make sure that some classes remain immutable.<\/li>\n<li><strong>Dead code<\/strong>: dead code is code that can be removed safely, because it is not invoked anymore at runtime. Not only\u00a0<em>can\u00a0<\/em>it be removed, but it\u00a0<em>must<\/em>\u00a0be removed, because this extra code\u00a0add unnecessary\u00a0complexity to the program. Static analysis can find most of dead code in your program (yet not all).<\/li>\n<li><strong>API breaking change<\/strong>: if you present an API to your client, it is very easy to remove a public member without noticing and thus, breaking your clients code. Static analysis can compare two states of a program and can warn about this pitfall.<\/li>\n<li><strong>API usage<\/strong>: some APIs are intended to be used carefully. For example, a class that hold disposable fields must be itself disposable in general, except when\u00a0the disposable field lifetime is not aligned with\u00a0the class instances lifetime, which then sounds like\u00a0a\u00a0design problem.<\/li>\n<\/ul>\n<p>Code smell can be also considered as a bug-prone situation, here&#8217;s it&#8217;s definition from wikipedia:<\/p>\n<pre>In\u00a0computer programming,\u00a0<b>code smell<\/b>, (or\u00a0<b>bad smell<\/b>) is any\u00a0symptom\u00a0in the\u00a0source code\u00a0of a\u00a0program\u00a0that possibly indicates a deeper problem.\u00a0According to\u00a0Martin Fowler, \"a code smell is a surface indication that usually corresponds to a deeper problem in the system\".\u00a0Another way to look at smells is with respect to principles and quality:\u00a0\"smells are certain structures in the code that indicate violation of fundamental design principles and negatively impact design quality\". Code smells are usually not\u00a0bugs\u2014they are not technically incorrect and do not currently prevent the program from functioning. Instead, they indicate weaknesses in design that may be slowing down development or increasing the risk of bugs or failures in the future. Bad code smells can be an indicator of factors that contribute to\u00a0technical debt. Robert C. Martin\u00a0calls a list of code smells a \"value system\" for software craftsmanship.<\/pre>\n<p>Many interesting tools exist to detect bugs in your C++ code base \u00a0like cppcheck, clang-tidy and visual studio analyzer. But what about the detection of the bug-prone situations?<\/p>\n<p>If the static analysis tools creators could decide which situations are considered as bugs, it\u2019s not the case of the\u00a0code smells\u00a0cases which depends on the development team choices. For example a team could consider that a method with more than \u00a020 lines is complex, another team could define the max to 30. If a tool provides the detection of the code smells ,\u00a0it\u00a0must provides also the possibility to customize it.<\/p>\n<p><strong>Code as Data is the better way to detect the code smells<\/strong><\/p>\n<p>Static analysis is the idea of analyzing source code \u00a0for various properties and reporting on those properties, but it\u2019s also, philosophically, the idea of treating code as data.\u00a0 This is deeply weird to us as application developers, since we\u2019re very much used to thinking of source code as instructions, procedures, and algorithms.\u00a0 But it\u2019s also deeply powerful.<\/p>\n<p>After the source code analysis of a source file, we can extract its AST and generate a model\u00a0containing\u00a0 many interesting\u00a0data\u00a0 about the code. This way we can query it using a code query language similar to SQL.<\/p>\n<p><a href=\"http:\/\/www.cppdepend.com\/\">CppDepend\u00a0<\/a>provides a code query language named <a href=\"https:\/\/www.cppdepend.com\/cqlinq\">CQLinq<\/a> to query the code base like a database. Developers, designers and architects could define their custom queries to find easily the bug-prone situations.<\/p>\n<p>With CQlinq we can \u00a0combine the data from\u00a0the code metrics, dependencies,\u00a0API usage and other model\u00a0data to\u00a0\u00a0define very advanced queries that match some bug-prone situations.<\/p>\n<p>Here\u2019s an example \u00a0of a CQLinq query that matches the most complex methods:<\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/bugs.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1978\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/bugs.png\" alt=\"bugs\" width=\"461\" height=\"537\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<p>It\u2019s better to combine many C++ tools to detect some problems in your C++ code base, some tools detect bugs, some others detect also the bug-prone situations .With CppDepend we try to combine between many tools, indeed we provides an easy way to define your queries, but also\u00a0we can import \u00a0the result from other static analysis tools to query them with CQLinq.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Static analysis is not only about directly finding bugs, but also about finding bug-prone situations that can decrease code understanding and maintainability.\u00a0Static analysis can handle many other properties of the code:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[7,52,196,34],"class_list":["post-363","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c","tag-code-smells","tag-database","tag-static-analysis"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/comments?post=363"}],"version-history":[{"count":10,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/363\/revisions"}],"predecessor-version":[{"id":1492,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/363\/revisions\/1492"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}