Cohesion and Coupling

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

Time
35 hours 25 minutes
Difficulty
Advanced
Video Transcription
00:00
>> Let's talk about a couple of
00:00
additional aspects in object-oriented programming.
00:00
Let's talk about cohesion and coupling.
00:00
Of course, we're going to define
00:00
what cohesion is, what coupling is.
00:00
We're going to talk about
00:00
the relationship they have with each other.
00:00
They have almost an inverse relationship
00:00
>> with each other.
00:00
>> Let's start with cohesion.
00:00
Right off the bat, I'm going to tell you
00:00
high cohesion is desirable.
00:00
You can remember that
00:00
>> because they both have h's in them, so high cohesion.
00:00
>> Remember that.
00:00
>> That's what you want, high cohesion.
00:00
>> But what is cohesion?
00:00
Cohesion is singleness of purpose.
00:00
For instance, if you are one of those rare people
00:00
>> that can walk into a grocery store
00:00
>> to get a gallon of milk,
00:00
>> and you can come out with just a gallon of milk,
00:00
>> you are cohesive.
00:00
>> I'm not cohesive.
00:00
I won't wind up being the person in line
00:00
>> with 32 items all balanced in my hands,
00:00
>> always dropping something.
00:00
>> Because as soon as I walk into the grocery store,
00:00
I remember 13,000 other things I need.
00:00
I'm not cohesive at all.
00:00
But if you're cohesive,
00:00
walk into the grocery store,
00:00
buy what you came there to get, and leave.
00:00
If I hear cans of vegetables
00:00
>> being knocked off the shelf
00:00
>> in the canned vegetable aisle,
00:00
>> I know that's not you
00:00
>> because you weren't in the canned vegetable aisle.
00:00
>> You weren't over there
00:00
interfering with things you had no business in,
00:00
you did what you did,
00:00
what you came to do, and you left.
00:00
You weren't the person
00:00
>> that knocked all the ice cream out of the freezer
00:00
>> because you weren't in the freezer aisle.
00:00
>> If we take that and we apply it to an application,
00:00
we have specific modules of code
00:00
>> in object-oriented programming.
00:00
>> Does this module of code keep to itself?
00:00
Is it focused and cohesive?
00:00
It's not flailing about
00:00
interfering with other modules of code.
00:00
For instance, if I were to install a module of code
00:00
>> and something else broke or stopped working,
00:00
>> that's not a cohesive module of code.
00:00
My module of code would have low cohesion
00:00
>> if installing it
00:00
>> caused other elements not to work properly.
00:00
>> But if I have a tightly bound
00:00
>> well-written module of code
00:00
>> that just performs a specific function,
00:00
>> then I'm much less likely to interfere
00:00
>> with other modules
00:00
>> operating and performing other things.
00:00
>> Then we have coupling.
00:00
We want loose coupling.
00:00
As you can see, the l's go together.
00:00
L in loose, l in coupling.
00:00
That's what's desirable.
00:00
Coupling is the interdependency.
00:00
Interdependency is not a bad thing.
00:00
As matter of fact, dependency is good to a degree.
00:00
For instance, if you were in high school
00:00
>> and your best friend broke up
00:00
>> with their boyfriend or girlfriend
00:00
>> and they come to sit on your couch eating ice cream
00:00
>> going, "My life is horrible, they left me."
00:00
>> That person is too highly coupled.
00:00
Coupling is fine.
00:00
Nothing wrong with coupling.
00:00
But when you're so coupled
00:00
>> that one other module will break your module,
00:00
>> that is high coupling or tight coupling,
00:00
>> and that's not desirable.
00:00
>> With an application, if you think about
00:00
>> when you go to install things in Windows,
00:00
>> maybe in Windows Server,
00:00
you might want to go install certificates services.
00:00
What it's going to tell you is,
00:00
in order to install certificate services,
00:00
you have to install
00:00
Microsoft's Internet Information Server.
00:00
The reason for that is IIS has modules of code
00:00
>> that are coupled with other applications.
00:00
>> That's good because you don't want to have
00:00
>> to keep creating the same module of code
00:00
>> and installing it over and over,
00:00
>> certificate services can use
00:00
the module of code that's in IIS, so that's good.
00:00
Now the problem might be if you go to shut down IIS,
00:00
if so many other applications are tightly coupled,
00:00
>> that when you shut down IIS,
00:00
>> it'll also shut down certificate services,
00:00
it will shut down certain database applications
00:00
>> that you might be running.
00:00
>> It will shut down just various elements.
00:00
When things are tightly coupled,
00:00
they're too dependent on other applications.
00:00
It's not that coupling is bad,
00:00
it's that we want loose coupling.
00:00
Here's just a little chart that reminds you
00:00
>> what's good about cohesion,
00:00
>> what's bad about it,
00:00
what's good about coupling, and what's negative.
00:00
This is usually worth a question,
00:00
cohesion and coupling.
00:00
You want high cohesion, loose coupling.
Up Next