{"id":2068,"date":"2024-09-15T08:39:40","date_gmt":"2024-09-15T08:39:40","guid":{"rendered":"https:\/\/cppdepend.com\/blog\/?p=2068"},"modified":"2024-09-15T08:39:47","modified_gmt":"2024-09-15T08:39:47","slug":"a-look-inside-the-macchina-sdk-source-code-clean-design-and-implementation","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/a-look-inside-the-macchina-sdk-source-code-clean-design-and-implementation\/","title":{"rendered":"A Look Inside the Macchina SDK Source Code: Clean Design and Implementation."},"content":{"rendered":"\n<p>In C++, many libraries can aid in implementing an IoT application, but most are low-level. For a high-level SDK,\u00a0<a href=\"https:\/\/macchina.io\/index.html\">Macchina.io<\/a>\u00a0is an excellent choice, especially if you seek a robust framework that simplifies IoT application creation.<\/p>\n\n\n\n<p>Macchina is not only a perfect solution for IoT applications but it&#8217;s also a well designed and implemented project, so the SDK users could easily understand and customize its behavior.<\/p>\n\n\n\n<p>Let&#8217;s take a look inside the Macchina source code using CppDepend and discover some facts about its design and implementation.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p><strong>Clean Design<\/strong><\/p>\n\n\n\n<p>When designing a C++ project with a well-organized folder structure, it&#8217;s important to consider <strong>modularity<\/strong>, <strong>maintainability<\/strong>, and <strong>scalability<\/strong>. Organizing your code by folders allows you to separate concerns, making it easier to navigate, modify, and extend.<\/p>\n\n\n\n<p>As we can discover from the following DSM. The Macchina SDK is well structured using folders:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"548\" height=\"562\" src=\"https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image.png\" alt=\"\" class=\"wp-image-2070\" srcset=\"https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image.png 548w, https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-293x300.png 293w\" sizes=\"auto, (max-width: 548px) 85vw, 548px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>The technical layer based on the POCO library is isolated in the platform folder.  The\u00a0<strong>POCO C++ Libraries<\/strong>\u00a0are powerful cross-platform open-source C++ libraries for building network- and internet-based applications that run on desktop, server, mobile, IoT, and embedded systems.<\/p>\n\n\n\n<p>Here&#8217;s the dependency graph of some POCO libraries. <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"989\" height=\"493\" src=\"https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-8.png\" alt=\"\" class=\"wp-image-2078\" srcset=\"https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-8.png 989w, https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-8-300x150.png 300w, https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-8-768x383.png 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n\n\n\n<p>Let&#8217;s take a look inside the Foundation project to explore its structure:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"356\" height=\"473\" src=\"https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-9.png\" alt=\"\" class=\"wp-image-2079\" srcset=\"https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-9.png 356w, https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-9-226x300.png 226w\" sizes=\"auto, (max-width: 356px) 85vw, 356px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>The namespaces in the source code are used for 3 differents goals:\u00a0<\/p>\n\n\n\n<p><strong>1- Modularize the application<\/strong><\/p>\n\n\n\n<p>Modern C++ librarires use extensively the namespaces to modalirize their code base, and they use the &nbsp;\u201cNamespace-by-feature\u201d approach.&nbsp;Namespace-by-feature uses namespaces&nbsp;to reflect the feature set. It places all items related to a single feature (and only that feature) into a single namespace. This results in namespaces&nbsp;with high cohesion and high modularity, and with minimal coupling between namespaces. Items that work closely together are placed next to each other.<\/p>\n\n\n\n<p><strong>2- Anonymous namespace.<\/strong><\/p>\n\n\n\n<p>Namespace with no name avoids making global static variable. The \u201canonymous\u201d namespace you have created will only be accessible within the file you created it in.<\/p>\n\n\n\n<p><strong>3- Hiding details by convention<\/strong><\/p>\n\n\n\n<p>In C++  where there&#8217;s no way to hide public types to the library user (In\u00a0C# the \u201cinternal\u201d keyword did the job). It\u2019s\u00a0interesting to find a way to inform the library user that he dont need to use directly some specific types because they concern only the implementation.\u00a0<\/p>\n\n\n\n<p>A common idiom in modern C++, pioneered by the developers of the Boost libraries, is to separate symbols that form part of the implementation of your module (that is, don\u2019t form part of the public API) but that have to be publicly available into a separate sub-namespace, by convention named detail.<\/p>\n\n\n\n<p>The namespaces Poco.Details and Poco.Dynamic.Impl are used to hide the implmentation by convention.<\/p>\n\n\n\n<p><strong>Loose coupling enforced for more flexibility<\/strong><\/p>\n\n\n\n<p id=\"d2eb\">Loose coupling is desirable because changes in one area of an application will require fewer changes in other parts of the system. In the long run, this can save significant time, effort, and costs associated with modifying and adding new features to the application.<\/p>\n\n\n\n<p id=\"e4a1\">Loose coupling can be achieved by using abstract classes or generic types and methods.<\/p>\n\n\n\n<p>The Macchina source code contains more than 300 abstract classes:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"350\" height=\"549\" src=\"https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-2.png\" alt=\"\" class=\"wp-image-2072\" srcset=\"https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-2.png 350w, https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-2-191x300.png 191w\" sizes=\"auto, (max-width: 350px) 85vw, 350px\" \/><\/figure>\n\n\n\n<p>For example, the client authenticator class is an abstract class that allows for multiple implementations, providing flexibility in the user authentication feature.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"350\" height=\"455\" src=\"https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-1.png\" alt=\"\" class=\"wp-image-2071\" srcset=\"https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-1.png 350w, https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-1-231x300.png 231w\" sizes=\"auto, (max-width: 350px) 85vw, 350px\" \/><\/figure>\n\n\n\n<p><strong>High cohesion is enforced<\/strong> <\/p>\n\n\n\n<p id=\"8c0d\">The single responsibility principle states that a class should not have more than one reason to change. Such a class is said to be cohesive. A high LCOM value generally pinpoints a poorly cohesive class. There are several LCOM metrics. The LCOM takes its values in the range [0\u20131]. The LCOM HS (HS stands for Henderson-Sellers) takes its values in the range [0\u20132]. A LCOM HS value highest than 1 should be considered alarming. Here are to compute LCOM metrics:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p id=\"b9da\"><em>LCOM = 1 \u2014 (sum(MF)\/M*F)<br>LCOM HS = (M \u2014 sum(MF)\/F)(M-1)<\/em><\/p>\n<\/blockquote>\n\n\n\n<p id=\"fa36\">Where:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>M is the number of methods in class (both static and instance methods are counted, it includes also constructors, properties getters\/setters, events add\/remove methods).<\/li>\n\n\n\n<li>F is the number of instance fields in the class.<\/li>\n\n\n\n<li>MF is the number of methods of the class accessing a particular instance field.<\/li>\n\n\n\n<li>Sum(MF) is the sum of MF over all instance fields of the class.<\/li>\n<\/ul>\n\n\n\n<p id=\"fdfc\">The underlying idea behind these formulas can be stated as follow: a class is utterly cohesive if all its methods use all its methods use all its instance fields, which means that sum(MF)=M*F and then LCOM = 0 and LCOMHS = 0.<\/p>\n\n\n\n<p id=\"d2aa\">LCOMHS value higher than 1 should be considered alarming.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"576\" height=\"579\" src=\"https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-3.png\" alt=\"\" class=\"wp-image-2073\" srcset=\"https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-3.png 576w, https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-3-298x300.png 298w, https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-3-150x150.png 150w\" sizes=\"auto, (max-width: 576px) 85vw, 576px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Clean Implementation<\/strong><\/p>\n\n\n\n<p><strong>Few big types in the code base<\/strong><\/p>\n\n\n\n<p>Big types are extremely complex to develop and maintain. Thes kind of classes could controls too many other classes in the system and has grown beyond all logic to become The Class That Does Everything.<\/p>\n\n\n\n<p>In the Macchina SDK source code only few types are big.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"575\" height=\"582\" src=\"https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-4.png\" alt=\"\" class=\"wp-image-2074\" srcset=\"https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-4.png 575w, https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-4-296x300.png 296w\" sizes=\"auto, (max-width: 575px) 85vw, 575px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Methods are small and easy to understand<\/strong><\/p>\n\n\n\n<p>Many metrics exist to detect complex functions, NBLinesOfCode,Number of parameters and number of local variables are the basic ones.<\/p>\n\n\n\n<p>There are other interesting metrics to detect complex functions:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Cyclomatic complexity is a popular procedural software metric equal to the number of decisions that can be taken in a procedure.<\/li>\n\n\n\n<li>Nesting Depth\u00a0is a metric defined on methods that is relative to the maximum depth\u00a0of the more nested scope in a method body.<\/li>\n\n\n\n<li>Max Nested loop is\u00a0equals the maximum level of loop nesting in a function.<\/li>\n<\/ul>\n\n\n\n<p>The max value tolerated for these metrics depends more on the team choices, there are no standard values.<\/p>\n\n\n\n<p>In the Macchina SDK source code only few methods could be considered as complex.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"581\" height=\"580\" src=\"https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-5.png\" alt=\"\" class=\"wp-image-2075\" srcset=\"https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-5.png 581w, https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-5-300x300.png 300w, https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-5-150x150.png 150w\" sizes=\"auto, (max-width: 581px) 85vw, 581px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Code is maintainable<\/strong><\/p>\n\n\n\n<p>The <strong>Maintainability Index<\/strong> is a software metric used to measure how easily a codebase can be maintained over time. It gives a quantifiable score based on various factors like code complexity, size, and readability, helping developers understand how changes in the code might affect its long-term maintainability.<\/p>\n\n\n\n<p>Let&#8217;s discover the maintainability of the Machhina SDK methods using the CppDepend Treemap:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"972\" height=\"581\" src=\"https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-6.png\" alt=\"\" class=\"wp-image-2076\" srcset=\"https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-6.png 972w, https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-6-300x179.png 300w, https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2024\/09\/image-6-768x459.png 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>In the&nbsp;<strong>Metric View<\/strong>, the code base is represented through a&nbsp;<strong>Treemap<\/strong>. Treemapping is a method for displaying tree-structured data by using nested rectangles. The tree structure used in CppDepend treemap is the usual code hierarchy:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>C\/C++ projects contain namespaces,<\/li>\n\n\n\n<li>Namespaces contain types,<\/li>\n\n\n\n<li>Types contains methods and fields.<\/li>\n<\/ul>\n\n\n\n<p>The option\u00a0<strong>Size<\/strong>\u00a0of the treemap determines the size of rectangles which is by default proportional  to the \u00a0<em>number of Lines of Code<\/em>,  and a code metric can be represented by coloring code elements rectangle, in our case the color is related to the  maintainability.<\/p>\n\n\n\n<p>As we can see only few methods are in red, and most of them concerns the test methods.<\/p>\n\n\n\n<p><strong>Conclusion<\/strong><\/p>\n\n\n\n<p>Macchina is known for its ease of use in developing IoT applications. Its implementation is clean, easy to understand, and customizable, making it one of the best choices for IoT applications.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In C++, many libraries can aid in implementing an IoT application, but most are low-level. For a high-level SDK,\u00a0Macchina.io\u00a0is an excellent choice, especially if you seek a robust framework that simplifies IoT application creation. Macchina is not only a perfect solution for IoT applications but it&#8217;s also a well designed and implemented project, so the &hellip; <a href=\"https:\/\/cppdepend.com\/blog\/a-look-inside-the-macchina-sdk-source-code-clean-design-and-implementation\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;A Look Inside the Macchina SDK Source Code: Clean Design and Implementation.&#8221;<\/span><\/a><\/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":[67,18],"class_list":["post-2068","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c-2","tag-cppdepend"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/2068","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=2068"}],"version-history":[{"count":7,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/2068\/revisions"}],"predecessor-version":[{"id":2085,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/2068\/revisions\/2085"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=2068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=2068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=2068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}