{"id":270236,"date":"2021-06-09T17:00:51","date_gmt":"2021-06-09T14:00:51","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/whats-an-impedance-mismatch-in-programming\/"},"modified":"2021-06-09T17:00:51","modified_gmt":"2021-06-09T14:00:51","slug":"whats-an-impedance-mismatch-in-programming","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/whats-an-impedance-mismatch-in-programming\/","title":{"rendered":"#What\u2019s an \u201cImpedance Mismatch\u201d in Programming?"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2de1f4b5cb9\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2de1f4b5cb9\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/whats-an-impedance-mismatch-in-programming\/#Mapping_and_Relationships\" >Mapping and Relationships<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/whats-an-impedance-mismatch-in-programming\/#Considering_Hierarchy\" >Considering Hierarchy<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/whats-an-impedance-mismatch-in-programming\/#Inheritance\" >Inheritance<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/whats-an-impedance-mismatch-in-programming\/#Visibility_and_Encapsulation\" >Visibility and Encapsulation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/whats-an-impedance-mismatch-in-programming\/#Summary\" >Summary<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#What\u2019s an \u201cImpedance Mismatch\u201d in Programming?&#8221;<\/strong><\/p>\n<figure id=\"attachment_11877\" style=\"width: 2030px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage wp-image-11877 size-full\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/06\/e7934b02.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"hands with puzzle pieces\" width=\"2030\" height=\"1138\" data-crediturl=\"https:\/\/www.shutterstock.com\/image-photo\/closeup-hand-woman-connecting-jigsaw-puzzle-683030524\" data-credittext=\"Shutterstock\/oatawa\" \/><figcaption class=\"wp-caption-text\"><span class=\"type:primaryImage imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-photo\/closeup-hand-woman-connecting-jigsaw-puzzle-683030524\">Shutterstock\/oatawa<\/a><\/span><\/figcaption><\/figure>\n<p>A programming impedance mismatch occurs when data needs to be transformed into a different architectural paradigm. The most prominent example involves object-oriented codebases and relational databases.<\/p>\n<p>An impedance mismatch arises when data is fetched from or inserted into a database. The properties of objects or classes within the codebase need to be m<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>ed to their corresponding database table fields.<\/p>\n<h2 id=\"mapping-and-relationships\"><span class=\"ez-toc-section\" id=\"Mapping_and_Relationships\"><\/span>Mapping and Relationships<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Your classes won&#8217;t necessarily map directly to individual database tables. The construction of an object might require data from several tables to be used in aggregate.<\/p>\n<p>You&#8217;ll also need to handle relationships between your data. Relational databases make this straightforward by allowing you to reference other records. You can use a <code>JOIN<\/code> to access all the data encapsulated by the relationship.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"sql\">\n<pre class=\"de1\"><span class=\"kw1\">CREATE<\/span> <span class=\"kw1\">TABLE<\/span> productTypes<span class=\"br0\">&#040;<\/span>\n    ProductTypeUuid <span class=\"kw1\">VARCHAR<\/span><span class=\"br0\">&#040;<\/span><span class=\"nu0\">32<\/span><span class=\"br0\">&#041;<\/span> <span class=\"kw1\">PRIMARY<\/span> <span class=\"kw1\">KEY<\/span><span class=\"sy0\">,<\/span>\n    ProductTypeName <span class=\"kw1\">VARCHAR<\/span><span class=\"br0\">&#040;<\/span><span class=\"nu0\">255<\/span><span class=\"br0\">&#041;<\/span> <span class=\"kw1\">NOT<\/span> <span class=\"kw1\">NULL<\/span> <span class=\"kw1\">UNIQUE<\/span>\n<span class=\"br0\">&#041;<\/span>;\n&nbsp;\n<span class=\"kw1\">CREATE<\/span> <span class=\"kw1\">TABLE<\/span> products<span class=\"br0\">&#040;<\/span>\n    ProductUuid <span class=\"kw1\">VARCHAR<\/span><span class=\"br0\">&#040;<\/span><span class=\"nu0\">32<\/span><span class=\"br0\">&#041;<\/span> <span class=\"kw1\">PRIMARY<\/span> <span class=\"kw1\">KEY<\/span><span class=\"sy0\">,<\/span>\n    ProductName <span class=\"kw1\">VARCHAR<\/span><span class=\"br0\">&#040;<\/span><span class=\"nu0\">255<\/span><span class=\"br0\">&#041;<\/span> <span class=\"kw1\">NOT<\/span> <span class=\"kw1\">NULL<\/span> <span class=\"kw1\">UNIQUE<\/span><span class=\"sy0\">,<\/span>\n    ProductType <span class=\"kw1\">VARCHAR<\/span><span class=\"br0\">&#040;<\/span><span class=\"nu0\">32<\/span><span class=\"br0\">&#041;<\/span> <span class=\"kw1\">NOT<\/span> <span class=\"kw1\">NULL<\/span><span class=\"sy0\">,<\/span>\n    <span class=\"kw1\">FOREIGN<\/span> <span class=\"kw1\">KEY<\/span> <span class=\"br0\">&#040;<\/span>ProductType<span class=\"br0\">&#041;<\/span> <span class=\"kw1\">REFERENCES<\/span> productTypes<span class=\"br0\">&#040;<\/span>ProductTypeUuid<span class=\"br0\">&#041;<\/span> <span class=\"kw1\">ON<\/span> <span class=\"kw1\">DELETE<\/span> CASCADE\n<span class=\"br0\">&#041;<\/span>;<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Using plain SQL, you could get the combined properties of a Product and its ProductType with this simple query:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"sql\">\n<pre class=\"de1\"><span class=\"kw1\">SELECT<\/span> <span class=\"sy0\">*<\/span> <span class=\"kw1\">FROM<\/span> products\n<span class=\"kw1\">INNER<\/span> <span class=\"kw1\">JOIN<\/span> productTypes <span class=\"kw1\">ON<\/span> ProductTypeUuid <span class=\"sy0\">=<\/span> ProductType;<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Then properties of the Product and its ProductType are accessed from the same flat structure:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw1\">echo<\/span> <span class=\"re0\">$record<\/span><span class=\"br0\">&#091;<\/span><span class=\"st0\">&quot;ProductName&quot;<\/span><span class=\"br0\">&#093;<\/span><span class=\"sy0\">;<\/span>\n<span class=\"kw1\">echo<\/span> <span class=\"re0\">$record<\/span><span class=\"br0\">&#091;<\/span><span class=\"st0\">&quot;ProductTypeName&quot;<\/span><span class=\"br0\">&#093;<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>This flat array quickly becomes limiting in complex applications. Developers naturally model the Product and ProductType entities as separate classes. The Product class could then hold an instance of a ProductType. Here&#8217;s how that looks:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\">final <span class=\"kw2\">class<\/span> ProductType <span class=\"br0\">&#123;<\/span>\n&nbsp;\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">&#040;<\/span>\n        <span class=\"kw2\">public<\/span> string <span class=\"re0\">$Uuid<\/span><span class=\"sy0\">,<\/span>\n        <span class=\"kw2\">public<\/span> string <span class=\"re0\">$Name<\/span><span class=\"br0\">&#041;<\/span> <span class=\"br0\">&#123;<\/span><span class=\"br0\">&#125;<\/span>\n&nbsp;\n<span class=\"br0\">&#125;<\/span>\n&nbsp;\nfinal <span class=\"kw2\">class<\/span> Product <span class=\"br0\">&#123;<\/span>\n&nbsp;\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">&#040;<\/span>\n        <span class=\"kw2\">public<\/span> string <span class=\"re0\">$Uuid<\/span><span class=\"sy0\">,<\/span>\n        <span class=\"kw2\">public<\/span> string <span class=\"re0\">$Name<\/span><span class=\"sy0\">,<\/span>\n        <span class=\"kw2\">public<\/span> ProductType <span class=\"re0\">$ProductType<\/span><span class=\"br0\">&#041;<\/span> <span class=\"br0\">&#123;<\/span><span class=\"br0\">&#125;<\/span>\n&nbsp;\n<span class=\"br0\">&#125;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>There&#8217;s now a significant impedance mismatch in the code. Some form of specialised mapping is required before records from the database query can be represented as <code>Product<\/code> instances.<\/p>\n<p>Further complications arise when you want to access all the products of a particular type. Here&#8217;s how you might do that in code:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\">final <span class=\"kw2\">class<\/span> ProductType <span class=\"br0\">&#123;<\/span>\n&nbsp;\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">&#040;<\/span>\n        <span class=\"kw2\">public<\/span> string <span class=\"re0\">$Uuid<\/span><span class=\"sy0\">,<\/span>\n        <span class=\"kw2\">public<\/span> string <span class=\"re0\">$Name<\/span><span class=\"sy0\">,<\/span>\n        ProductCollection <span class=\"re0\">$Products<\/span><span class=\"br0\">&#041;<\/span> <span class=\"br0\">&#123;<\/span><span class=\"br0\">&#125;<\/span>\n&nbsp;\n<span class=\"br0\">&#125;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Now the <code>ProductType<\/code> holds a <code>ProductCollection<\/code>, which would ultimately contain an array of <code>Product<\/code> instances. This creates a bi-directional relational reference &#8211; <code>ProductType<\/code> contains all its products and each <code>Product<\/code> contains its product type.<\/p>\n<p>This form of modelling doesn&#8217;t exist within the relational paradigm. Each form of connection is represented with a single relational link record. The use of bi-directional references simplifies developer access to object properties. However, it requires more complex mapping when transferred to and from the database. This is because SQL doesn&#8217;t natively understand the semantics of the model.<\/p>\n<h2 id=\"considering-hierarchy\"><span class=\"ez-toc-section\" id=\"Considering_Hierarchy\"><\/span>Considering Hierarchy<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The model explained above creates a hierarchy in the codebase: <code>Product<\/code> sits below <code>ProductType<\/code>. This sounds logical and matches our expectations of the real world.<\/p>\n<p>Relational databases don&#8217;t respect hierarchies. As all relationships are equivalent, relational databases have an inherently &#8220;flat&#8221; structure. We saw this earlier when fetching data with a <code>JOIN<\/code>.<\/p>\n<p>The lack of hierarchy in SQL means all tables possess an equivalent priority to each other. An effect of this is that you can easily access the properties of records nested deep in your logical object hierarcy. Furthermore, there&#8217;s a lower risk of cyclical dependencies.<\/p>\n<p>The example above shows that <code>Product<\/code> and <code>ProductType<\/code> can end up referring to each other in code; the flat nature of relational databases would prevent this specific example occurring. Cycles can still crop up in plain SQL but you&#8217;re less likely to encounter them than when modelling with object-oriented code.<\/p>\n<p>Object-oriented programming relies on the composition of simple objects into more complex ones. Relational models have no such notion of composition or the &#8220;simple&#8221; and &#8220;complex&#8221; &#8211; any record can reference any other.<\/p>\n<h2 id=\"inheritance\"><span class=\"ez-toc-section\" id=\"Inheritance\"><\/span>Inheritance<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Another OOP-exclusive function is inheritance. It is common for a class to extend another, adding additional behaviours. Relational databases are unable to replicate this. It&#8217;s impossible for a table to &#8220;extend&#8221; another table.<\/p>\n<p>A codebase that uses inheritance will encounter difficulties when child objects are persisted or hydrated via a relational database. Within the database, you&#8217;ll usually need two tables. One stores the properties of the base object (that you&#8217;re extending), with another handling the properties of the child.<\/p>\n<p>The mapping code then iterates all the properties of the object. Properties deriving from the extended class will be inserted into the first code. Those directly defined on the child will end up in the second table.<\/p>\n<p>A similar system is required when mapping back to the codebase from the database. A SQL <code>JOIN<\/code> could be used to get all records corresponding to the base (extended) class, with the child properties included. Those records that contained the child properties would then be mapped to child class instances.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"sql\">\n<pre class=\"de1\"><span class=\"kw1\">CREATE<\/span> <span class=\"kw1\">TABLE<\/span> parent<span class=\"br0\">&#040;<\/span>Id <span class=\"kw1\">INTEGER<\/span> <span class=\"kw1\">PRIMARY<\/span> <span class=\"kw1\">KEY<\/span><span class=\"sy0\">,<\/span> A <span class=\"kw1\">INTEGER<\/span><span class=\"br0\">&#041;<\/span>;\n<span class=\"kw1\">CREATE<\/span> <span class=\"kw1\">TABLE<\/span> child<span class=\"br0\">&#040;<\/span>Id <span class=\"kw1\">INTEGER<\/span> <span class=\"kw1\">PRIMARY<\/span> <span class=\"kw1\">KEY<\/span><span class=\"sy0\">,<\/span> B <span class=\"kw1\">INTEGER<\/span><span class=\"sy0\">,<\/span> ParentId <span class=\"kw1\">INTEGER<\/span><span class=\"br0\">&#041;<\/span>;<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">class<\/span> Parent <span class=\"br0\">&#123;<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">&#040;<\/span>int <span class=\"re0\">$A<\/span><span class=\"br0\">&#041;<\/span> <span class=\"br0\">&#123;<\/span><span class=\"br0\">&#125;<\/span>\n<span class=\"br0\">&#125;<\/span>\n&nbsp;\nfinal <span class=\"kw2\">class<\/span> Child <span class=\"kw2\">extends<\/span> Parent <span class=\"br0\">&#123;<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">&#040;<\/span>int <span class=\"re0\">$A<\/span><span class=\"sy0\">,<\/span> int <span class=\"re0\">$B<\/span><span class=\"br0\">&#041;<\/span> <span class=\"br0\">&#123;<\/span><span class=\"br0\">&#125;<\/span>\n<span class=\"br0\">&#125;<\/span>\n&nbsp;\n<span class=\"co1\">\/\/ Get records<\/span>\n<span class=\"co1\">\/\/ SELECT * FROM parent INNER JOIN child ON child.ParentId = parent.Id;<\/span>\n<span class=\"re0\">$objs<\/span> <span class=\"sy0\">=<\/span> <span class=\"br0\">&#091;<\/span><span class=\"br0\">&#093;<\/span><span class=\"sy0\">;<\/span>\n<span class=\"kw1\">foreach<\/span> <span class=\"br0\">&#040;<\/span><span class=\"re0\">$records<\/span> <span class=\"kw1\">as<\/span> <span class=\"re0\">$record<\/span><span class=\"br0\">&#041;<\/span> <span class=\"br0\">&#123;<\/span>\n    <span class=\"kw1\">if<\/span> <span class=\"br0\">&#040;<\/span><span class=\"kw3\">isset<\/span><span class=\"br0\">&#040;<\/span><span class=\"re0\">$record<\/span><span class=\"br0\">&#091;<\/span><span class=\"st0\">&quot;B&quot;<\/span><span class=\"br0\">&#093;<\/span><span class=\"br0\">&#041;<\/span><span class=\"br0\">&#041;<\/span> <span class=\"br0\">&#123;<\/span>\n        <span class=\"re0\">$objs<\/span><span class=\"br0\">&#091;<\/span><span class=\"br0\">&#093;<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw2\">new<\/span> Child<span class=\"br0\">&#040;<\/span><span class=\"re0\">$record<\/span><span class=\"br0\">&#091;<\/span><span class=\"st0\">&quot;A&quot;<\/span><span class=\"br0\">&#093;<\/span><span class=\"sy0\">,<\/span> <span class=\"re0\">$record<\/span><span class=\"br0\">&#091;<\/span><span class=\"st0\">&quot;B&quot;<\/span><span class=\"br0\">&#093;<\/span><span class=\"br0\">&#041;<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">&#125;<\/span>\n    <span class=\"kw1\">else<\/span> <span class=\"re0\">$objs<\/span><span class=\"br0\">&#091;<\/span><span class=\"br0\">&#093;<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw2\">new<\/span> Parent<span class=\"br0\">&#040;<\/span><span class=\"re0\">$record<\/span><span class=\"br0\">&#091;<\/span><span class=\"st0\">&quot;A&quot;<\/span><span class=\"br0\">&#093;<\/span><span class=\"br0\">&#041;<\/span><span class=\"sy0\">;<\/span>\n<span class=\"br0\">&#125;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The introduction of inheritance necessitates the use of more involved mapping logic. The inability of relational databases to model the inheritance capabilities of object-oriented languages introduces this impedance mismatch.<\/p>\n<h2 id=\"visibility-and-encapsulation\"><span class=\"ez-toc-section\" id=\"Visibility_and_Encapsulation\"><\/span>Visibility and Encapsulation<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A fundamental tenet of object-oriented programming is visibility and encapsulation. Properties are declared as <code>public<\/code> or <code>private<\/code>. An object&#8217;s internal representation can be concealed behind an interface that pre-formats data for the consumer.<\/p>\n<p>Relational databases lack these controls. There&#8217;s no way to declare a field as <code>private<\/code>, nor is there an obvious need to do so. Each piece of data stored in a database has its own purpose; there should be no redundancy.<\/p>\n<p>This isn&#8217;t necessarily true when transposed to an object-oriented paradigm. An object might use two database fields in aggregate to expose a new piece of computed information. The values of the two individual fields may be irrelevant to the application and consequently hidden from view.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"sql\">\n<pre class=\"de1\"><span class=\"kw1\">CREATE<\/span> <span class=\"kw1\">TABLE<\/span> products<span class=\"br0\">&#040;<\/span>Price <span class=\"kw1\">INTEGER<\/span><span class=\"sy0\">,<\/span> TaxRate <span class=\"kw1\">INTEGER<\/span><span class=\"br0\">&#041;<\/span>;<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\">final <span class=\"kw2\">class<\/span> Product <span class=\"br0\">&#123;<\/span>\n&nbsp;\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">&#040;<\/span>\n        <span class=\"kw2\">protected<\/span> int <span class=\"re0\">$Price<\/span><span class=\"sy0\">,<\/span>\n        <span class=\"kw2\">protected<\/span> int <span class=\"re0\">$TaxRate<\/span><span class=\"br0\">&#041;<\/span> <span class=\"br0\">&#123;<\/span><span class=\"br0\">&#125;<\/span>\n&nbsp;\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> getTotalPrice<span class=\"br0\">&#040;<\/span><span class=\"br0\">&#041;<\/span> <span class=\"sy0\">:<\/span> int <span class=\"br0\">&#123;<\/span>\n        <span class=\"kw1\">return<\/span> <span class=\"br0\">&#040;<\/span><span class=\"re0\">$this<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">Price<\/span> <span class=\"sy0\">*<\/span> <span class=\"br0\">&#040;<\/span><span class=\"re0\">$this<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">TaxRate<\/span> <span class=\"sy0\">\/<\/span> <span class=\"nu0\">100<\/span><span class=\"br0\">&#041;<\/span><span class=\"br0\">&#041;<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">&#125;<\/span>\n<span class=\"br0\">&#125;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The application only cares about the total product price, including taxes. The unit price and tax rate are <em>encapsulated<\/em> by the <code>Product<\/code> class. Visibility controls (<code>protected<\/code>) conceal the values. The public interface consists of a single method that uses the two fields to compute a new value.<\/p>\n<p>More <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/general\/\" data-internallinksmanager029f6b8e52c=\"3\" title=\"General\" target=\"_blank\" rel=\"noopener\">general<\/a>ly, object-oriented programming advocates programming to interfaces. Direct access to properties is discouraged. Using class methods that implement an interface enables alternative implementations to be constructed in the future.<\/p>\n<p>There is no direct counterpart to this in the relational world. Database views do provide a way to combine tables and abstract fields into new forms. You&#8217;re still working directly with field values though.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Object-relational impedance mismatches occur when an object-oriented codebase exchanges data with a relational database. There are fundamental differences in the way in which data is modelled. This neccessitates the introduction of a mapping layer that transposes data between the formats.<\/p>\n<p>The impedance mismatch problem is one of the key motivating factors in the adoption of ORMs within object-oriented languages. These enable the automatic hydration of complex codebase objects from relational data sources. Without a dedicated data mapping layer, only the simplest of applications will have a straightforward pathway to and from a relational database.<\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/10526\/whats-an-impedance-mismatch-in-programming\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#What\u2019s an \u201cImpedance Mismatch\u201d in Programming?&#8221; Shutterstock\/oatawa A programming impedance mismatch occurs when data needs to be transformed into a different architectural paradigm. The most prominent example involves object-oriented codebases and relational databases. An impedance mismatch arises when data is fetched from or inserted into a database. The properties of objects or classes within the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":270237,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/06\/e7934b02.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-270236","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/270236","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=270236"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/270236\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/270237"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=270236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=270236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=270236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}