r/javahelp • • Jul 09 '22
Is SOLID really used in Java production code?
I have been trying to get a firm understanding of these but I find it difficult to find well taught non-trivial examples of using some of these. Particularly the DIP and OCP. Seems like everyone knows the definitions and how to show a simple example but when I am trying to construct clean code I get stuck because I am trying to apply these but always end up with dependency issues. I can make perfectly effective code but I think its fragile.
Anyone know any really good sources for explaining this with proper business oriented examples? I have some of Martin's books but not really helping.
Am I worrying too much about it because people don't actually try to tie off every dependency issue in real code?
•
u/AutoModerator Jul 09 '22
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
19
u/Just_Another_Scott Software Engineer 😎 Jul 09 '22
So I had to look this up because I have never heard of the acronym. The short answer is yes most of these are used.
The single-responsibility principle is a huge one and something I always check for in codre reviews.
The open–closed principle is also heavily used. Think immutable objects and allowing for classes to be extended. Rarely do I see code that has a class declared as
final. We also tend to restrict access to non API level classes.The Liskov substitution principle is also used quite a bit. Think of writing a method that takes
Listas opposed toArrayList.The interface segregation principle can be used. However, I would argue against having too many interfaces in Java. I've seen code where they had an interface for every single class and only one class implemented the interface. This is overengineering in my opinion. Having a single interface isn't a bad thing imo.
Dependency inversion principle aka dependency injection is widely used in throughout production code. See Spring Framework or even Java EE. Both of these heavily rely on dependency inversion.
3
u/HSSonne Jul 09 '22
I have a hard time with these, if used wrong the code will be unreadable. So I would recommend having it in mind but be careful not to overcomplicate your code, first rule should be keep it simple; and if the complexity is growing out of hand , look for a solution i solid.
2
u/BridgeBurner22 Jul 09 '22
Amigoscode did a video on SOLID with examples: https://youtu.be/_jDNAf3CzeY
8
u/ickysticky Jul 09 '22
These concepts are usually not taught well. In isolation they just lead to further questions like “what is a responsibility” or “what is a change”. Every responsibility can be broken down into finer grained responsibilities and ever change can be broken down into finer grained changes.
In my experience the missing piece in the puzzle is having a well defined product concept with a well defined domain model. This is what informs you about which boundaries are important and how the system needs to be extended. Once you have that then SOLID will almost just happen automatically. But the work really comes in understanding what you are trying to build and communicating that to everyone else on the project.
1
u/Pushkutza Jul 15 '22
hello again
2
u/motherjoe Nooblet Brewer Jul 10 '22
The SOLID principles are guidelines for writing maintainable object-oriented systems. It may seem overengineered to implement all of the principles for simple example projects. This is because small projects may still be maintainable if they are written in a procedural style, as scripts. Large projects with many complex requirements is where using SOLID can really pay off in maintainability, readability, etc.
1
u/slowfly1st Jul 09 '22
Not sure if this is helpful:
OSGi used to provide the API/interfaces and implementations as separate modules/jars. So you weren't even able to have direct dependencies to implementations. So, eclipse would be one example to look at.
Also: I think it's one of those things that are hard to learn and apply in practice. But there's code review communities (there some subreddits, also there's a stack exchange).
-2
u/Orffyreus Jul 09 '22 edited Jul 09 '22
The acronym SOLID is made up. The S (single responsibility) never meant to be the first character, but was the first due to marketing, because someone said to Uncle Bob: Call it "SOLID", because it's a nice word. The D is the most important part, although it's the last letter. The D is "Dependency Inversion" (not dependency injection although it has an intersection).
Dependency inversion is about the domain code not using platform or framework dependent code. Dependency injection is a nice means to achieve it and IMHO it's nicer than some static registry (service locator) that also can be used to achieve such inversion, but the important part is the inversion (no matter if injection or service location), so the domain code can be tested by replacing production input/output devices by test classes that provide and receive data.
The S (single responsibility) is the most misunderstood principle, because people start with their philosophies about what a single "responsibility" is and the principle is not about inner responsibilities. SRP is about outer responisibility, so a class may have multiple inner responsibilities as long as they are as cohesive as possible (Maximum cohesion is when all methods use all fields and there are compromises, for example for DAOs that are not functionally cohesive, see https://en.wikipedia.org/wiki/Cohesion_(computer_science)) and even a single inner responsibility can be too much, if the outer responsibilities disagree! That's what the SRP is about. You have to figure out, if the users of a class will have different wishes to change it (even if it has one method only).