views:

285

answers:

6
+17  Q: 

SOLID vs. YAGNI

One of the most frequent agruments I hear for not adhering to the SOLID principles in the class design is YAGNI (allthough the arguer often doesn't call it that):

"It is OK that I put both feature X and feature Y into the same class. It is so simple why bother adding a new class (i.e. complexity)."

"Yes, I can put all my business logic directly into the GUI code it is much easier and quicker. This will always be the only GUI and it is highly unlikely that signifcant new requirements will ever come in."

"If in the unlikely case of new requirements my code gets too cluttered I still can refactor for the new requirement. So your 'What if you later need to ...' argument doesn't count."

What would be your most convincing arguments against such practise? How can I really show that this is an expensive practise. Especially to somebody that doesn't have a too much experience in software development.

+1  A: 

The correct application of these principles is often not very obvious and depends very much on experience. Which is hard to obtain if you didn't do it yourself. Every programmer should have had experiences of the consequences of doing it wrong, but of course it always should be "not my" project.

Explain to them what the problem is, if they don't listen and you're not in a position to make them listen, let them do the mistakes. If you're too often the one having to fix the problem, you should polish your resume.

knitti
"polish your resume"? You mean, get a better job?
bitbonk
yes, when the hassle is too much it is best to move on. The _are_ after all companies who value this type of thinking
knitti
+1  A: 

In my experience, it's always a judgment call. Yes, you should not worry about every little detail of your implementation, and sometimes sticking a method into an existing class is an acceptable, though ugly solution.

It's true that you can refactor later. The important point is to actually do the refactoring. So I believe the real problem is not the occasional design compromise, but putting off refactoring once it becomes clear there's a problem. Actually going through with it is the hard part (just like with many things in life... ).

As to your individual points:

It is OK that I put both feature X and feature Y into the same class. It is so simple why bother adding a new class (i.e. complexity).

I would point out that having everything in one class is more complex (because the relationship between the methods is more intimate, and harder to understand). Having many small classes is not complex. If you feel the list is getting to long, just organize them into packages, and you'll be fine :-). Personally, I have found that just splitting a class into two or three classes can help a lot with readability, without any further change.

Don't be afraid of small classes, they don't bite ;-).

Yes, I can put all my business logic directly into the GUI code it is much easier and quicker. This will always be the only GUI and it is highly unlikely that signifcant new requirements will ever come in.

If someone can say "it is highly unlikely that signifcant new requirements will ever come in." with a straight face, I believe that person really, really needs a reality check. Be blunt, but gentle...

If in the unlikely case of new requirements my code gets too cluttered I still can refactor for the new requirement. So your 'What if you later need to ...' argument doesn't count

That has some merit, but only if they actually do refactor later. So accept it, and hold them to their promise :-).

sleske
Have the right number of classes of the right size. Make them too small and you end up putting the complexity into the interconnect between the classes, and that *doesn't* help.
Donal Fellows
@Donal Fellows: Yes, that is true. My rule of thumb is to put at least one bit of non-trivial functionality into each class - that works reasonably well. But to be honest, I have never seen a real-world system with too many small classes, but many with large classes, so most people seem to gravitate to the "big class" side anyway.
sleske
@sleske: I've seen both (some well known frameworks are really bad that way). I just try to remember that a problem has a certain *natural* complexity, and I try to make sure that the solution has that level of complexity made manifest and as little extra as practical.
Donal Fellows
+3  A: 

It sounds like you're arguing with a brick wall. I'm a big fan of YAGNI, but at the same time, I also expect that my code will always be used in at least two places: the application, and the tests. That's why things like business logic in UI code don't work; you can't test business logic separate of UI code in that circumstance.

However, from the responses you're describing, it sounds like the person is simply uninterested in doing better work. At that point, no principle is going to help them; they only want to do the minimum possible. I'd go so far as to say that it's not YAGNI driving their actions, but rather laziness, and you alone aren't going to beat laziness (almost nothing can, except a threatening manager or the loss of a job).

EricBoersma
I have the feeling that those persons want to get the features/requirements implemented fast test the main use cases and then forget about it. Then fix the issues when the code is deployed. The customer is a bit the acceptance tester.
bitbonk
@bitbonk: Yes, a very common scenario. It can be a legitimate way to work, provided you inform the customers that they will be the beta testers (and they agree). However, not everyone does that :-).
sleske
+9  A: 
Roger Pate
+2  A: 

SOLID principles allow software to adapt to change - in both requirements and techical changes (new components, etc), two of your arguments are for unchanging requirements:

  • "it is highly unlikely that signifcant new requirements will ever come in."
  • "If in the unlikely case of new requirements"

Could this really be true?

There is no substitute for experience when it comes to the various expenses of development. For many practitioners I think doing things in the lousy, difficult to maintain way has never resulted in problems for them (hey! job security). Over the long term of a product I think these expenses become clear, but doing something about them ahead of time is someone else's job.

There are some other great answers here.

Alex Lo
A: 

Understandable, flexible and capable of fixes and improvements are always things that you are going to need. Indeed, YAGNI assumes that you can come back and add new features when they prove necessary with relative ease, because nobody is going to do something crazy like bunging irrelevant functionality in a class (YAGNI in that class!) or pushing business logic to UI logic.

There can be times when what seems crazy now was reasonable in the past - sometimes the boundary lines of UI vs business or between different sets of responsibilities that should be in a different class aren't that clear, or even move. There can be times when 3hours of work is absolutely necessary in 2hours time. There are times when people just don't make the right call. For those reasons occasional breaks in this regard will happen, but they are going to get in the way of using the YAGNI principle, not be a cause of it.

Jon Hanna