SOLID is that all they say?

Related: What are SOLID principles?

I ordered my "Principles, Standards and agile practices in C#" from Robert C. Martin and one of the reasons is SOLID. But I'm reticent about SOLID.

I don't even know all the principles. I know SOLI, missing D. The thing is, I see the principles and I think, " Okay, so what?". Some of them seem to me that do not come with parameters saying where they should be applied, or how. They are kind of like design standards: you first find a situation in which they fit, and then apply in case they fall well into that situation.

Moreover, they seem to me just a sparse set of demands that are individually well-founded, but together have no cohesion at all. Why Will my Code Get solid™ if I apply them?

Those who defend them put them on a very high level. But I don't know if it's all that. There are those who criticize (if it is to read only one of those that are listed below, read the first).

Criticism, who generally agreed: Why I don't teach SOLID

Criticisms and defenses: SOLID is OOP for dummies

Other criticism: Not-so-SOLID OOP principles

Criticism criticized: Re: Why every element of SOLID is wrong

Defense against some criticism: In defense of the SOLID principles

I would like as impartial an assessment as possible of the applicability of these principles, and whether they bring all the benefits that boasts of them.

Author: Piovezan, 2018-10-14

3 answers

The first mistake people make is to think that Uncle Bob is the creator of SOLID. This acronym was created by Michael Feathers. And the concepts individually were created by other people, although Robert Martin gave his version of how each of the 5 items should be interpreted. So SOLID is one of those things that everyone understands in a way, and therefore no one knows for sure what it is, and it does not serve as a good parameter, but are good tips to think and discuss the subject.

And they are kind of purposefully vague. This is one of the criticisms that is made at the beginning. His problem is that if the person understands everything he is, he does not need it, and if the person only uses it as a basis for what he needs to accomplish the person gets "bitolada" and makes mistakes by following "magic formula".

Solid's ideas are good, but you always have to understand the whole context of it. Understand it in depth to know when and how to apply. Exaggeration in its use is a problem, like almost everything we do.

In general his problem is to put a lot of hanging, a lot of mechanism in what should be the domain. And he does not always produce the wonder that is expected of him, even when done right. It will still need a lot of refactoring. If it's to refactor, just put stuff on it when you need it.

Plug put into the socket through gambiarra halfway

Another related problem is that it mixes levels of abstraction, you find in the same code things that express your domain and things that help your code "work better".

What you should understand is that certain relationships between objects are difficult to undo and this is what matters throughout SOLID. Do not create dependencies where a refactoring will be too complicated. The secret is to eliminate dependencies, it is to allow composition. What matters is cohesion, a very old concept.

What software are we talking about?

This is something that is always left out. We are talking about a game? Something shipped? From an internal application? From a commercial product? From a public library or framework? Or something that has no importance or long life span?

Relation to OOP

Some say it helps OOP. There are those who say that it goes against, or at least that it is not about OOP. It doesn't seem to me that it's about OOP, everyone talks about one thing and overall talks about modularization, or even doing the opposite of what OOP preaches.

Principles important

I like other principles with priority, two of them are YAGNI and KISS that preach that you should do everything simple and only what you need. Of course with some care not to overdo it. In fact KISS says to do simple, but not simplistic, and YAGNI is not saying to leave the application completely naked.

Interestingly, the people who created and champion SOLID also strongly advocate legacy code, continuous improvement, and refactoring. Because well, not using SOLID until it is necessary meets the above two principles and it can be applied when needed in legacy code by doing refactoring. But only when the gain pays off.

Another principle that I like a lot, what I like the most, is the DRY. For the experience I have had in more than 35 years developing software, and in general working years on the same software, what makes maintenance easier is DRY. I get to exaggerate in the use of it, but it does not bring me big problem. Having a canonical system information is critical. When you start spreading the information you will lose control. And if you need to change something that is spread you will have problems, in addition to being much more confused to understand what is happening. Of course, the wrong use trying to reduce a circumstantial repetition can cause problems, so I always say that you have to know how to model with mastery, understand the problem in depth.

I find it unfortunate that they are creating a wave against DRY. DDD is very not DRY (in parts), SRP says it should have a reason to change one class, but not that a change of one class should not change others, and not having to change other places is more important.

Moreover, they seem to me only a sparse set of demands that are individually well-founded, but together have no cohesion at all. Why Will My Code Get solid if I apply them?

I agree. But I think they just threw together to sell the idea more easily. Forget that the software will be more solid. Understand that this in itself is just marketing. Stick to the individual principles themselves and understand them. Or to the original principles that are more important and that SOLID was created to market them better.

People like to buy these ideas, and the more it is repeated the more people want to follow it. It creates a virtuous circle for those who understand what it does and a vicious one for those who you don't understand. When people start doing it just because they read it is something they have to do it is wrong by definition, even if it works out by coincidence.

Dependency Inversion

Just what you haven't learned yet, which is usually implemented with that of DI (I already talked about it ), it is one of the things that most critical. Because most of the time it is created solely to facilitate testing. They don't even talk about what. And it is used ideologically. Anything let it be putting in the code without a need for it in itself I am against. If it is used because the software will look better is ok. If it is used to give flexibility and this is optional also ok. If it's just for testing has other ways to perform it. It is usually very simple to add this later.

Plastic seat placed in place of a car seat in a car

Critical to each point

  • SRP

    It's cool, but it's confusing. At first people understood that it was the DRY, today they say that it is another thing and it worries me. It's just about modularizing.

    One of his problems is that he can encourage creating too many classes, too many functions, making the code too complex, having to create linking elements, facilitators, or else very verbose codes to express what he needs because of the existence of so many components. And having so much it becomes easier to tinker with them individually, but it becomes very complicated to maintain and use.

    It is very difficult to define well what is sole responsibility. Almost everyone has to create a Cliente that derives from PessoaJuridica, and the moment it does this Cliente happens to have at least two responsibilities, but "no one" sees it. This is the fault of the canonical example ezrúxulo they use to teach OOP.

    In many cases a change in one class requires the change in another in an Associated way to function properly and in a way you don't know automatically. Refactoring to add something from solid late it is usually required in languages with a good type system, otherwise it does not compile. In other languages it can cause problems at runtime, unless the person creates a typing Control System in the code, which indicates that they have chosen the wrong language to do that.

    To tell the truth we don't have adequate tools to deal with it, even if it's desirable. And no one creates.

  • OCP

    As a general idea is good, but in practice it creates complexity because everything requires making new versions. It is common to violate DRY because of this (ironically it should be the opposite). One way to avoid this is to separate responsibilities well, but when you do it the right way you start running away from OOP.

    In general it preaches the abuse of inheritance, even in the right way, but still use inheritance where perhaps you can do in a more flexible and easier way with composition.

    When everything is well planned and a new type it is created inherited from some kind that was thought to be base of something else, it can work. When you take something from one domain and inherit it in another domain to make it more flexible, you have something wrong, so it's best to let the object deal with the differences.

    One of the criticisms I make is that she sees inheritance in a basic way. Today some languages have more sophisticated mechanisms that allow something that is in the middle ground.

  • LSP

    The idea is very good, mainly because she commands to avoid inheriting in a bungled way, without much sense. So reuse is avoided by Reuse, you need to have a reason to inherit. At the bottom she preaches the composition.

  • ISP

    In general it's good, but it's more about contracts. The bad part is when they exaggerate just to follow this. Since you can refactor it is better not to segregate so much and segregate afterwards when necessary. Don't get it wrong, it's good to segregate, but watch out for do not overdo it. The worst is when the person creates an interface just by creating, without it being actually used. This is a mistake that only sins those who are very fanatical for SOLID and do not fully understand it, but among developers who are not beginners it is common to fall into this trap.

  • DIP

    I mentioned above.

Conclusion

I like the idea of SOLID as hints of things you should watch out for when you are developing a software and points starting to study more on the subject.

None of these things guarantee quality in software. Quality is given when the person knows what he is doing and is dedicated to doing it correctly.

If Uncle Bob does that and it works out for him, great. But are you sure about this? Does it not end up doing the same job that you would do by not following SOLID when you have to give maintenance? Does he no longer have ease just because he knows how to do the right thing regardless of follow SOLID or not? We have no answers to this.

Who has proven that SOLID is good? Should we just believe he is? Is it religion?

SOLID is good, its irresponsible use is not. The benefits exist in specific cases when used correctly. Don't be alone in SOLID and don't demand to use it all.

To be fair it has much worse things, SOLID is not even that bad, but to be fairer it has better principles and guides that meet the same goal. What if it is applied with a less object-oriented ideal maybe it works better. Perhaps his biggest problem is the examples used and solutions offered.

In the general calculation the important thing is to know the pros and cons of it, it is silly to say that it is bad or good without a context.

I need to make time to substantiate this answer a little more. I have the basis of the problem, but I still need more research so as not to leave much in the air. In essence I put in Portuguese with my words the which is in all the links of the question, and some more knowledge.

 13
Author: Maniero, 2020-08-11 14:57:09

SOLID

SOLID principles serve as metrics in object orientation. Applying SOLID is not like a design pattern, as its concepts are not formulas, but ideas that can be applied in code. To apply the principles there are no strict rules.

Solid is much more a reference to be able to identify a quality code, serving as a basis to know how to create a code that is more favorable to maintenance and changes. Apply these concepts is not an obligation, everything should be done with thoughtfulness, since no rule should be followed blindly.

In short, knowing these principles helps a lot to understand object orientation, but it is always important to have critical sense, because something that is used or defended without weighting loses sense. I find solid essential, but it's not a magic formula for developing quality code.

 4
Author: raphael, 2018-11-15 00:29:25

On the principles in programming

SOLID contains 5 Principles of software design , but they are not all existing principles. There are several:

  • SOC Separation of Concerns;
  • DRY Don't Repeat Yourself;
  • YAGNI You Ain't Gonna Need it;
  • KISS Keep It Simple, Stupid;
  • REP the Release Reuse Equivalency Principle;
  • CCP The Common Closure Principle;
  • CRP The Common Reuse Principle;
  • ADP the Acyclic Dependencies Principle;
  • SDP The Stable Dependencies Principle;
  • SAP The Stable Abstractions Principle;
  • among others.

The principles cited above are not SOLID, but are also principles of software design . They are as important as SOLID so that the designer can know what is positive and what it's negative.

I'm not saying that some are better than others, or imposing a hierarchy. I affirm, however, that all are important to form a designer's knowledge. Knowing which practices can bring benefits and which can bring disastrous headaches as software evolves is the quest of every good designer. The principles guide this knowledge.

Since the principles arose because of a need to deliver quality software and in less time as possible. At a time when the big names in Computer Science argued that "software should be easy to maintain"!

Understanding that: "less time possible" does not mean delivering running. Many use the word Agile, stating that software should be done fast, but in the wrong sense. The meaning of" agility " is to deliver quality software. Quality gives less work to change, less work to fix. This results in less cost and less time to maintain and evolve the software.

As Maniero said: "I like other principles with priority, two of them are YAGNI and KISS that preach that you should do everything simple and only what you need. Of course with some care not to overdo it. In fact KISS says to do simple, but not simplistic, and YAGNI is not saying to leave the application completely naked". principles help to do well done, but one must learn to avoid exaggeration.

The experimentation and analysis that gave rise to the principles were not done by heated programmers, but by people who knew what they were talking about, people from the academic (MIT, Oxford, etc.) and business of great importance (IBM, for example). This experimentation took place over an extensive period (of more than 30 years) of research, so it is difficult for our immediate generation to understand them. It is not enough to give Control + C and Control + V.

Many of these principles they arose in a scientific environment, in large universities. The LSP of solid (Liskov Substitution Principle), for example, was made by Barbara Liskov of MIT in one of her publications.

When we see articles and sharp criticism in blogs about "why use or not use principles," we realize how far people are from the real purpose of these precious tools for software designers. The principles exist to provide:

  • ease of maintenance;
  • code reuse in other projects;
  • deliver evolutions and fixes as soon as possible.

See the Manifesto for agile software development .

Another important thing: the goal is not to follow the scratch all principles to iron and fire at all times!! If so, the software becomes very difficult to do, especially for less experienced programmers. Requirements and rules compliance at all costs is not the goal!! the importance of principles is related to knowledge , allowing the designer to know what is proven to work and what does not work in various situations so that he decides, for himself, the best way to make a software that suits the reality of his company.

About SOLID

In fact, Robert Martin did not" create " the SOLID principles, but catalogued several principles that, according to his experience, would be the more important for synthesizing all the others (see Design Principles and Design Patterns ). In other words, by understanding and fulfilling these principles, one would be fulfilling, consequently, the others existing in the literature.

From this list, separated 5. In the book Clean Architecture, Martin explains that around 2004 his friend Michael Feathers:

"he sent you an email saying that if you reorganized the principles of your catalog, the first letters would spell the word SOLID in Portuguese) " .

It was at this point that the acronym was born.

It took Robert Martin 20 years to formulate the catalog, based on tests and experience of him and his companions in various programming languages. It was not an assumption, it was joint or referenced experimentation of the works of various names in software engineering and computer science: Barbara Liskov, Bertrand Meyer, David L. Parnas, Dave Thomas, Edsger Dijkstra, Kent Beck, Larry L. Constantine, Alistair Cockburn, Martin Fowler, Andrew Hunt, Michael Feathers and several others. Just search for some of these names on the internet.

For the sake of truth, little is Robert Martin's, most of the principles he catalogued come from the literature of some of these names. Below are some of the references Martin used to synthesize and name the 5 Principles.:

  • SRP - principle of Sole responsibility: David L. Parnas and Edsger Dijkstra (author of SOC Separation of Concerns);

  • OCP-closed Open Principle: Bertrand Meyer and Larry L. Constantine (one of the first to talk about coupling and cohesion);

  • LSP-Liskov Substitution Principle: Barbara Liskov;

  • ISP-principle of Interface Segregation: based on design pattern Template Method, from Gang of four (Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides);

  • DIP - principle of inversion of dependencies: based on the work of Grady Booch, Meilir Page-Jones and Barbara Liskov;

In one of his old lines, Martin states that principles are important for senior designers and aspirants to this degree of knowledge, with long-standing experience in the area of software design. It further states that the new programmers, with little experience could not understand the real meaning of the principles and misrepresent them, even with good intention, by trying to summarize them to pass on to their colleagues.

That explains a lot! In most places where we talk about SOLID (blogs, websites and Literatures), it is so summarized that it can not be understood. SOLID is not simple but it is simple to practice. The transmission of anemic information is what made the solid journey difficult for us Brazilians to understand fact.

Hint: look for Robert Martin lectures on youtube, it has several old and New.

Or DIP (Dependecy Inversion Principle)

A great example of this confusion is the functioning of the principle of Dependency Inversion. It is very simple and states that if one class is to depend on another, this one must be more stable.

" the code that implements the high-level directive should not depend on the code that implements low-level details " (Robert Martin, Design Principles and Design Patterns ).

In other words, if Class a depends on Class B, B must be more stable than A, so that dependencies between classes always move towards stability, depending on modules more stable than itself.

If your program depends on a module where methods change with each version, for example, you will have always the job of updating all classes that use it. The more stable the dependencies are, the less changes, the more agility, the more profit and the less time wasted.

Simple and obvious, as solid software should be.

Conclusion

Is not a matter of defending or criticizing SOLID. It is a matter of defending knowledge, and criticizing the delivery of bad software.

It is very easy to make bad software, so it is important to know (understand from true) as more experienced people we have dealt with the same problems and solved them.

For an idea to become a Principle or a Standard, it must first be proven that many projects have made use of this idea and achieved the same results. This is possible only with time, with experience, with the spirit of research and reading from good authors.

Below, the references to the works used by Martin in the formulation of principles:

  • CONSTANTINE, Larry L. Structured design. IBM Systems Journal, VOL13, NO 2, 1974;
  • DEMARCO, Tom. Structured Analysis and System Specification , Press Computing Series, Yourdon, 1979;
  • DIJKSTRA, Edsger W. on the role of scientific thought. Burroughs Research Fellow, Netherlands, 1974;
  • GAMMA and all. Design Patterns: Elements of Reusable Object-Oriented Software, Addison Wesley, 1995;
  • BOOCH, Grady. Object Solutions, Addison Wesley, 1996;
  • JACOBSON, Ivar. Object Oriented Software Engineering a Use Case Driven Approach, Addison Wesley, 1992;
  • LISKOV and WING. a behavior notion of subtyping . Carnegie Mellom University: Pennsylvania, USA, 1994. Available in https://www.cs.cmu.edu/ ~ wing/publications / LiskovWing94. pdf . accessed on 03/08/2019;
  • MEYER, Bertrand. Object-Oriented Software Construction . Prentice Hall, 1988;
  • page-JONES, Meilir. the Practical Guide to Structured Systems Design, 2D. ed., Yourdon Press Computing Series, 1988;
  • Parnas, David L. on the Criteria to Be Used in Decomposing Systems into Modules. Carnegie-Mellon University, 1971.
 4
Author: Ricardo Pereira Dias, 2019-10-02 13:36:26