
{"id":266,"date":"2014-10-23T15:12:26","date_gmt":"2014-10-23T15:12:26","guid":{"rendered":"http:\/\/www.codergears.com\/Blog\/?p=266"},"modified":"2014-10-28T15:02:30","modified_gmt":"2014-10-28T15:02:30","slug":"some-reasons-why-modern-c-adopted-the-generic-programming","status":"publish","type":"post","link":"https:\/\/codergears.com\/Blog\/?p=266","title":{"rendered":"Some reasons why &#8220;Modern C++&#8221; adopted the generic programming"},"content":{"rendered":"<p>As Bjarne Stroustrup points out, &#8220;C++ is a multi-paradigmed language.&#8221; It supports many different styles of programs, or paradigms, and object-oriented programming is only one of these. Some of the others are structured programming, and generic programming. In the last few years C++ experts\u00a0like Andrei Alexandrescu, Scott Meyers and Herb Sutter promotes the uses of the generic programming and they qualify it as Modern C++ Design.<\/p>\n<p>Here&#8217;s what say Andrei Alexandrescu about the Modern C++ design:<\/p>\n<blockquote><p><span style=\"color: #000000;\">Modern C++ Design defines and systematically uses\u00a0<\/span><i style=\"color: #000000;\">generic components<\/i><span style=\"color: #000000;\">\u00a0&#8211; highly flexible design artifacts that are mixable and matchable to obtain rich behaviors with a small, orthogonal body of code.<\/span><\/p><\/blockquote>\n<p><!--more--><\/p>\n<p>Three\u00a0assetions are interesting in his point of view:<\/p>\n<ul>\n<li><span style=\"color: #000000;\">Modern C++ Design defines and <strong>systematically uses\u00a0<\/strong><\/span><strong>generic components<\/strong>.<\/li>\n<li><span style=\"font-style: italic; color: #000000;\">highly <strong>flexible<\/strong> design.<\/span><\/li>\n<li><span style=\"font-style: italic; color: #000000;\">obtain rich behaviors with a <strong>small, orthogonal<\/strong> body of code.<\/span><\/li>\n<\/ul>\n<p><strong>Generics flexibility.<br \/>\n<\/strong><\/p>\n<p>To have a concrete idea about the generics flexibility, let&#8217;s compare the implementation of a class calculating a tax in OOP and generic programming.<\/p>\n<p><em>OOP implemenation<\/em><\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/generics1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-287\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/generics1.png\" alt=\"generics1\" width=\"475\" height=\"332\" \/><\/a><\/p>\n<p>What the CTaxCalculator class implementation tell us exactly?<\/p>\n<p>I&#8217;m the class CTaxCalculator, I know how to calculate the tax but I collaborate only with classes of ICalculator\u00a0kind. I refuse to colaborate with any other not ICalculator class even if it can help me to calculate the Tax.<\/p>\n<p><em>Generic implementation<\/em><\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/generics2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-288\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/generics2.png\" alt=\"generics2\" width=\"479\" height=\"270\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>The CGenericTaxCalculator class in the other side know how to calculate the tax , and for that it can collaborate with any type capable to calculate the tax, and it&#8217;s \u00a0not aware about its kind.<\/p>\n<p>This\u00a0makes the generic programming more natural and flexible, \u00a0concerning\u00a0 the first implementation it&#8217;s like in the real world, a company searching for a developer\u00a0accept only ones graduated\u00a0from a specific school and reject all the others even if they\u00a0have the skills needed.<\/p>\n<p>But the flexibility comes with\u00a0a price, the code became hard to understand, Indeed in OOP programming I can just go to the definition of ICalculator to know what we wait for this type. it&#8217;s not the case in generic programming, it&#8217;s difficult to know what we expect exactly from the template parameter , which members\u00a0must contains? it must derives from a specific class or not?<\/p>\n<p>It&#8217;s true that \u00a0<a href=\"http:\/\/accu.org\/index.php\/journals\/442\">C++\u00a0Traits<\/a> and <a href=\"http:\/\/en.wikipedia.org\/wiki\/Substitution_failure_is_not_an_error\">SFINAE <\/a>\u00a0mechanisms help to defines \u00a0what we expect from a template param, but it&#8217;s an advanced feature and few C++ developers master them.<\/p>\n<p><span style=\"color: #474747;\">The C++ designers are aware of this issue, for this reason many improvements are added to template programming in the new C++ standards, and new ones will come like the interesting feature\u00a0<\/span><a style=\"color: #0274bd;\" href=\"http:\/\/isocpp.org\/blog\/2013\/02\/concepts-lite-constraining-templates-with-predicates-andrew-sutton-bjarne-s\">&#8220;Concept-lite&#8221;<\/a><span style=\"color: #474747;\">\u00a0 to let define constraints for the template params.\u00a0<\/span><\/p>\n<p><strong>\u00a0Generic programming produce compact code<\/strong><\/p>\n<p style=\"color: #333333;\">Repeating boilerplate code makes maintenance difficult, and allows all kinds of mistakes. With generic programming you can avoid harmful repetition.<\/p>\n<p>Let&#8217;s take this second minimal example of sum between two numbers.<\/p>\n<p>Here&#8217;s an implementation without using templates:<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/generics4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-289\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/generics4.png\" alt=\"generics4\" width=\"477\" height=\"238\" \/><\/a><\/p>\n<p>By using a template function, the code became more compact:<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/generics7.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-290\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/generics7.png\" alt=\"generics7\" width=\"478\" height=\"102\" \/><\/a><\/p>\n<p>It was just a mini sample to show the benefit of using templates to have a compact code, STL and boost contains more advanced algorithm which shows better the power of generic programming to remove the boilerplate code.<\/p>\n<p><strong>But why the generic programming is\u00a0not widely used ?<\/strong><\/p>\n<p>Generic programming sound more natural and flexible, and it can provides more possibilities than OOP, however many developers found it very complex and difficult to learn and use.<\/p>\n<ul>\n<li><span style=\"color: #474747;\">The code became illisible and hard to maintain.<\/span><\/li>\n<li><span style=\"color: #474747;\">The compiler errors are very hard to understand.\u00a0<\/span><\/li>\n<\/ul>\n<p>Which it&#8217;s confirmed by the design choice of many C++ open source projects, indeed if you take a look in some known C++ source projects, the generic programming is mostly used only in C++ libraries like stl, boost, loki and folly, but very few applications uses the generic approach for their design. however the majority of them uses templated libraries.<\/p>\n<p><strong>Conclusion<\/strong><\/p>\n<p>The Modern C++ design has a preference to the generic programming approach, its very powerful, but before adopting it you have to be aware of the price to pay. And even there&#8217;s a big effort to understand better the generic\u00a0programming benefits and to simplify their use, thanks to guys like Andrei Alexandrescu, Scott Meyers and Herb Sutter. But the Modern C++ design is not\u00a0widely adopted yet by the C++ community.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As Bjarne Stroustrup points out, &#8220;C++ is a multi-paradigmed language.&#8221; It supports many different styles of programs, or paradigms, and object-oriented programming is only one of these. Some of the others are structured programming, and generic programming. In the last few years C++ experts\u00a0like Andrei Alexandrescu, Scott Meyers and Herb Sutter promotes the uses of &hellip; <a href=\"https:\/\/codergears.com\/Blog\/?p=266\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Some reasons why &#8220;Modern C++&#8221; adopted the generic programming&#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-266","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/266","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=266"}],"version-history":[{"count":30,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/266\/revisions"}],"predecessor-version":[{"id":300,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/266\/revisions\/300"}],"wp:attachment":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}