{"id":386,"date":"2023-08-13T10:20:28","date_gmt":"2023-08-13T16:20:28","guid":{"rendered":"https:\/\/kop.lat\/blog\/?p=386"},"modified":"2023-08-13T10:20:28","modified_gmt":"2023-08-13T16:20:28","slug":"low-coupling-and-high-cohesion","status":"publish","type":"post","link":"https:\/\/kop.lat\/blog\/low-coupling-and-high-cohesion\/","title":{"rendered":"&#8220;low coupling&#8221; and &#8220;high cohesion&#8221;"},"content":{"rendered":"\n<p><strong>Low Coupling:<br><\/strong><br>Imagine you have two friends, Alice and Bob. <strong>Both<\/strong> love <strong>to<\/strong> <strong>play<\/strong> video games. <strong>Now<\/strong> Alice decides to buy a new <strong>game<\/strong> <strong>console<\/strong> and Bob wants to play <strong>games<\/strong> <strong>with<\/strong> <strong>her.<\/strong> They connect their consoles <strong>with<\/strong> a special cable. But here&#8217;s the <strong>problem<\/strong> <strong>&#8211;<\/strong> instead of a regular cable, they use a fancy cable that has all sorts of different connectors and <strong>leads.<\/strong> This <strong>sophisticated<\/strong> cable can be used <strong>to<\/strong> <strong>play<\/strong> <strong>games,<\/strong> <strong>watch<\/strong> movies, <strong>connect<\/strong> to the <strong>internet<\/strong> and even <strong>charge<\/strong> their devices.<br><br>In this analogy, fancy <strong>cables<\/strong> <strong>represent<\/strong> high coupling. High coupling means two parts of a system <strong>&#8220;like<\/strong> Alice&#8217;s console and Bob&#8217;s <strong>console&#8221;;<\/strong> <strong>interwoven<\/strong> and <strong>interdependent<\/strong> in complex ways. If something goes wrong with one part, it can affect <strong>another,<\/strong> and changing one part <strong>can<\/strong> <strong>drag<\/strong> <strong>on<\/strong> <strong>another.<\/strong><br><br>Now, imagine Alice and Bob <strong>deciding<\/strong> to <strong>buy<\/strong> simpler cables designed specifically for gaming. These cables <strong>have<\/strong> only the connectors and wires <strong>needed<\/strong> <strong>to<\/strong> <strong>play<\/strong> <strong>the<\/strong> <strong>game.<\/strong> This is <strong>a<\/strong> <strong>weak<\/strong> coupling. <strong>Loose<\/strong> coupling means that different parts of <strong>the<\/strong> system are <strong>simply<\/strong> and <strong>purposefully<\/strong> <strong>connected.<\/strong> If one part changes, <strong>it<\/strong> <strong>is<\/strong> less likely to <strong>interrupt<\/strong> or <strong>request<\/strong> changes in the <strong>other.<\/strong><br><br><strong>High<\/strong> <strong>cohesion:<\/strong><br><br><strong>Stick<\/strong> with our <strong>game<\/strong> theme. Imagine <strong>you<\/strong> <strong>are<\/strong> building a video <strong>game<\/strong> and a team of developers <strong>are<\/strong> working on it. Each developer has a specific role:<br>one person designs the characters, another works on the <strong>levels<\/strong> and <strong>the<\/strong> third <strong>does<\/strong> the sound effects.<br><br><strong>The<\/strong> <strong>high<\/strong> cohesion in this scenario means that each developer is focusing on their own specific task and doing it <strong>very<\/strong> well. The character designer <strong>does<\/strong> <strong>not<\/strong> <strong>attempt<\/strong> to create sound effects, and the level designer <strong>does<\/strong> <strong>not<\/strong> <strong>interfere<\/strong> with <strong>the<\/strong> character <strong>design.<\/strong> <strong>People<\/strong> <strong>work<\/strong> on what <strong>they<\/strong> <strong>do<\/strong> <strong>best,<\/strong> and their work is <strong>clearly<\/strong> <strong>organized<\/strong> and <strong>divided.<\/strong> On the other hand, imagine if <strong>every<\/strong> developer <strong>tried<\/strong> to do <strong>everything<\/strong> a <strong>little<\/strong> <strong>bit.<\/strong> The character designer also <strong>tries<\/strong> to create <strong>the<\/strong> sound effects, the level designer <strong>provides<\/strong> input on <strong>the<\/strong> character design, and there&#8217;s a lot of mixing and matching of <strong>quests.<\/strong><br><br><strong>Strong<\/strong> cohesion is like having a team where everyone <strong>understands<\/strong> their role and stays focused on their expertise. <strong>This<\/strong> means different parts of <strong>the<\/strong> system <strong>&#8220;like<\/strong> the <strong>game&#8217;s<\/strong> characters, <strong>levels<\/strong> and sound <strong>effects&#8221;;<\/strong> <strong>well<\/strong> <strong>organized<\/strong> and closely related to their <strong>particular<\/strong> purpose.<br><br>In a software context, <strong>loose<\/strong> coupling and high <strong>coherence<\/strong> are design principles that help create systems that are easier to understand, maintain, and update. In C# .NET programming, applying these principles means designing your code so that different components (like classes and modules) are loosely <strong>coupled<\/strong> (low coupling) and each component <strong>is<\/strong> <strong>loosely<\/strong> <strong>coupled.<\/strong> <strong>section<\/strong> <strong>with<\/strong> a clear and specific purpose (high <strong>coherence).<\/strong> This makes your code more flexible, easier to debug, and <strong>easier<\/strong> to <strong>modify<\/strong> in the future.\u00a0<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Conceptually<\/strong><\/p>\n\n\n\n<p><br>In software design, &#8220;low coupling&#8221; and &#8220;high cohesion&#8221; are two principles that aim to create well-structured and maintainable code. These principles are particularly relevant in object-oriented programming languages like C# in the context of designing classes and components. Let&#8217;s dive into each concept:<\/p>\n\n\n\n<ol>\n<li>Low Coupling: Coupling refers to the degree of interdependence between different modules or components in a software system. Low coupling implies that modules are relatively independent and have minimal knowledge of each other&#8217;s internal details. This makes the system more flexible, easier to maintain, and less prone to ripple effects when changes are made to one module.<\/li>\n<\/ol>\n\n\n\n<p>In C#\/.NET, low coupling can be achieved by:<\/p>\n\n\n\n<ul>\n<li>Using well-defined interfaces: Components should interact with each other through well-defined interfaces rather than relying on specific implementation details.<\/li>\n\n\n\n<li>Minimizing direct dependencies: Avoid directly referencing classes or components from other modules. Instead, use abstractions like interfaces or abstract classes to establish communication.<\/li>\n\n\n\n<li>Using dependency injection: Rather than creating dependencies within a class, inject the required dependencies from the outside. This reduces the class&#8217;s coupling with its dependencies.<\/li>\n<\/ul>\n\n\n\n<ol start=\"2\">\n<li>High Cohesion: Cohesion refers to the degree to which the responsibilities and functionality within a module or class are related and focused. High cohesion indicates that a module or class has a clear and well-defined purpose, and its internal components work together to achieve that purpose. This leads to more readable, maintainable, and reusable code.<\/li>\n<\/ol>\n\n\n\n<p>In C#\/.NET, high cohesion can be achieved by:<\/p>\n\n\n\n<ul>\n<li>Following the Single Responsibility Principle (SRP): Each class or module should have a single, well-defined responsibility. This ensures that the class&#8217;s functionality remains focused and easier to understand.<\/li>\n\n\n\n<li>Grouping related methods and data: Place methods and data that are closely related within the same class. This reduces the need to jump between different classes to understand the complete behavior.<\/li>\n\n\n\n<li>Avoiding &#8220;God classes&#8221;: A &#8220;God class&#8221; is a class that tries to do too much, leading to low cohesion. Splitting such a class into smaller, more focused classes increases cohesion.<\/li>\n<\/ul>\n\n\n\n<p>To sum up, low coupling and high cohesion work together to create maintainable, flexible, and understandable software systems. Low coupling reduces the interdependencies between modules, while high cohesion ensures that each module is focused and well-organized. Following these principles helps developers create more robust and maintainable C#\/.NET applications.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Low Coupling:Imagine you have two friends, Alice and Bob. Both love to play video games. Now Alice decides to buy a new game console and Bob wants to play games with her. They connect their consoles with a special cable. But here&#8217;s the problem &#8211; instead of a regular cable, they use a fancy cable [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":224,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[36,41,1,32,12,7],"tags":[],"_links":{"self":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/386"}],"collection":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/comments?post=386"}],"version-history":[{"count":1,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/386\/revisions"}],"predecessor-version":[{"id":387,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/386\/revisions\/387"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media\/224"}],"wp:attachment":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media?parent=386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/categories?post=386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/tags?post=386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}