{"id":729,"date":"2018-03-16T23:26:41","date_gmt":"2018-03-16T23:26:41","guid":{"rendered":"http:\/\/cppdepend.com\/blog\/?p=729"},"modified":"2019-03-13T22:52:31","modified_gmt":"2019-03-13T22:52:31","slug":"oop-vs-generics-is-vs-has-approach","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/oop-vs-generics-is-vs-has-approach\/","title":{"rendered":"OOP vs Generics : &#8220;Is&#8221; vs &#8220;Has&#8221; approach."},"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.<\/p>\n<p>And as Thomas Becker\u00a0explains in this interesting\u00a0article, there&#8217;s a tension between the generic programming and OOP. And here&#8217;s from the article the opinion of Alexander Stepanov and elder statesman about OOP:\u00a0<!--more--><\/p>\n<pre>Let us start with a little trivia quiz. Who said the following things about object-oriented programming?\r\n\r\n\"I find OOP technically unsound.\"\r\n\r\n\"I find OOP philosophically unsound.\"\r\n\r\n\"I find OOP methodologically wrong.\"\r\n\r\n\"I have yet to see an interesting piece of code that comes from these OO people.\"\r\n\r\n\"I think that object orientedness is almost as much of a hoax as artificial intelligence.\"\r\n\r\nAll the quotes above are from an interview with Alexander Stepanov, the inventor of the STL and elder statesman of generic programming.\r\n\r\nTo have a concrete idea about the generics flexibility, let\u2019s compare the implementation of a class calculating a tax in OOP and generic programming.<\/pre>\n<p>Let&#8217;s discover a major difference\u00a0between the two methodologies which could explain the opinion of Alexander Stepanov about the OOP approach.\u00a0 For that let&#8217;s take as example\u00a0this basic tax calculator<\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/generics1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-287\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/generics1.png\" alt=\"generics1\" width=\"475\" height=\"332\" \/><\/a><\/p>\n<p>The\u00a0CTaxCalculator collaborate only with classes of ICalculator\u00a0kind. Indeed, we have to\u00a0inherit from ICalculator and override some virtual methods to implement our algorithm.<\/p>\n<p>On the other side here&#8217;s an example of the\u00a0 generic TaxCalculator<\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/generics2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-288\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/generics2.png\" alt=\"generics2\" width=\"479\" height=\"270\" \/><\/a><\/p>\n<p>The CGenericTaxCalculator class does not collaborate only with a specific\u00a0class\u00a0kind but with any type capable to calculate the tax,\u00a0 it \u2019s not\u00a0aware\u00a0of\u00a0its kind.<\/p>\n<p>The OOP approach is more an &#8220;<strong>Is oriented approach<\/strong>&#8220;, the inheritance is overused and in almost all the OOP code each class A collaborate with another class B if B <strong>is<\/strong>\u00a0a kind of another class, abstract or not.<\/p>\n<p>On the other side, for the generic approach a class A collaborate with B if B <strong>has\u00a0<\/strong>some specific methods and fields, no need to inherit from a specific class.<\/p>\n<p>This\u00a0makes the generic programming more natural and flexible, it gives to the developer the possibility to adopt a &#8220;<strong>has oriented approach<\/strong>&#8220;, the OOP is more rigid due to the high coupling generated by the inheritance which forces the developer to follow an &#8220;<strong>Is oriented approach<\/strong>&#8220;.<\/p>\n<p>Think about\u00a0it the &#8220;<strong>has oriented\u00a0approach<\/strong> &#8221; is the more natural one. Indeed in the real world, I collaborate with a person who <strong>has<\/strong>\u00a0specific skills, no matter if it <strong>is<\/strong> from a specific family.<\/p>\n<p>The fact that the generic programming is more flexible than OOP makes it the preferred\u00a0choice for the modern C++ design as pointed by\u00a0\u00a0Andrei Alexandrescu:<\/p>\n<pre>Modern C++ Design defines and systematically uses\u00a0<i>generic components<\/i>\u00a0- highly flexible design artifacts that are mixable and matchable to obtain rich behaviors with a small, orthogonal body of code.<\/pre>\n<p>Three\u00a0assertions are interesting in his point of view:<\/p>\n<ul>\n<li>Modern C++ Design defines and\u00a0<strong>systematically uses\u00a0<\/strong><strong>generic components<\/strong>.<\/li>\n<li>highly\u00a0<strong>flexible<\/strong>\u00a0design.<\/li>\n<li>obtain rich behaviors with a\u00a0<strong>small, orthogonal<\/strong>\u00a0body of code.<\/li>\n<\/ul>\n<p>To resume the generic programming is more naturable and flexible than the OOP approach and it gives more possibilities to write efficient code.<\/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. And as Thomas Becker\u00a0explains in this interesting\u00a0article, there&#8217;s a tension between the generic programming and OOP. And here&#8217;s &hellip; <a href=\"https:\/\/cppdepend.com\/blog\/oop-vs-generics-is-vs-has-approach\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;OOP vs Generics : &#8220;Is&#8221; vs &#8220;Has&#8221; approach.&#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":[],"class_list":["post-729","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/729","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=729"}],"version-history":[{"count":10,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/729\/revisions"}],"predecessor-version":[{"id":1245,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/729\/revisions\/1245"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}