I've started to study concepts like Cohesion and principles like Single Responsibility Principle. I am very confused about the difference between them.
The web has a lot of information, but I find it very difficult to filter out the correct comparison.
What is the relationship between Cohesion and Single Responsibility? Is it possible for a class to have only one responsibility but low cohesion?
Cohesion can be seen a software quality metric, while SRP is more of a subjective software quality principle. As the Pragmatic Programmers describe it, cohesive code happens when things that belong together and depend on one another stay together.
For instance, if a class's instance variables or properties are all used by its methods and its methods alone, but the methods also do not use any external data or methods from other objects, that class is said to be highly cohesive. However, if that class happens to be reading and writing from a file, that class can be seen as not adhering to the Single Responsibility Principle, as reading and writing can be viewed as two very distinct tasks. Regardless, you can also see the class's main task as "do IO operations with the file system", so SRP is somewhat open to interpretation depending on the context it's applied.