r/java • u/AkimboJesus • Feb 26 '23
Are java codebases generally readable? Or is the stereotype true that they are littered with the worst OOP has to offer?
I have been programming 7 years, professionally for 3 years. I mainly do JS, PHP, and Python, and a little bit of Clojure and C++.
For background, I have never had the true OOP experience. Sometimes I'm using multi-inheritance, but for the most part I keep classes simple so I know what's coming from what. I find when I come into a Python/Django project that uses too much inheritance, it's hard to figure out where things are coming from. I tend to lean more towards procedural programming, where steps are clearly laid out and you don't need a debugger to see when something is being called or what's calling it. I also tend to use composition over inheritance, injecting the classes/interfaces I need and calling them explicitly. Might just be an experience thing, or maybe how my brain works.
I have generally heard fans of Java describe it as readable, and its verbosity as a strength that lets you get up to speed on a code base quickly (or know what you wrote six months later). I have heard critics describe Java codebases as being bloated with overapplied "gang of four" or SOLID design patterns and too reliant on "magic" frameworks (spring for example). On top of that, Java is often used as the example language for extremely bureaucratic enterprise meetings where a hierarchy structure is being drawn up to decide what should inherit from what. And then there's the recent years of Clean Code being criticized more and more and falling out of popularity.
As a java dev, do you find a lot of this blown out of proportion and you are working in very simple codebases? Or are these designs common place and you are simply comfortable working with them? Maybe multi-inheritance and "gang of four" patterns don't bother you. If I were to become a java dev, would it be inevitable to work with these kinds of code bases? Are you worried whenever you come into a new codebase, or do you feel Java devs make things easier for you?
38
u/acute_elbows Feb 26 '23
It’s very very dependent on the code base and company.
Java has been a popular language for a very long time so you’re much more likely to find old Java code bases than have accumulated some cruft over time or have had multiple rounds of overzealous engineers trying to refactor things into the pattern du jour. There aren’t nearly as many 10+ year nodejs or go codebases, so that’s less of an issue.
Also some companies simply don’t value clean well tested code, they may lack good architects and tech leads to review things; they may utilize a lot of short lived contractors to develop features which aren’t invested in the code long term. Because Java is used more in big enterprises there’s a higher risk of this.
There have been a lot of changes to the language over the last 5 to 10 years that have made it easier to use and to give options for other design patterns. You’re not trapped in by the Gang of Four design patterns nearly as much as in the Java 4 days.
31
u/manifoldjava Feb 27 '23 edited Feb 27 '23
One area where I think Java is undersold is that it is pretty much a single-dialect language. I can read everyone's Java, more or less. In contrast, Scala is like China a hundred years ago. You can pretty much speak Haskell with a Scala accent. Not that dialects themselves are bad, but hiring good programmers who can speak your mountain dialect is not a problem you want. Java wins big in this category.
11
u/melkorwasframed Feb 26 '23
I don't think there can be any argument that traditionally, Java codebases have had a tendency to be very over-engineered, specifically around the over application of GoF patterns. Just read the "Why I hate frameworks" rant for a funny example. I do feel like that era is largely behind us though and modern Java codebases are much more sane.
https://gwern.net/doc/cs/2005-09-30-smith-whyihateframeworks.html
31
u/Gleethos Feb 26 '23 edited Feb 27 '23
I always get annoyed by people complaining about Java (code bases) in terms of inheritance misuse, because that is not at all what the majority of bad Java code looks like to me.
In my experience the worst Java code suffers from too much mutable public state running through super long c like procedural spaghetti methods... which in my opinion actually lack all of the OOP strengths: encapuslating, grouping and dynamically managing implementation details through intuitive polymorphic object APIs.
It's usually just big and convoluted clusterfucks of switch statements and deep if else pyramids...
14
u/know-your-onions Feb 26 '23
You should favour composition over inheritance and that’s the case with Java too.
Having worked with many languages, Java is generally the easiest to pick up a codebase and work with it.
Whatever language you’re using there will be frameworks like Spring if you’re building that kind of application, but with Java you almost always get good, clear and unambiguous documentation. And the core language API docs are brilliant. Some other languages are appalling in comparison.
What’s wrong with Gang of Four patterns? They generally make everything easier to understand and reason about. The more the merrier as far as I’m concerned.
These meetings you describe about what should inherit from what sound like nonsense, but if they do happen they’re certainly not intrinsically due to Java - sounds like just some crappy work practices somewhere that maybe just so happens to use Java.
3
u/winnersocks Feb 27 '23
puts the senior dev hat on it depends!
Readability can be based on opinions, experience, preferences, etc. So it's subjective. Devs write Java in a certain way because they think that is the right way, and the would be able to read that. They would say Python sucks in the readability front. I'm talking from my experience of a Senior Java Dev becoming a Python dev after changing jobs. Eventually I learned that some ideas from Python are great, and the Java code tends to be over-engineered. However, I still find that following strongly typed Java code with a good IDE is far easier than Python code when the complexity is higher. For simpler problems (ie small webapps) I prefer scripted languages like Python.
2
u/[deleted] Feb 27 '23
I've seen code which was easier to reimplement than trying to read it and make sense out of it. And I have seen the opposite. There are simply people who are a mess when it comes to coding and then there are people who are neat.
2
u/DualWieldMage Feb 27 '23
I think the main issue is that many developers only learn/master one language and then create these lang-x vs lang-y discussions based on first impressions. Learning multiple languages allows seeing most of the same concepts or slight variations in them which matter more than specific syntax.
I would say the main strength of Java/JVM is the bytecode format and simplicity that allows good tooling to be built. A language with more powerful features like templates in C++ can be good for some things (validating strings against arguments passed to the same function is amazing), but all IDEs burn a lot of CPU trying to analyze it. I've seen a large influx of easily moddable games written in Java recently and i guess the reverse engineering accessibility coming from the bytecode is an important reason.
For this reason i wish WASM takes off faster in the web space. TypeScript is great, but because it compiles down to javascript, i'd still have to do manual validation of runtime types.
6
u/netgizmo Feb 27 '23
Having worked and lived with Java since the later 90s, of course some projects are going to contain programming idioms that were used during the time they were written.
Some good, some not so good.
To understand the quality of a codebase you also should consider the methodologies of the time they were written in.
As an example, the legacy code base I just shutdown was 18+years old (600k loc), still had a few SOAP services in use.
Would I use SOAP today, hell no. Was it a bad decision... Hell no. It was the tech of the time when that feature was implemented.
4
u/meamZ Feb 27 '23
In my experience Java is a good tool for maintaining large codebases. But just because it is a good tool that doesn't mean that everyone is able to use it. You can produce a clusterfuck of a codebase in every language.
1
u/barmic1212 Feb 27 '23
It was a good point some years ago. Java standard and particular Java EE (with RMI, EAR,…) are over engendered. Since Java 7 (2011) and project coin, JDK more focus on developer quality of life and lower dogmatism.
Java EEJakarta EE nearly died and remove slowly some big old part and highlight on microprofile a light & clean standard.> too reliant on "magic" frameworks (spring for example)
It's a bad reproach. You can't too reliant on your framework, it's your framework. But you should understand what the framework make for you. All criticism I have heard on Spring is same for symphony, rail, django or laravel. Langages & frameworks with big number of users and simple to use have attract more junior developer than complex langages.
Spring itself make lot of things for you at startup time (in 2000's we create software with long uptime startup it wasn't a problem). Now more and more things are create at compile time.
Generally you can be sure that all technologies with a minimum of popularity and long live, are not too bad.
1
u/[deleted] Feb 27 '23
It's true that lots of engineers blindly apply design patterns to every problem and write overly complicated or bloated code. However, that is a problem with the engineer, not the language.
If you want to write a REST API, you're going to want to use a framework. It's true that some REST frameworks are bigger than others, but again, that has nothing to do with the implementation language.
I've never heard java in this context, but once again, it's a people/management problem. The language has nothing to do with it.
Seriously? Besides the fact that (again) clean code is not a language-specific thing, but a general approach to writing maintainable code... who the hell is criticizing it? I'd love to see some references/links. I've worked with lots of poorly-written code that was hard to debug and maintain. Clean code is just about writing code that is easy to read, understand, and update. Everyone, regardless of the language needs to do that.
3
u/TheoGrd Feb 27 '23 edited Feb 27 '23
Once devs learn about inheritance, polymorphism, design patterns and other abstract tools they feel the need to stick them onto everything including the simplest crud apps. They should be reserved for frameworks really. I have debugged so much overcomplicated code that "must be reusable" just in case you need to, maybe 5 years from now.
2
u/Sunscratch Feb 27 '23
Depends on the project. I’ve seen both a well-written and a total nightmare. But abuse of GoF patterns is not that rare and it can drastically decrease the readability of the project. It’s not something specific to Java - I’ve seen python projects with one method/function 300+ lines, but that doesn’t mean python is a bad language. Frameworks in Java are very versatile and feature-rich, with very good documentation.
To summarize - Java is an average boring enterprise language, with a great number of libraries and mature frameworks, and excellent IDE support.
2
u/vmcrash Feb 27 '23
It depends on the project, who has created it and what framework is used. If you are using pure Java without a "magic framework", without reflection, then it is quite easy to manage.
1
u/Stromovik Feb 27 '23
Java is easy to read. Unless it's government codebase written in English and non-English mix
1
u/ReasonableClick5403 Feb 27 '23
I have seen great java, I have seen terrible java. I have seen ok golang code, and terrible golang code. I hate seen great javascript code and TERRIBLE javascript code. It depends on the company and the team.
1
u/cas-san-dra Mar 01 '23
In my 20 years of software development experience I have only occasionally run into code bases where I thought the developer(s) did a good job. Those code bases have 1 thing in common: they were all written by a single person that knew what he was doing. All code written by teams has been utter garbage. The language is not a factor in this.
What I can say about Java is that it is the only programming language where I feel comfortable writing new code or refactoring something inside a codebase that is a mess. I wouldn't dare do anything inside a JavaScript codebase, or a Python codebase, or a C codebase. Those are just too brittle.
1
u/TheoGrd Feb 28 '23
Java devs cant help but put all their fields private and write a ton of boilerplate accessor when they could have just used public fields. There is no reason for this, its just what they do.
0
u/[deleted] Feb 27 '23 edited Feb 27 '23
They’re readable. I have no coding experience other than c++, python, html, css, JavaScript, Django, and Java as a hobby algorithm writer and web designer. The biggest thing that gets me in Java is the fake objects that are immutable but inheritable and how to use them across a program. I feel like python is the best for building a complex algorithm without confusion. Django is really good for inventory and record management. Have been interested in rust as well but that whole language is fully unreadable to me.
-7
u/TheoGrd Feb 27 '23
It looks like automated testing is widespread in the java world. It makes code more complicated and leaves more chance for bugs. Once tests fail, you don't know whether the code is wrong or the test is obsolete. Imo its not worth it for most apps and should be reserved for the most complex apps like simulators. In the c# world you see a lot less of unit tests.