0

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();
  1. to give back a connection
  2. to report an error if it fails
  3. 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:

  1. cohesion has to be replaced with an entirely new concept
  2. cohesion has to be "stretched" to a certain point

And since i prefer the 2nd, my question is what is that point.

AgostinoX
  • 7,477
  • 20
  • 77
  • 137

2 Answers2

1

I think you are missing some parts in OOP concepts and good design. If your code is not readable anymore - as Martin Fowler mentioned it, if your code smells - because of your tries to increase cohesion or reduce coupling (you can't remove coupling to %0 or you can't increase cohesion upto 100%. As you try this, you get a code like your connect() example above), you are on a wrong way. Because, these concepts are there to make your code more readable. There are also refactoring concepts like "extract method" to increase cohesion. Cohesion and Coupling are usually used with adjectives, "lower coupling" and "higher cohesion". How low or high they must be, the designer must decide/optimise it. By the way, if you session.connect() calls, i don't expect that the connection has to be configured. To do this, there are many other concepts like connection factory, session manager and Co. If the connection is once configured, then you can call connect() method to establish a physical connection to device (database).

Erhan Bagdemir
  • 5,231
  • 6
  • 34
  • 40
  • i agree with you. but my question is not about refactoring some code that i or my team have written. my question is about having some criteria that help in design choiches. some more objective that "a value between 0% and 100%" and "common sense" and "intuition" and "ask more experience developer". the fact to distinguish "library" code from "business logic" code and state that in library you should be more rigorous is yet a step further than the things you say :-) – AgostinoX May 30 '11 at 07:21
  • Refactoring was just an example in my answer. In software design, there are many attributes like extensibility, readability, testability etc. as one depends on another. For example, as you make your code more readible, maybe you reduce extensibility. I don't think that there is a formula which describes how much extensible your code must be while you reduce % x cohesion. – Erhan Bagdemir May 30 '11 at 08:41
  • infact probably it won't be a formula, but a set of pattenrs illustrated by use cases and use-enforced standards. – AgostinoX May 30 '11 at 08:51
1

And since i prefer the 2nd, my question is what is that point.

I don't think this is answerable in any meaningful way.

As @Erhan's answer points out, Cohesion and Coupling are measures that are "in tension" with other measures, and with the principles of design (OO and otherwise). If you ignore common sense and attempt to max/minimize these measures in isolation, you end up with unreadable code.

IMO, the best approach is to develop a good intuition of what makes your code readable and your design comprehensible. Pay some attention to the various measures, but if they contradict your intuition, be prepared to ignore them. If you are unsure if your intuition is correct, ask a more experienced developer to review your work.

Always remember that the various measures (cohesion, coupling, complexity, test coverage) are empirical, as are "good design" and "best practice". They ignore the realities of the problem that you are trying to solve. Don't use them as an excuse for not thinking and using your judgement.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • @AgistinoX - I've answered that. See the first sentence of my answer. And the last paragraph is apropos ... even if it is not the "magic bullet" answer that you want to hear. Oh course, you are free to disagree, and figure out for yourself a better way to approach this problem. – Stephen C May 30 '11 at 07:48
  • Ok.Let me explain why i don't think it is true.When an "expirienced programmer" have an "intuition",to use your words, it's not performing "art" or magician. He is just considering the problem as collocated in a broaden field, and do some logic deductions starting from more elements. *I'm intrested in this logic*. Take it out. Calling it "expirience" means "black box" it in the developer's head. I'm sure that if I had asked if there was a criteria to construct objects before the "idea" of the construction patterns, many would have said "depends on experience, there are not meaningful answers"! – AgostinoX May 30 '11 at 08:16
  • Intuition is just knowledge that's so applied and ingrained that it becomes more of a feeling than a conscious thought. In order to develop that, one needs experience. It's the process of doing something a bunch of times that leads someone to develop a feel for the "right" or "wrong" way. There's really no shortcut, other than applying someone else's dogma -- which itself often leads to bad design, as it tends to be applied out of context by people who don't have the experience to judge what they saw on the internet or in a book somewhere, or when it might not be a good fit for what they need. – cHao Jun 01 '11 at 11:19