Post

Low Coupling

Low Coupling

Low Coupling


Prerequites

1
NOTHING

What is Low Coupling

1. What is Low Coupling?

Low Coupling is principle of software design. Low Coupling is about keeping low depencey between functions and it is to be robust for software.

Actually During my first two years as a junior developer at the company, i didn’t care this. Because making good performance and developing as fast as possible is the most important part of developer. Yes, I think so too, now also. But After that assume to develop the good program explictly, I found the design of my function is too bad.

As time is passed, my algorithm is to be more complex and not to easily change the code when i have issue unexpected.

The vicious circle is like that:

1
2
3
4
5
6
7
8
9
10
Catch A function → Revise A function
→ Execute the program 
→ Something wrong i didn't expect → Check programs
→ Catch B function issued after revising A fucntion →  Revise B function
→ Execute the program
→ Something wrong i didn't expect → Check programs
→ Catch C function issued after revising A or B fucntion →  Revise C function
→ Execute the program
→ Something wrong i didn't expect → Check programs
→ And So on..... Annoying 

And the worst case is i found if i want to revise A fucntion, i should revise B ~ Z fucntions and change the member variables and so on.

I think everyone have experienced about that. Because even though you know the design, unlike mathematics there are no strictly defined rules, so sometimes it can vary subjectively depending on the situation and design decisions.

2. How to deal with Low Coupling

There’s no rule, Very ambigious BUT i will post my rule for Low Coupling.

Step 1. Ambigious status

When we design the architectures, very hard to define couplings. So at the first time, i just program the architectures without strictly rules but trying to split the code into functionality unit or semantic unit.

Step 2. Implementation status

First time, i divide units of function and think that how can someone who didn’t know the function understand the flow by my split

Step 3. Check the dependency of function’s parameters

The More it is divided, The less depency between parameters of different functions. So i check the parameter dependency. If i change a parameter from A function, it can effect the parameter from B function directly

Step 4. Semantic Unit

The last thing is i think beacuse the split should have semantic information, checking the semantic duplicate between different functions.

If the semantic of function is ambigious, i should be more think about it is really independent or not.

3. What is effect with Low Coupling

Let’s see the vicious circle is like that:

1
2
3
4
5
6
7
8
9
10
Catch A function → Revise A function
→ Execute the program 
→ Something wrong i didn't expect → Check programs
→ Catch B function issued after revising A fucntion →  Revise B function
→ Execute the program
→ Something wrong i didn't expect → Check programs
→ Catch C function issued after revising A or B fucntion →  Revise C function
→ Execute the program
→ Something wrong i didn't expect → Check programs
→ And So on..... Annoying 

Assume they are independent:

By means that is the cycle of correction and happening is stopped thanks to no dependency.

1
2
3
Catch A function → Revise A function
→ Execute the program 
→  Finish

Even though this situation is happend to us

1
2
3
4
5
Catch A function → Revise A function
→ Execute the program 
→ Something wrong i didn't expect → Check programs

→ Catch B function issued WITHOUT revising A fucntion →  Revise B function

The issue arose from problems derived from the original problem itself, NOT from function dependencies.

4. Example

Assume to develop add operation between two images. maybe we make a algorithm this process.

1
Bring Images → Add Images

Very Simple! There’s no reason to consider a design of program. But if we extend more functions like another format of image or ROI. Most of the developers I’ve seen check the all of flow and add the function. and sometimes they struggled with a large number of variables introduced earlier without proper consideration for modular indepence. I’m also suffered from this when i was junior.

BUT now i always design the function with decoupling each features. If i make the Add Operation of image processing, i will structure the algorithm like this

1
Initialize Parameters → Make Region of Interest for add → Operation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
bool Process()
{
    bool bOK = false;

    do
    {
        if(bOk = Init())
            break;

        SRect sRtVaild;
        {
            if(bOk = ValidRegion(&sRtVaild))
                break;
        }

        SOptiTable sOptiTable;
        {
            if(bOK = CreateOptiTable(&OptiTable))
                break;
        }

        if(bOK = Calcuation(&sRtVaild, &OptiTable))
            break;

        bOK = true;
    }
    while(false);

    return bOK;
}
This post is licensed under CC BY 4.0 by the author.