When you call a method from a library you expect that it does exactly what its name implies it will do.
Connection c = driver.getConnection();
- to give back a connection
- to report an error if it fails
- not to do more than expected
When writing "library code" it is very easy to stick with the decoupling, cohesion, abstraction principles. Not adhering to these, most times, means an error in design.
But when you are in business logic, some difficulties arise and perhaps the perspective has to be changed. At the business logic level i say:
session.connect(); //session is of Session type, my "business logic class"
and i expect that it reads the config file, takes defaults if it doesn't find it, connect, do some checking, decide which warning need to be notified to the user, for example the fact that the database version is older than the client software.
If you say that a method shouldn't do all this stuff because it must be cohesive, and apply this rigorously, you'll end up with a connect method with just one line:
public class Session {
...
Connection c;
public void connect() {
c = driver.getConnection();
}
}
that is, the method connect
of the business class Session will collapse to the underlying classes.
And the rest of the tasks? Read the file, check the db version etc?
You are postponing the businness code to a "big method" that will do all the logic.
And this is exactly my point.
If you apply cohesion and decoupling principles in a business logic context, you'll fail to subdivide tasks into smaller sub-tasks, and you will have some big "monolithic" methods doing all the work and being poorly readable.
I find the good principles (cohesion, abstraction) for low level libraries (Driver.getConnection()) quite inadequate for businness logic.
My ideas are:
- cohesion has to be replaced with an entirely new concept
- cohesion has to be "stretched" to a certain point
And since i prefer the 2nd, my question is what is that point.