This discussion is archived
7 Replies Latest reply: Dec 3, 2010 1:03 PM by 796440 RSS

Tight Coupling

652295 Newbie
Currently Being Moderated
Given "Methods in ClassC use public methods in ClassA", then Can I say "ClassC is tightly coupled to ClassA"?
Help is appreciated.
  • 1. Re: Tight Coupling
    796440 Guru
    Currently Being Moderated
    user7 wrote:
    Given "Methods in ClassC use public methods in ClassA", then Can I say "ClassC is tightly coupled to ClassA"?
    Help is appreciated.
    If ClassC refers to ClassA directly, then yes. However, if ClassA implements InterfaceI, and ClassC's code refers only to InterfaceI, and ClassA just happens to be the implementation that ClassC gets at runtime, then ClassC is not tightly coupled to ClassA.

    And even if there are direct references to ClassA, if there are only a few of them, and everything else refers to methods that are common to both ClassA and ClassB, so that it would be a simple change to replace ClassA with ClassB, then I would also say it's not that tightly coupled.

    Edited by: jverd on Dec 2, 2010 1:13 PM
  • 2. Re: Tight Coupling
    652295 Newbie
    Currently Being Moderated
    "ClassC uses public methods in ClassA" could be like below also...

    ClassA {
    public double caculateTax(String state) { }
    }

    ClassC {
    public void generateReport(){
    .
    .
    double tx = calculateTax("CO");
    .
    .
    }

    Then, are ClassC is not tightly coupled with ClassA?
  • 3. Re: Tight Coupling
    796440 Guru
    Currently Being Moderated
    user7 wrote:
    "ClassC uses public methods in ClassA" could be like below also...
    No, it's not using anything in ClassA.
    ClassA {
    public double caculateTax(String state) { }
    }
    
    ClassC {
    public void generateReport(){
    .
    .
    double tx = calculateTax("CO");
    .
    .
    } 
    ClassC is not using ClassA at all. You're calling calulateTax() without a reference to whose calculateTax() method, so it must be ClassC's.
    Then, are ClassC is not tightly coupled with ClassA?
    ClassC is not using ClassA at all. Now, if you meant to say:
    doubl tx = SOME_CLASS_A_INSTANCE.calculateTax("CO");
    Then C is directly using A in a way that I would consider tight coupling.

    As it stands, your question is internally inconsistent.

    Edited by: jverd on Dec 2, 2010 2:55 PM
  • 4. Re: Tight Coupling
    652295 Newbie
    Currently Being Moderated
    ClassA {
    public double caculateTax(String state) { }
    }

    ClassC {

    public void generateReport(){
    .
    .
    double tx = new ClassA().calculateTax("CO");
    .
    .
    }

    My bad, I code snippet was missing reference to ClassA.
    I think now "ClassC is tightly coupled to ClassA", since ClassA is not implementing any Interface and ClassC is using the direct reference of ClassA?
  • 5. Re: Tight Coupling
    796440 Guru
    Currently Being Moderated
    user7 wrote:
    ClassA {
    public double caculateTax(String state) { }
    }

    ClassC {

    public void generateReport(){
    .
    .
    double tx = new ClassA().calculateTax("CO");
    .
    .
    }

    My bad, I code snippet was missing reference to ClassA.
    I think now "ClassC is tightly coupled to ClassA", since ClassA is not implementing any Interface and ClassC is using the direct reference of ClassA?
    Yes, I would agree. However, "tightly coupled" is a continuum, is relative, and is somewhat subjective. There's no real meaning to "tightly coupled" with such a tiny, isolated toy example. Being "tightly coupled" is not an on-off switch. It's a gradient, and the goal is generally to minimize or reduce coupling as much as possible, so that now we are less tightly coupled than we were yesterday.
  • 6. Re: Tight Coupling
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    jverd wrote:
    It's a gradient, and the goal is generally to minimize or reduce coupling as much as possible, so that now we are less tightly coupled than we were yesterday.
    Errr...no.

    The goal is to minimize coupling to minimize cost in terms of business needs. So use some reasonable criteria in terms of the business (problem domain) to decide when to focus on it and when to ignore it.

    Using the phrase "as much as possible" might lead one to decide that every class should have an interface and every class should have a factory (which is a situation that one long term poster encountered in one system.)
  • 7. Re: Tight Coupling
    796440 Guru
    Currently Being Moderated
    jschell wrote:
    jverd wrote:
    It's a gradient, and the goal is generally to minimize or reduce coupling as much as possible, so that now we are less tightly coupled than we were yesterday.
    Errr...no.

    The goal is to minimize coupling to minimize cost in terms of business needs.
    Yeah, "as much as possible" was a poor choice of words. Just like when somebody says, "How can I make operation X as fast as possible?" and it detracts from my actual point in that sentence, which is that it's not a yes/no proposition.

    A better wording would be: It's a gradient, and one goal of good design is to minimize coupling, but this must be balanced against other, sometimes contradictory goals, such as simplicity, performance, or time to market.

    Edited by: jverd on Dec 3, 2010 1:03 PM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points