{
    "href": "/post/2014/10/06/whats-the-difference-between-tightly-loosely-and-de-coupled/",
    "relId": "2014/10/06/whats-the-difference-between-tightly-loosely-and-de-coupled",
    "title": "What's The Difference Between Tightly-, Loosely-, and De-Coupled ?",
    "author": "pmjones",
    "markup": "html",
    "tags": [
        {
            "href": "/tag/aura/",
            "relId": "aura",
            "title": "Aura",
            "author": null,
            "created": "2020-09-14 21:51:57 UTC",
            "updated": [
                "2020-09-14 21:51:57 UTC"
            ],
            "markup": "markdown"
        },
        {
            "href": "/tag/patterns/",
            "relId": "patterns",
            "title": "Patterns",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        },
        {
            "href": "/tag/php/",
            "relId": "php",
            "title": "PHP",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        },
        {
            "href": "/tag/programming/",
            "relId": "programming",
            "title": "Programming",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        }
    ],
    "created": "2014-10-06 12:00:02 UTC",
    "updated": [
        "2014-10-06 12:00:02 UTC"
    ],
    "html": "<p>In <a href=\"https://twitter.com/brandonsavage/status/518043012623265792\">a tweetstorm</a> that spun up late last week, Taylor Otwell produced the following commentary:</p>\n<blockquote>\n<p>look guys I\u00e2\u0080\u0099m \u00e2\u0080\u009cdecoupled\u00e2\u0080\u009d because this package doesn\u00e2\u0080\u0099t have composer dependencies!!! HAHAHAHA LOL</p>\n<p>how many composer packages a given package has does NOT affect YOUR code\u00e2\u0080\u0099s decoupling.</p>\n<p>that is a matter of programming to an interface, etc.</p>\n<p>you people seriously do not understand decoupling. at all.</p>\n<p>if you type hint AuraAnything that is a HARD, CONCRETE dependency. THAT is coupling.</p>\n<p>IlluminateContracts are all interfaces. abstractions. not concretions. THAT\u00e2\u0080\u0099s decoupling.</p>\n<p>IlluminateContractsViewFactory could be a Laravel view factory, could be an Aura one. That\u00e2\u0080\u0099s decoupling.</p>\n<p>how many composer pkgs the IMPLEMENTOR needs is an implementation detail my consuming code need not care about</p>\n<p>consuming code ONLY cares about programming to an interface for decoupling.</p>\n<p>you [@philsturgeon] and brandon [savage] and paul [jones] don\u00e2\u0080\u0099t understand basic programming concepts like coupling</p>\n<p>and think somehow coupling is tied to composer</p>\n<p>Aura ships hard concretions = you are tightly coupled to Aura.</p>\n<p>which should my consuming code give a shit if Aura is decoupled AMONGST ITSELF. Nobody gives a shit.</p>\n<p>i only care if MY code is coupled to Aura.</p>\n<p>and since Aura makes you depends on hard concretions, it promotes hard couplings.</p>\n<p>I\u00e2\u0080\u0099m saying if you type-hint a class dependency, you are coupled to that implementation (cont)</p>\n<p>regardless of that package\u00e2\u0080\u0099s internal dependencies</p>\n</blockquote>\n<p>While some of Taylor\u2019s rant is correct for as far as it goes, much of it glosses over important distinctions in subtle misdirection, and the remainder displays some misunderstandings. He is also flat wrong in his assertions of other peoples\u2019 understanding of \u201cbasic programming terminology.\u201d As such, I think his words demand a careful response for future reference.</p>\n<p>First, I\u2019m glad to see Taylor paying attention to the proper use of terminology in a software context. This is something he\u2019s not always been great at in the past, and I encourage him here.</p>\n<p>But I can\u2019t quite tell if Taylor thinks the developers who <em>use</em> Aura believe <em>their</em> code is decoupled <em>by virtue of using Aura</em>. Or maybe it\u2019s that the Aura marketing phrase \u201cfully decoupled libraries\u201d is the target of his ire. I infer allusions to both from his tweets, so I\u2019ll attempt to address both possibilities. (Perhaps there is some other interpretation I have missed.)</p>\n<p>It should be so obvious as to not require stating, but for the sake of explicitness: If your code has a dependency on classes in a particular thrid-party package, your code is tightly coupled to the code in that package. This is true for any classes in any library, framework, or other package code. So, if you believe that depending on an Aura library in your code makes your code \u201cdecoupled\u201d then you are mistaken. As far as I know, I have never attempted to state or imply otherwise. I don\u2019t think any Aura users have this misperception, but if so, consider this a corrective.</p>\n<p>The fact that <em>your</em> code could be tightly coupled to another package does not mean that the <em>other package</em> is coupled to anything else. That is to say, the other package might have no couplings of any sort to any other code outside itself. The other package in that case is <em>de</em>-coupled.</p>\n<p>The Aura library packages are designed with that kind of decoupling in mind. That is, no Aura library package depends on anything at all in any other Aura package. Each of the Aura libraries is thus fully decoupled from the others, and incidentally from any framework that is composed of them. (Note that the *_Kernel and *_Project packages <em>are</em> coupled to other packages; the decoupling principle applies only to the Aura library packages.)</p>\n<p>But why would you care if a particular library package is itself decoupled from other packages?  I assert that one reason (of many) you want libraries that are themselves decoupled is so that, if you have to swap one out in your own code, you only have to worry about the one library, not about all the  dependent libraries that it is coupled to (and all the dependent libraries <em>they</em> are coupled to). Swapping out is still tedious: you will need to work through your code, change all the typehints from that library\u2019s classes to those in another, <em>and</em> change all the injections that specify classes from the library. But at least it\u2019s only the one library; the fact that the library is itself decoupled reduces the swapping-out work.</p>\n<p>Taylor points out another level of coupling called \u201cloose\u201d coupling. This means that, instead of your code depending on a particular class, you instead depend on an interface. This couples you to the interface, but not to any particular implementation. If your code depends on interfaces, your code is <em>loosely</em> coupled to the implementations of those interfaces (although I don\u2019t think this means you are <em>de</em>-coupled \u2013 there\u2019s still some knowledge necessary for interactions).</p>\n<p>Being loosely coupled is a good situation to be in compared to being tightly coupled. If you need to swap out an implementation of an interface, you won\u2019t need to change your typehints (unless you swap to another set of interfaces). However, you will still need to change all your injections to the new implementation. Overall, being loosely coupled makes for less work when swapping out libraries.</p>\n<p>How can you tell if a package is coupled to another package? Provided that composer.json is not lying, it\u2019s easy enough to examine the \u201crequire\u201d element to see if there are other packages listed there. If there are, then it seems likely that the package is coupled to whatever is required. You need to exercise a little judgment, though. If the required package contains only interfaces, then the coupling is \u201cloose\u201d. Otherwise, it is \u201ctight\u201d. If there are no other required packages at all, then the package has no coupling of any sort; it is fully decoupled from anything outside of it.</p>\n<p>However, that\u2019s only if you assume composer.json is not lying. To really discover the coupling of a particular package, you would need to examine its code. Any uses of interfaces defined outside the package indicates loose coupling, uses of classes defined outside the package indicates tight coupling, and no uses of interfaces or classes outside the package indicates full decoupling.</p>\n<p>(Note that this discussion is of inter-package coupling. Even if the classes <em>inside</em> a package may still be coupled to each other, the package as a whole may still be decoupled from any other package.)</p>\n<p>Having said all this, Taylor is trying out a \u201ccontracts\u201d package that exposes the Laravel interfaces independently of the implementations. I think this is a neat idea. It\u2019s the only truly new thing I\u2019ve seen introduced to the PHP community by the Laravel codebase, and I think it is worthy of emulation.</p>\n<p>Even so, if the \u201ccontracts\u201d include anything besides interfaces, I think coupling to them might be defined as \u201ctight\u201d. I am thinking specifically of the Exception classes included in the \u201ccontracts\u201d package. Although it may be fair to think that Exceptions are exempt from coupling rules, perhaps they would be better provided as interfaces to Exceptions, instead of classes proper. I will reserve my judgment on that for a later time.</p>\n<hr>\n<p class=\"reddit-links\">Read the Reddit discussion about this post <a href=\"https://www.reddit.com/r/PHP/comments/2ifybu/whats_the_difference_between_tightly_loosely_and/\">here</a>.</p>\n"
}
