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