views:

372

answers:

9

Do they contradict?

Decoupling is something great and quite hard to achieve. However in most of the applications we don't really need it, so I can design highly coupled applications and it almost will not change anything other than obvious side effects such as "you can not separate components", "unit testing is pain in the arse" etc.

What do you think? Do you always try to decouple and deal with the overhead?

+2  A: 

I (almost) always decouple. Every time I did this I found it useful, and (almost) every time I didn't I had to do it later. I've also found it a good way to decrease the number of bugs.

Hosam Aly
A: 

Well, YAGNI is little more than a bogus simplistic phrase people throw around. Decoupling, however, is a fairly well understood concept. YAGNI seems to imply that one is some sort of psychic. It's just programming by cliche, which is never a good idea. To be honest, there is a case to be made that YAGNI is probably not related to decoupling at all. Coupling is typically "quicker" and "who knows if you are you are going to need a decoupled solution; you aren't gonna change X component anyway!"

BobbyShaftoe
YAGNI implies that one is _not_ some sort of psychic -- see http://c2.com/cgi/wiki?YouArentGonnaNeedIt
Roger Lipscombe
I know exactly what it is but you are just playing a game with it. But you show why it is essentially a silly phrase. If you want to debate whether YAGNI is psychic gibberish or just myopia, I won't do it because that's a boring, fruitless discussion.
BobbyShaftoe
+2  A: 

Decoupling for the sake of decoupling can be bad. Building testable components is very important though.

The hard part of the story is to know when and how much decoupling you need.

cherouvim
Why do you say decoupling for the sake of it can be bad?
MrWiggles
I mean decoupling without a real benefit but simply for doing it, even if it takes longer.
cherouvim
A: 

As your tag says, it's highly subjective. It rests entirely upon your own engineering wisdom to decide what you "ain't gonna need". You may need coupling in one case, but you won't in another. Who is to tell? You, of course.

For a decision so subjective, then, there cannot be a guideline to prescribe.

Frederick
Thanks for the answer, I'm not looking for a guideline as I said I'm curious about what other people are doing. What's your idea on the subject and when / which cases you fell like it and not feel like it. Sorry if that wasn't clear on the question.
dr. evil
+2  A: 

I'd say they don't. Decoupling is about reducing unnecessary dependencies within code and tightening up accesses through clean, well-defined interfaces. "You ain't gonna need it" is a useful principle which generally advises against over-extensibility and overly broad architecture of a solution where there's no obvious and current use case.

The practical upshot of these is that you have a system where it's much easier to refactor and maintain individual components without inadvertently causing a ripple effect across the entire application, and where there are no unnecessarily complicated aspects to the design - it's as simple as is required to meet the current requirements.

Rob
A: 

YAGNI the mess :) ... really, we don't need to have all the code mixed to go "faster".

The unit tests really help on feeling when it is coupled (given one understand well what is an unit test vs. other types of tests). If you instead do it with the "you can not separate components" mindset, you can easily get to add stuff that you ain't gonna need :)

I would say YAGNI comes in when you start twisting and changing logic all around beyond the actual usage scenarios the current implementation calls for. Lets say you have some code that uses a couple external payment providers that both work with redirects to an external site. It is ok to have a design that keeps everything clean, but I don't think it is ok to start thinking of providers that we don't know if will be ever supported that have plenty of different way of handling the integration and the related workflow.

eglasius
+1  A: 

If "unit testing is a pain in the arse" then I would say that you do need it. Most of the time decoupling can be achieved with virtually zero cost as well, so why wouldn't you want to do it?

Furthermore, one of my biggest bugbears when working on a new codebase is having to decouple the code before I can start writing unit tests when the introduction of an interface somewhere or use of dependency injection could save alot of time

MrWiggles
How would you achieve decoupling with virtually zero cost? That's almost impossible in my mind. I'm curious about this, because I think most of the people agree that decoupling is quite hard to achieve. Especially when even the simplest things like reading a configuration requires dep. injection
dr. evil
Dependency injection should be done as a matter of course (and will be if you're unit testing properly). You don't need a DI container to achieve this but will see you writing some boilerplate code to achieve it. Guice gives you DI with very little setup and can be retrofitted quite easily
MrWiggles
+4  A: 

It seems to me decoupling and YAGNI are very much complementary virtues. (I just noticed Rob's answer, and it seems like we're on the same page here.) The question is how much decoupling you should do, and YAGNI is a good principle to help determine the answer. (For those who speak of unit testing -- if you need to decouple to do your unit test, then YAGNI obviously doesn't apply.)

I really sincerely doubt the people who say they "always" decouple. Maybe they always do every time they think of it. But I have never seen a program where additional layers of abstraction couldn't be added somewhere, and I sincerely doubt there is a non-trivial example of such a program out there. Everyone draws a line somewhere.

From my experience, I've deoupled code and then never taken advantage of the additional flexibility about as often as I've left code coupled and then had to go back and change it later. I'm not sure if that means I'm well-balanced or equally broken in both directions.

Sol
+4  A: 

YAGNI is a rule of thumb (not a religion). Decoupling is more or less a technique (also not a religion). So they're not really related, nor do they contradict each other.

YAGNI is about pragmatism. Assume you don't need something, until you do.

Usually assuming YAGNI results in decoupling. If you don't apply that knife at all, you end up assuming that you need to have classes that know all about each other's behavior before you have demonstrated that to be true.

"Decouple" (or "loosely couple") is a verb, so it requires work. YAGNI is a presumption, for which you adjust when you find that it's no longer true.

JasonTrue