Low coupling, high cohesion: less spaghetti, less regretti meme

meme; painting people at table eating; less spaghetti, less regretti

Low Coupling, High Cohesion is a programming principle that says that your classes should be coupled with other classes as little as possible but the cohesion inside your classes should be as high as possible. 

I like this concept and always keep it mind when developing.

It helps to keep my code clean and promotes all the other clean code principles.

It also applies to modules but I will focus on classes since that is where you will apply it, most of the time.

What does it mean?

Low coupling means your class should be coupled as little as possible with the other classes in your project.

A class is coupled with another class when it uses its methods and properties.

The more a class uses the other class the more coupled it is to it.

We can also say that the class knows the other class.

A class should know the other classes as little as possible.

When classes in your project use other classes a lot and know all of their internals, like what properties they have, what properties those properties have, your class has High coupling
This leads to a sort of entanglement called spaghetti code.

You can watch the Clean Code courses here:

- Watch Clean code with Java examples course on Udemy
- Watch Clean code with PHP examples course on Udemy
- Watch this Clean code with Java examples - Basics for free here AND get 2 FREE months of skillshare.com Premium

Leave a Reply

Your email address will not be published. Required fields are marked *