.NET Tips and Tricks

Blog archive

Making Your Code More Loosely Coupled

As programmers, we often talk about how it's better for components of an application to be isolated from each other. We recognize that, with our present programming tools, some level of coupling is unavoidable. We take it for granted, for example, that we have to use "signature coupling": To call a method we must know the method's name, the format of its parameter list and the data type of its return value.

But we also recognize that when we go beyond that level of coupling we start to create problems for ourselves. The primary problem is that, as components are more tightly coupled, changes in one component require changes in other components … and changes in those "other components" then require changes to still more components.

Where, I think, many developers don't realize that they've made their components too tightly coupled is in what's called "control coupling." Control coupling occurs when you pass a parameter to a method that controls the way the method behaves. The clue here is that inside a method you have an If statement (or some other conditional) that tests a parameter passed to the method and, based on that test, has the method do something different (other than, perhaps, throwing an exception because bad data was passed in the parameter).

Effectively, with control coupling, the code that calls a method is messing with the internal processing of that method. With this level of coupling any changes to how those control parameters are used (in either the method being called or in the calling code) are going to require changes to the other set of code.

The first step in reducing this level of coupling is to recognize that when you write code like this you're probably violating the separation of concerns principle: You have a method that does two things rather than doing one thing well. Your If statement is selecting between the two different things your method is doing. You're probably going to be better off to write two methods, each of which handles one piece of functionality.

There are two benefits here. First, all the versions of your method will now be much simpler. Simply eliminating an If statement cuts your method's complexity in half (after all, now there's only one path through the method to test). Second, when you want to change how things are done, you won't change any existing code. Instead, you'll write a new method that does the right thing and have your application call that method. By doing this, you avoid violating Vogel's first law of programming: Don't screw with working code!

If you feel that's going to make calling the right method hard to do, remember that you have options for simplifying that. Overloading will let you create multiple versions of a single method, each version of which is dedicated to handling a specific parameter list.

Moving code to classes derived from the same base class will let you hide different versions of the code behind the same signature (for example, the CreditCheck method does something different in PremiumCustomer than it does in DeadbeatCustomer). Any common code shared between the two versions can be put in the base class. You don't even need a base class: The Strategy and Role design patterns also allow you to put different implementations behind the same signature, as will effective use of interfaces.

If you value loose coupling, reducing control coupling might be your next step.

Posted by Peter Vogel on 02/26/2016


comments powered by Disqus

Featured

  • ASP.NET Core and Blazor Dominate 'Relatively Small' .NET 6 Preview 6

    In what Microsoft called a “relatively small” release, .NET 6 Preview 6 delivered its biggest set of updates to ASP.NET Core and Blazor. New features include required component parameters, JavaScript interop enhancements, and support for state persistence in Blazor Server apps. Other areas like .NET MAUI and EF Core saw targeted improvements focused on UI styling, accessibility, and standardized configuration conventions. Visual Studio 2022 Preview 2 is now officially supported, enabling key tools like .NET MAUI development and C# Hot Reload.

  • GitHub Copilot Is Rising All-Time Contributor to .NET MAUI Repo

    The .NET MAUI team is pursuing a dream scenario where GitHub Copilot handles a straightforward issue all by itself with just minimal initial instructions.

  • Kernel Ridge Regression with Stochastic Gradient Descent Training Using C#

    Dr. James McCaffrey presents a complete end-to-end demonstration of the kernel ridge regression technique to predict a single numeric value. The demo uses stochastic gradient descent, one of two possible training techniques. There is no single best machine learning regression technique. When kernel ridge regression prediction works, it is often highly accurate.

  • VS Code v1.102 Doubles Down on GitHub Copilot and AI-Enhanced Development

    VS Code’s June 2025 update supercharges GitHub Copilot with new chat customization tools, smarter inline completions, and an open-source Copilot Chat extension that makes AI-assisted coding more transparent and flexible.

  • Azure Vibe Coding for the Enterprise Masses: Microsoft Partners with Replit

    Replit has partnered with Microsoft to bring its AI-powered, natural language coding platform to Azure, enabling enterprise workers to build and deploy software without writing code—marking a major step toward agentic, no-code application development at scale.

Subscribe on YouTube