{"id":419328,"date":"2022-03-21T14:00:22","date_gmt":"2022-03-21T11:00:22","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/what-is-composable-code-and-how-can-you-create-it-cloudsavvy-it\/"},"modified":"2022-03-21T14:00:22","modified_gmt":"2022-03-21T11:00:22","slug":"what-is-composable-code-and-how-can-you-create-it-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/what-is-composable-code-and-how-can-you-create-it-cloudsavvy-it\/","title":{"rendered":"#What Is Composable Code and How Can You Create It? \u2013 CloudSavvy IT"},"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-6a2f085999946\" 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-6a2f085999946\" 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-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-composable-code-and-how-can-you-create-it-cloudsavvy-it\/#%E2%80%9CWhat_Is_Composable_Code_and_How_Can_You_Create_It_%E2%80%93_CloudSavvy_IT%E2%80%9D\" >&#8220;What Is Composable Code and How Can You Create It? \u2013 CloudSavvy IT&#8221;<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-composable-code-and-how-can-you-create-it-cloudsavvy-it\/#Composability_and_Inversion_of_Control\" >Composability and Inversion of Control<\/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\/what-is-composable-code-and-how-can-you-create-it-cloudsavvy-it\/#Examples_of_Composability\" >Examples of Composability<\/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\/what-is-composable-code-and-how-can-you-create-it-cloudsavvy-it\/#Composition_vs_Inheritance\" >Composition vs Inheritance<\/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\/what-is-composable-code-and-how-can-you-create-it-cloudsavvy-it\/#Summary\" >Summary<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CWhat_Is_Composable_Code_and_How_Can_You_Create_It_%E2%80%93_CloudSavvy_IT%E2%80%9D\"><\/span>&#8220;What Is Composable Code and How Can You Create It? \u2013 CloudSavvy IT&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<div id=\"article-content-area\">\n<figure style=\"width: 1200px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage size-full wp-image-15781\" data-pagespeed-lazy-srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2022\/02\/54dc54e6.jpg?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2022\/02\/54dc54e6.jpg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2022\/02\/54dc54e6.jpg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Photo of colourful woodblocks\" width=\"1200\" height=\"675\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><figcaption class=\"wp-caption-text\"><span class=\"type:primaryImage imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-photo\/colorful-wooden-building-blocks-selective-focus-306641441\">Shutterstock.com\/locrifa<\/a><\/span><\/figcaption><\/figure>\n<p>Composable code describes classes and functions that can be readily combined to create more powerful higher-level constructs. Composability compares favorably to alternative forms of code reuse such as object-oriented inheritance. It advocates the creation of small self-contained units that are treated as building blocks for bigger systems.<\/p>\n<h2 id=\"composability-and-inversion-of-control\"><span class=\"ez-toc-section\" id=\"Composability_and_Inversion_of_Control\"><\/span>Composability and Inversion of Control<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Composable code is often an aim and effect of Inversion of Control (IoC) strategies. Techniques such as dependency injection work with self-contained components that are passed (\u201cinjected\u201d) into the places where they\u2019re needed. This is an example of IoC \u2013 the outer environment is responsible for resolving the dependencies of the deeper code layers it calls.<\/p>\n<p>The concept of composability encapsulates the specific pieces you can provide and how they\u2019re integrated together. A composable system will consist of distinct units of functionality that each have a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Single-responsibility_principle\">single responsibility<\/a>. More complex processes are developed by \u201ccomposing\u201d several of these units into a new larger one.<\/p>\n<h2 id=\"examples-of-composability\"><span class=\"ez-toc-section\" id=\"Examples_of_Composability\"><\/span>Examples of Composability<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Here\u2019s an example of three possible functional units:<\/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=\"kw2\">interface<\/span> LogMessage <span class=\"br0\">{<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> getMessage<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> string<span class=\"sy0\">;<\/span>\n<span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"kw2\">interface<\/span> Mailable <span class=\"br0\">{<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> getEmailContent<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> string<span class=\"sy0\">;<\/span>\n<span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"kw2\">interface<\/span> RelatesToUser <span class=\"br0\">{<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> getTargetUserId<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> int<span class=\"sy0\">;<\/span>\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Now let\u2019s add a logger implementation into the mix:<\/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=\"kw2\">interface<\/span> Logger <span class=\"br0\">{<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> <span class=\"kw3\">log<\/span><span class=\"br0\">(<\/span>LogMessage <span class=\"re0\">$message<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> void<span class=\"sy0\">;<\/span>\n<span class=\"br0\">}<\/span>\n\u00a0\nfinal <span class=\"kw2\">class<\/span> SystemErrorLogMessage <span class=\"kw2\">implements<\/span> LogMessage<span class=\"sy0\">,<\/span> Mailable <span class=\"br0\">{<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">(<\/span>\n        <span class=\"kw2\">protected<\/span> readonly \\Exception <span class=\"re0\">$e<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span><span class=\"br0\">}<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> getMessage<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> string <span class=\"br0\">{<\/span>\n        <span class=\"kw1\">return<\/span> <span class=\"st0\">\"Unhandled error: \"<\/span> <span class=\"sy0\">.<\/span> <span class=\"re0\">$this<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">e<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">getMessage<\/span><span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">}<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> getEmailContent<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> string <span class=\"br0\">{<\/span>\n        <span class=\"kw1\">return<\/span> <span class=\"re0\">$this<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">getMessage<\/span><span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Let\u2019s now consider another type of log message:<\/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> UserLoggedInLogMessage <span class=\"kw2\">implements<\/span> LogMessage<span class=\"sy0\">,<\/span> Mailable<span class=\"sy0\">,<\/span> RelatesToUser <span class=\"br0\">{<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">(<\/span>\n        <span class=\"kw2\">protected<\/span> readonly int <span class=\"re0\">$UserId<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span><span class=\"br0\">}<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> getMessage<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> string <span class=\"br0\">{<\/span>\n        <span class=\"kw1\">return<\/span> <span class=\"st0\">\"User {<span class=\"es4\">$this<\/span> -&gt; UserId} logged in!\"<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">}<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> getEmailContent<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> string <span class=\"br0\">{<\/span>\n        <span class=\"kw1\">return<\/span> <span class=\"re0\">$this<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">getMessage<\/span><span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">}<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> getTargetUserId<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> int <span class=\"br0\">{<\/span>\n        <span class=\"kw1\">return<\/span> <span class=\"re0\">$this<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">UserId<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Here we\u2019re seeing the benefits of composability. By defining the <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>lication\u2019s functionality as interfaces, concrete class implementations are free to mix and match the pieces they need. Not every log message has an associated user; some messages might be ineligible for email alerts if they\u2019re low-priority or contain sensitive information. Keeping the interfaces self-contained lets you create flexible implementations for each situation.<\/p>\n<p>The examples above are written in PHP but could be replicated in any object-oriented language. Composability\u2019s not limited to OOP code though: it\u2019s also a foundational aspect of functional programming. Here complex behaviors are obtained by chaining small functions together. Functions may take <em>other<\/em> functions as arguments and return a <em>new<\/em> higher-order function as a result.<\/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=\"javascript\">\n<pre class=\"de1\"><span class=\"kw1\">const<\/span> square <span class=\"sy0\">=<\/span> x <span class=\"sy0\">=&gt;<\/span> <span class=\"br0\">(<\/span>x <span class=\"sy0\">*<\/span> x<span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"kw1\">const<\/span> quadruple <span class=\"sy0\">=<\/span> x <span class=\"sy0\">=&gt;<\/span> <span class=\"br0\">(<\/span>x <span class=\"sy0\">*<\/span> <span class=\"nu0\">4<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"co1\">\/\/ 16<\/span>\nconsole.<span class=\"me1\">log<\/span><span class=\"br0\">(<\/span>compose<span class=\"br0\">(<\/span>square<span class=\"sy0\">,<\/span> quadruple<span class=\"br0\">)<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">2<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>This minimal JavaScript example uses the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.npmjs.com\/package\/compose-function\"><code>compose-function<\/code><\/a> library to compose the <code>square<\/code> and <code>quadruple<\/code> units into another function that squares and then quadruples its input. The <code>compose()<\/code> utility function accepts other functions to compose together; it returns a new function that calls the chain of inputs in sequence.<\/p>\n<p>You\u2019ll also come across composability in modern componentized development frameworks. Here\u2019s an example of a simple set of React components:<\/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=\"javascript\">\n<pre class=\"de1\"><span class=\"kw1\">const<\/span> UserCard <span class=\"sy0\">=<\/span> <span class=\"br0\">(<\/span><span class=\"br0\">{<\/span>user<span class=\"sy0\">,<\/span> children<span class=\"br0\">}<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"br0\">(<\/span>\n    <span class=\"sy0\">&lt;<\/span>div<span class=\"sy0\">&gt;<\/span>\n        <span class=\"sy0\">&lt;<\/span>h1<span class=\"sy0\">&gt;<\/span><span class=\"br0\">{<\/span>user.<span class=\"me1\">name<\/span><span class=\"br0\">}<\/span><span class=\"sy0\">&lt;\/<\/span>h1<span class=\"sy0\">&gt;<\/span>\n        <span class=\"br0\">{<\/span>children<span class=\"br0\">}<\/span>\n    <span class=\"sy0\">&lt;\/<\/span>div<span class=\"sy0\">&gt;<\/span>\n<span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"kw1\">const<\/span> UserAvatar <span class=\"sy0\">=<\/span> <span class=\"br0\">(<\/span><span class=\"br0\">{<\/span>user<span class=\"br0\">}<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"br0\">{<\/span>\n    <span class=\"kw1\">if<\/span> <span class=\"br0\">(<\/span>user.<span class=\"me1\">avatarId<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span>\n        <span class=\"kw1\">return<\/span> <span class=\"sy0\">&lt;<\/span>img src<span class=\"sy0\">=<\/span><span class=\"br0\">{<\/span>`<span class=\"sy0\">\/<\/span>avatars<span class=\"sy0\">\/<\/span>$<span class=\"br0\">{<\/span>user.<span class=\"me1\">avatarId<\/span><span class=\"br0\">}<\/span>.<span class=\"me1\">png<\/span>`<span class=\"br0\">}<\/span> <span class=\"sy0\">\/&gt;;<\/span>\n    <span class=\"br0\">}<\/span>\n    <span class=\"kw1\">else<\/span> <span class=\"kw1\">return<\/span> <span class=\"sy0\">&lt;<\/span>img src<span class=\"sy0\">=<\/span><span class=\"br0\">{<\/span>`<span class=\"sy0\">\/<\/span>avatars<span class=\"sy0\">\/<\/span>generic.<span class=\"me1\">png<\/span>`<span class=\"br0\">}<\/span> <span class=\"sy0\">\/&gt;;<\/span>\n<span class=\"br0\">}<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"kw1\">const<\/span> UserCardWithAvatar <span class=\"sy0\">=<\/span> <span class=\"br0\">(<\/span><span class=\"br0\">{<\/span>user<span class=\"br0\">}<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"br0\">(<\/span>\n    <span class=\"sy0\">&lt;<\/span>UserCard user<span class=\"sy0\">=<\/span><span class=\"br0\">{<\/span>user<span class=\"br0\">}<\/span><span class=\"sy0\">&gt;<\/span>\n        <span class=\"sy0\">&lt;<\/span>UserAvatar user<span class=\"sy0\">=<\/span><span class=\"br0\">{<\/span>user<span class=\"br0\">}<\/span> <span class=\"sy0\">\/&gt;<\/span>\n    <span class=\"sy0\">&lt;\/<\/span>UserCard<span class=\"sy0\">&gt;<\/span>\n<span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Each component is kept simple by only concerning itself with a specific part of the overall functionality. You can render the <code>UserCard<\/code> on its own or compose a new variant with an avatar or any other React component. The <code>UserCard<\/code> isn\u2019t complicated by the logic responsible for rendering the correct avatar image file.<\/p>\n<h2 id=\"composition-vs-inheritance\"><span class=\"ez-toc-section\" id=\"Composition_vs_Inheritance\"><\/span>Composition vs Inheritance<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Object-oriented languages often achieve code reuse through the means of inheritance. Choosing inheritance as your default strategy can be a costly mistake that makes it harder to maintain a project over time.<\/p>\n<p>Most languages don\u2019t support multiple inheritance so your options for complex combinations of functionality are limited. Here\u2019s the log message from earlier refactored into a similar inheritance model:<\/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=\"kw2\">class<\/span> LogMessage <span class=\"kw2\">implements<\/span> LogMessage <span class=\"br0\">{<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">(<\/span>\n        <span class=\"kw2\">public<\/span> readonly string <span class=\"re0\">$message<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> void<span class=\"sy0\">;<\/span>\n<span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"kw2\">class<\/span> LogMessageWithEmail <span class=\"kw2\">extends<\/span> LogMessage <span class=\"kw2\">implements<\/span> Mailable <span class=\"br0\">{<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> getEmailContent<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> string <span class=\"br0\">{<\/span>\n        <span class=\"kw1\">return<\/span> <span class=\"st0\">\"New Log Message: {<span class=\"es4\">$this<\/span> -&gt; message}\"<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">}<\/span>\n<span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"kw2\">class<\/span> LogMessageWithUser <span class=\"kw2\">extends<\/span> LogMessage <span class=\"kw2\">implements<\/span> RelatedToUser <span class=\"br0\">{<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">(<\/span>\n        <span class=\"kw2\">public<\/span> readonly string <span class=\"re0\">$message<\/span><span class=\"sy0\">,<\/span>\n        <span class=\"kw2\">public<\/span> readonly int <span class=\"re0\">$userId<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span><span class=\"br0\">}<\/span>\n\u00a0\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> getTargetUserId<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> int <span class=\"br0\">{<\/span>\n        <span class=\"kw1\">return<\/span> <span class=\"re0\">$this<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">userId<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>These classes might seem helpful to begin with. Now you don\u2019t need specific log message implementations like our <code>UserLoggedInMessage<\/code> class. There\u2019s one big problem though: if you need to write a log message which relates to a user and sends an email, there\u2019s no class for that. You could write a <code>LogMessageWithEmailAndUser<\/code> but you\u2019d be starting down the slippery slope of covering every possible permutation with \u201cgeneric\u201d concrete class implementations.<\/p>\n<p>Despite these issues, code using inheritance for this kind of relationship model remains prevalent in projects large and small. It does have valid use cases but is often implemented for the wrong reasons. Small composable units based on interfaces and functions are more versatile, make you think about the bigger picture within your system, and tend to create more maintainable code with fewer side-effects.<\/p>\n<p>A good rule of thumb for inheritance is to use it when an object <em>is<\/em> something else. Composition is usually the better choice when an object <em>has<\/em> something else:<\/p>\n<ul>\n<li><strong>Log \/ Email<\/strong> \u2013 A log message is not inherently an email but it may <em>have<\/em> email content associated with it. The Log should include the Email content as a dependency. If not all Logs will have an Email component, composition should be used as shown above.<\/li>\n<li><strong>User \/ Administrator<\/strong> \u2013 The Administrator inherits all the behaviors of the User and adds a few new ones. It could be a good case for inheritance \u2013 <code>Administrator extends User<\/code>.<\/li>\n<\/ul>\n<p>Reaching for inheritance too early can restrict you later on as you find more unique scenarios within your application. Keeping units of functionality as small as possible, defining them as interfaces, and creating concrete classes that mix and match those interfaces is a more efficient way to conceptualize complex systems. It makes your components easier to reuse in disparate locations.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Composable code refers to source that combines self-contained modular units into bigger chunks of functionality. It\u2019s an embodiment of \u201chas-a\u201d relationships between different entities. The actual composition mechanism depends on the paradigm you\u2019re using; for OOP languages, you should program to interfaces rather than concrete classes, whereas functional realms often guide you towards good composability by design.<\/p>\n<p>Being proactive in your use of composable techniques leads to more resilient code that\u2019s loosely coupled, easier to reason about, and more adaptable to future use cases. Creating composable blocks is often the most effective starting point when you\u2019re refactoring large-scale systems. Although alternatives like inheritance have valid roles too, they\u2019re less broadly applicable and more prone to misuse than composability, dependency injection, and IoC.\n<\/p><\/div>\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\/15780\/what-is-composable-code-and-how-can-you-create-it\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;What Is Composable Code and How Can You Create It? \u2013 CloudSavvy IT&#8221; Shutterstock.com\/locrifa Composable code describes classes and functions that can be readily combined to create more powerful higher-level constructs. Composability compares favorably to alternative forms of code reuse such as object-oriented inheritance. It advocates the creation of small self-contained units that are treated&#8230;<\/p>\n","protected":false},"author":1,"featured_media":419329,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2022\/02\/54dc54e6.jpg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-419328","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\/419328","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=419328"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/419328\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/419329"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=419328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=419328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=419328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}