0

As far as I know, coupling is about counting number of other classes in a class, so for the following program that emulates a UI program that shows "WelcomePage" at start and also pops a AboutDialog that requires token to fetch information to show, lets count the number of "other classes":

Global state version:

public class UserData{ //Number of "other classes" : 1 (String)
    public static String token;
}

public class WelcomePage{ //Number of "other classes" : 2 (String,AboutDialog)
    public static void main(String[] args){
        // some indirect initialization of UserData.token
        // utilizing another class, so WelcomePage
        // does not access UserData directly
        new AboutDialog().show();
    }
}

public class AboutDialog{ //Number of "other classes" : 2 (HttpUtils,UserData)
    public void show(){
        HttpUtils.get("(some url)/?token="+UserData.token");
    }
}

non global state version:

public class UserData{ //Number of "other classes" : 1 (String)
    public String token;
}

public class WelcomePage{ //Number of "other classes" : 3 (String,AboutDialog,UserData)
    public UserData userData=null;
    public static void main(String[] args){
        //some explicit initialization of this.userData.token
        new AboutDialog().show(this.userData);
    }
}

public class AboutDialog{ //Number of "other classes" : 2 (HttpUtils,UserData)
    public void show(UserData userData){
        HttpUtils.get("(some url)/?token="+userData.token");
    }
}

As the "non global state" version of WelcomePage has one more class : UserData, so for the whole system, the "global state" version has less coupling than "non global state" version. Also I believe it is generally true for other cases because when turning "global state" to "pass parameter", the intermediate class need to add the parameter of "UserData", resulting one more class to know, and hence increasing coupling of the whole system when eliminating global state.

However, the answer https://softwareengineering.stackexchange.com/a/401717/432039 which states the code similar to above has no difference in terms of coupling. As the answer says, WelcomePage depends on AboutDialog and AboutDialog depends on global UserData, so WelcomePage is also depend on UserData, result in no change of coupling.

So what I don't understand is, isn't "coupling" about counting number of other classes in a class? Does I have misconception about "coupling" when came across global variables? Does global variable has special coupling counting rule (eg: does a global variable count as coupling to another class even if the another class does't use it)?

Note : the question is just about coupling change after eliminating global state with passing parameters, not about raising new reasons of support using global states

3
  • I'm surprised that this doesn't seem to have a clear-cut duplicate. There's of course softwareengineering.stackexchange.com/questions/148108/… but none of the answers explicitly deal with the coupling inherent in global objects Commented Feb 22 at 8:55
  • 1
    The example in this question was misleading. You oversimplified the example from the original question softwareengineering.stackexchange.com/q/401705 up to a point where the math looked wrong. I took the freedom to fix this for you.
    – Doc Brown
    Commented Feb 22 at 10:53
  • 2
    Coupling is an abstract concept for how interdependent different components are. This is typically used in a negative sense, e.g. if high coupling is preventing changes from being made or if encapsulation is broken so that one component accesses another's internal details. Counting dependencies is an attempt to quantify coupling, but this metric of coupling is not actually coupling. Similarly, code complexity metrics are an attempt to quantify human (subjective) perception of complexity.
    – amon
    Commented Feb 22 at 11:51

3 Answers 3

3

Coupling comes in many forms.

For globals it's called:

Common coupling

Common coupling is said to occur when several modules have access to the same global data. But it can lead to uncontrolled error propagation and unforeseen side-effects when changes are made.

Wikipedia - Coupling (computer programming)

For parameters it's called:

Data coupling

Data coupling occurs when modules share data through, for example, parameters. Each datum is an elementary piece, and these are the only data shared (e.g., passing an integer to a function that computes a square root).

Wikipedia - Coupling (computer programming)

So if both lead to coupling why prefer one over the other? Because parameters are easier to reason about than global state. You're forced to show what you depend on right at the top. You can statically mark in the code where info is sent from one bit of code to another. Globals confound all that. Sure, there is still coupling here. But it's a better coupling.

So what I don't understand is, isn't "coupling" about counting number of other classes in a class?

Not only is coupling not simply about other classes, it's not simply about counting. If your ideal coupling metric is zero no communication can happen at all.

The concept of coupling is meant to encourage us to simplify all forms of contact between chunks of code. The less I have to hold in my head at any one time the happier my brain is.

Sometimes fewer is better. Sometimes simpler is better. Sometimes different is better. Going from global to parameters is a case of different being better.

1

In my answer to the question Are open-closed principle" and "less coupling" rationales to use global state?, I was not talking about metrics which measure the number of direct dependencies exclusively, like efferent and afferent coupling.

What I had in mind was simply "can a component or class like WelcomePage put into a library which will work without the availability of the class UserData"?

  • If "yes", I consider WelcomePage as decoupled from UserData,

  • if "no", I consider it as as coupled.

Because of the transitive dependency, both of the code variants (global state or not) will count as coupled in this sense.

So in terms of coupling metrics, one can say I had the "number of transitive dependencies" in mind. I don't know if that metrics has a fancy name, but I think in the context of that question it is the metrics which matters.

0

isn't "coupling" about counting number of other classes in a class?

No, this rule is overly simplistic and the linked answer is more correct, you have to count all the classes that are linked together.

Does global variable has special coupling counting rule (eg: does a global variable count as coupling to another class even if the another class does't use it)?

I've never heard of this counting rule, it sounds like its a quick rule of thumb to explain coupling rather than a precise definition. If you want to expand it to include global variables it could get complicated, but I think the addition of "also count classes in classes that are referenced" would be a simple way of expanding the rule.

The main problem with global variables is that they can be used anywhere, so maybe all classes are potentially coupled to the global variable, even if its not used at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.