{"id":418,"date":"2018-02-21T10:36:44","date_gmt":"2018-02-21T10:36:44","guid":{"rendered":"http:\/\/cppdepend.com\/blog\/?p=418"},"modified":"2018-11-07T16:39:49","modified_gmt":"2018-11-07T16:39:49","slug":"try-to-understand-the-linus-trovalds-c-opinion","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/try-to-understand-the-linus-trovalds-c-opinion\/","title":{"rendered":"Try to understand the Linus Torvalds C++ opinion."},"content":{"rendered":"<p>Linux, Php, and Git are a popular projects developed with C, in the other side OpenOffice, firefox, Clang, Photoshop are developed with C++, so it\u2019s proven that each one is a good candidate to develop complex applications. Try to prove that a language is better than the other is maybe not the good debate. However we can discuss the motivations behind choosing one of them.<\/p>\n<p>When I discovered the <a href=\"http:\/\/harmful.cat-v.org\/software\/c++\/linus\">opinion<\/a> of Linus Trovalds about C++, and as a C++ developer I totally disagree with his point of view. But it&#8217;s a point of view from the lead developer of the Linux kernel and git and he can&#8217;t be totally wrong.<!--more--><\/p>\n<p>When reading again its opinion I agree with this assertion<\/p>\n<pre>inefficient abstracted programming models where two years down the road you notice that some abstraction wasn't very efficient, but now all your code depends on all the nice object models around it, and you cannot fix it without rewriting your app.<\/pre>\n<p>It&#8217;s true that C++ provides a better possibilities to have a beautiful and well structured code, but it comes with a price, any changes or refactoring could be difficult. However it does not mean that I have to choose another language. Indeed each language or library comes with a price but we\u00a0have to know how\u00a0we can limit the impact of some possible changes of our C++ code after few years of development.<\/p>\n<p>Let&#8217;s\u00a0 analyze with\u00a0<a href=\"http:\/\/www.cppdepend.com\/\">CppDepend<\/a>\u00a0the\u00a0<a href=\"http:\/\/git-scm.com\/\">Git<\/a>\u00a0source code and discover some design facts and \u00a0compare\u00a0between C and C++ concerning these two points:<\/p>\n<ul>\n<li>Easy to understand.<\/li>\n<li>Managing changes.<\/li>\n<\/ul>\n<h2>Modularity: Physical vs Logical<\/h2>\n<p>Modularity is a software design technique that increases the extent to which software is composed from separate parts, you can manage and maintain modular code easily.<\/p>\n<p>We can modularize a project\u00a0using\u00a0 two approaches:<\/p>\n<ul>\n<li>Physically: by using directories and files, this modularity is provided by the operating system and can be applied to any language.<\/li>\n<li>Logically: by using namespaces, components, classes and structs, this technique depends on the language capabilities.<\/li>\n<\/ul>\n<p>When we develop with C, and to package our code we use essentially physical modularity, the code is structured by using directories to isolate the modules, here\u2019s for Git the dependency graph between some of its directories.<\/p>\n<p><a href=\"http:\/\/cppdepend.files.wordpress.com\/2012\/10\/git1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-837\" title=\"git1\" src=\"http:\/\/cppdepend.files.wordpress.com\/2012\/10\/git1.png\" alt=\"\" width=\"595\" height=\"304\" \/><\/a><\/p>\n<p>However for C++ instead of C we can use namespaces to modularize the codebase, these artifacts are provided by the language, and for the previous graph\u00a0the shapes could be\u00a0\u00a0namespaces to modularize our code instead of directories.<\/p>\n<p><strong>Impact of choosing one of the two approaches:<\/strong><\/p>\n<p><strong><em>Easy to understand<\/em><\/strong>\u00a0: The logical approach is better because the modularity is well defined by the language artifacts, and just reading the code we can know in which module a code element exist.<\/p>\n<p><strong><em>Managing changes<\/em><\/strong>:\u00a0a good design need in general many iterations, and for the physical approach the impact of design changes can be very limited than the logical one, indeed we need only to move function or variable from a file to another, or move file from directory to another.<\/p>\n<p>However for C++ it can impact a lot of code because the logical modularity is implemented by the language artifacts and a code modification is needed.<\/p>\n<h2>Encapsulation:Class vs File<\/h2>\n<p>for C++ the encapsulation is defined as the process of combining data and functions into a single unit called class. Using the method of encapsulation, the programmer cannot directly access the data. Data is only accessible through the functions present inside the class.<\/p>\n<p>For C we can have an encapsulation, but using also a physical approach\u00a0 and a class can be a file containing the functions and the data used by them, and we can limit the accessibility of functions and variables by using \u201cstatic\u201d keyword.<\/p>\n<p>Git use this technique to hide functions and variables, to discover that let\u2019s search for static function:<\/p>\n<pre>from m in Methods where m.IsStatic select m\r\n<\/pre>\n<p>The treemap is very useful to have a good idea of code elements concerned by a CQLinq query, the blue rectangles represent the result.<\/p>\n<p><a href=\"http:\/\/cppdepend.files.wordpress.com\/2012\/10\/git2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-842\" title=\"git2\" src=\"http:\/\/cppdepend.files.wordpress.com\/2012\/10\/git2.png\" alt=\"\" width=\"595\" height=\"337\" \/><\/a><br \/>\nAlmost all functions are declared as static to be visible only in the translation unit where there are declared, the same remark could be applicable for variables.<\/p>\n<pre>from f in Fields where f.IsStatic select f\r\n<\/pre>\n<p><a href=\"http:\/\/cppdepend.files.wordpress.com\/2012\/10\/git3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-843\" title=\"git3\" src=\"http:\/\/cppdepend.files.wordpress.com\/2012\/10\/git3.png\" alt=\"\" width=\"298\" height=\"320\" \/><\/a><br \/>\n<em><strong>Easy to Understand<\/strong><\/em>: Using C++ encapsulation mechanism improve the understanding and visibility of code, C is low level and use physical approach rather than logical.<\/p>\n<p><em><strong>Managing changes<\/strong><\/em>:If we have to change the place where variable or function are encapsulated, it can very easy for C, but for C++ it can impact a lot of code.<\/p>\n<h2>Polymorphism vs Selection idiom<\/h2>\n<p>Polymorphism means that some code or operations or objects behave differently in different contexts.<\/p>\n<p>This technique is very used in C++ projects, but what about C?<\/p>\n<p>For procedural languages the selection technique is adopted by using the keywords \u201cswitch\u201d, \u201cif\u201d or maybe \u201cgoto\u201d, but this technique tend to increase the cyclomatic complexity of the code.<\/p>\n<p>Let\u2019s search for complex function inside Git code source.<\/p>\n<p><a href=\"http:\/\/cppdepend.files.wordpress.com\/2012\/10\/git4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-845\" title=\"git4\" src=\"http:\/\/cppdepend.files.wordpress.com\/2012\/10\/git4.png\" alt=\"\" width=\"347\" height=\"383\" \/><\/a><br \/>\nEven Git is well developed, but many functions could be considered complex, it\u2019s due to overusing of control flow instructions like \u201cif\u201d, \u201cswitch\u201d or \u201cgoto\u201d, with C++ however we can use polymorphism and to minimize the complexity of the code.<\/p>\n<p>Easy to understand: Using Polymorphism permits the isolation of a specific behavior to a class, it improves the visibility and the cohesion of the code.<\/p>\n<p>Managing changes: Adding another behavior with polymorphism can implies the adding of another class, however with selection idiom, you can add only another case under the switch statement.<\/p>\n<p>[adrotate banner=&#8221;3&#8243;]<\/p>\n<h2>Inheritance vs Composition<\/h2>\n<p>Git uses essentially structs to define data manipulated by functions. Let\u2019s search for all structs used:<\/p>\n<pre>from t in Types where t.IsStructure select t\r\n<\/pre>\n<p><a href=\"http:\/\/cppdepend.files.wordpress.com\/2012\/10\/git5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-846\" title=\"git5\" src=\"http:\/\/cppdepend.files.wordpress.com\/2012\/10\/git5.png\" alt=\"\" width=\"349\" height=\"409\" \/><\/a><br \/>\nWhat\u2019s interesting is that almost all data are isolated inside structs, and to verify that we can search for all not const public variables that are primitives and not inside a struct:<\/p>\n<pre>from f in Fields where f.IsPublic &amp;&amp; f.IsPrimitiveType\r\n&amp;&amp; !f.IsStatic &amp;&amp; !f.IsConst\r\nselect f\r\n<\/pre>\n<p><a href=\"http:\/\/cppdepend.files.wordpress.com\/2012\/10\/git7.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-858\" title=\"git7\" src=\"http:\/\/cppdepend.files.wordpress.com\/2012\/10\/git7.png\" alt=\"\" width=\"346\" height=\"336\" \/><\/a><\/p>\n<p>Only some variables are concerned what\u2019s a good point for Git design.<\/p>\n<p>So what about extending a struct, with C we can use the composition like the case of \u201cremote\u201d struct, where many structs reference it.<\/p>\n<p><a href=\"http:\/\/cppdepend.files.wordpress.com\/2012\/10\/git6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-849\" title=\"git6\" src=\"http:\/\/cppdepend.files.wordpress.com\/2012\/10\/git6.png\" alt=\"\" width=\"595\" height=\"320\" \/><\/a><\/p>\n<p>However for C++ we can use also inheritance to extend structs, for example known_remote struct could inherit from remote one.<\/p>\n<p><em><strong>Easy to understand<\/strong><\/em>:\u00a0using inheritance can improve the understanding of data, but we have to be careful when using it, its used only for the \u201cIs\u201d relation.<\/p>\n<p><em><strong>Managing changes<\/strong><\/em>:\u00a0Inheritance implies a high coupling so any changes can impact a lot of code.<\/p>\n<h2>Conclusion:<\/h2>\n<p>C++ provides a better possibilities to have a beautiful and well structured code, but it comes with a price, any changes or refactoring could be difficult.<\/p>\n<p>But doing refactoring need to understand the existing code before making changes, C programs are more difficult to understand, but easy to change, however C++ project can be more structured than C one, but need some effort when making changes.<\/p>\n<p>How we can limit the impact of changes for C++?<\/p>\n<p>The good solution to limit the impact of changes when choosing an OOP approach\u00a0 is to use patterns, specially low coupling and high cohesion concepts to isolate changes only in a specific place.<\/p>\n<p>However the best approach is to adopt the generic programming and the modern c++ practices. the generic programming approach is more flexible than than the OOP one and it help better to limit the impact of the C++ code changes.<\/p>\n<p>[adrotate banner=&#8221;3&#8243;]<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux, Php, and Git are a popular projects developed with C, in the other side OpenOffice, firefox, Clang, Photoshop are developed with C++, so it\u2019s proven that each one is a good candidate to develop complex applications. Try to prove that a language is better than the other is maybe not the good debate. However &hellip; <a href=\"https:\/\/cppdepend.com\/blog\/try-to-understand-the-linus-trovalds-c-opinion\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Try to understand the Linus Torvalds C++ opinion.&#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":[13,44],"class_list":["post-418","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-cpp","tag-linus-torvads"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/418","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=418"}],"version-history":[{"count":13,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/418\/revisions"}],"predecessor-version":[{"id":1199,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/418\/revisions\/1199"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}