11 ms·
The problems with Java can't be fixed by adding new things, you can't undo decades of ecosystem development, training, and ideology built on top of the idea tha
by insanitybit 3y ago
The problems with Java can't be fixed by adding new things, you can't undo decades of ecosystem development, training, and ideology built on top of the idea that inheritance is really good idea and belongs everywhere.
edit: I will say that as a Java developer I am grateful every day for the improvements to the language. Java is a very impressive language and I have a lot of respect for the people working on it.
- matsemann 3y agoI rarely see inheritance used in practice in java code bases. Except where I would use a union type or sealed class in other languages anyways. I don't feel what youre describing is a real issue.
- xxs 3y agoInheritance used to be extremely common, look at AWT/Swing - however more or less it finished there, e.g. more than 20y back. There are still lots of folks who love 'protected' and deep hierarchies, of course. There is stuff like spring that uses way too many interfaces with a single implementation, doing something a bit off the regular road ends up implementing tons of the said interfaces anew. However the 'hate' part is mostly the internet (well esp. Hacker news)warrior topic
- sisve 3y agoGreat to hear. I used to program java in 2010 and inheritance was still beeing heavily used back then. But things change, if both the lang and the main community has change to focus more on simplicity its def worth looking at again. Coroutines and pattern matching is really good features
- insanitybit 3y agoContrast that, I've seen numerous Java codebases, young and old, and inheritance is very much one of the core ways that people program. I strongly suspect that in a few cases some Java devs using net new systems and avoiding common frameworks will perhaps be able to avoid lots of inheritance but I find it insane to say that that's common or even easy.
- School-Cotton 3y agoInheritance is heavily used in Java in 2023, at least in projects I’ve had to look at.
- sisve 3y agoSorry, what? Is not java toghether with Ruby maybe the most hardcore OOP enthusiasts that uses inheritance for "most stuff". Has the java culture move so far the last decade ?
- fiddlerwoaroof 3y agoI’ve been writing OOP code and reading about it for a decade or two, and inheritance was identified as problematic pretty quickly. The problem today, imo, is more underuse now.
- fuzztester 3y ago>inheritance was identified as problematic pretty quickly Exactly what I referred to here: https://news.ycombinator.com/item?id=37539443 https://news.ycombinator.com/item?id=37539443
- fiddlerwoaroof 3y agoYeah, it’s a bit like how the school version of lisp makes people think real lisp code bases are all lists and recursion.
- dragonwriter 3y agoEven in Ruby, IME, deep inheritance is fairly uncommon. Wide inheritance through mixins is common, though.
- EdwardDiego 3y agoEffective Java, written by Joshua Bloch, and first published in 2001, had a very influential section called Favor composition over inheritance: https://books.google.co.nz/books?id=Ra9QAAAAMAAJ&q=inheritance&source=gbs_word_cloud_r&cad=4 https://books.google.co.nz/books?id=Ra9QAAAAMAAJ&q=inheritan... Why do you think DI is so prevalent in Java codebases? It's a great way of simplifying composition.
- 3y ago
- insanitybit 3y ago> I rarely see inheritance used in practice in java code bases. That seems absurd to me and I have a hard time understanding it, honestly.
- wayfinder 3y agoIf you type everything with interfaces in your codebase, you are much less tied to inheritance. In fact, everyone could be written to be composed. However Java doesn’t support type union so you can get into some ugly and verbose situations but the JVM doesn’t really check type so this is more a compile-time issue and could be fixed in a future Java language revision.
- logicchains 3y ago>However Java doesn’t support type union so you can get into some ugly and verbose situations Isn't the "sealed interface" described in the OP blog post a type union? Or you mean anonymous unions?
- wayfinder 3y agoDefinitely referring to anonymous unions too. Without them, there’s still friction sometimes which makes union types unnatural. I haven’t written Java in a while and I can’t remember if you could sometimes fake a type union using a generic type on a method, but if you can, it’s definitely super ugly and would raise eyebrows during any code review.
- logicchains 3y agoAnonymous unions are a relatively rare feature; Rust and Haskell don't have them, for instance.
- Supermancho 3y ago> I rarely see inheritance used in practice in java code bases. Without some specific call-out, it can be assumed that this is a very niche viewpoint that has no bearing on modern development. The vast majority of projects use inheritance, today. Does it use Spring? extends SpringBootServletInitializer Meaningful responses using values from a request? extends OncePerRequestFilter Formatting exception handling responses on web requests? extends ResponseEntityExceptionHandler Then there's all the interfaces you have to satisfy, because it's all tied into the standard library and popular libraries.
- tail_exchange 3y agoYou must be really lucky then. In my previous job, inheritance and abstract classes were everywhere. Coupled with dependency injection frameworks that "worked like magic", it was really hard to follow the code inside that big, monolithic app. It made me never want to work with Java ever again.
- bb88 3y agoI've never been a big fan of dependency injection. It solves a problem with unit testing in Java, sure, but the reality is that Java could have done some nifty things to help alleviate that as well.
- jhaenchen 3y agoAgreed. Java injection frameworks are opaque and accomplish what they need to with overly powerful mechanisms because of the nature of the language. You don't see that sort of nonsense in python.
- kaba0 3y agoWtf, so what do you think Django is?
- bb88 3y agoI'm curious to understand what dependency injection is in Django that you're referring to? I know pytest does DI with fixtures, and trying to figure out what you're pulling into a test can be difficult.
- matsemann 3y agoIn django you're importing a default cache, a default storage etc, and write your code to the interface. In settings.py you wire it all up. It's basically the same, for testing you would have to mock the import or provide some implementation.
- rerdavies 3y agoHtmlElement. AST. Socket. Stream. Window. Closable. Runnable.
- fuzztester 3y agoYour Point? Long: Please make your Point Understandable.
- rerdavies 3y agoExamples of classes where inheritance is indispensable. I cannot imagine what kind of programming you are doing where that pattern never comes up.
- hutzlibu 3y agoThere is nothing wrong with inheritance. But sure, there is usually something wrong with deep inheritance or using it, where better solutions exist.
- insanitybit 3y agoNo, there's a lot wrong with inheritance. It leads to all sorts of issues and while theoretically one can keep the tree 1 level deep, in practice it's too tempting to expand the hierarchy. This is one of Java's big issues. The other is reference equality as a default, pretty horrible. Records help but records are also limited in when they can be used.
- hutzlibu 3y ago"in practice it's too tempting to expand the hierarchy." Not anymore, if you got stuck, by doing that once too often. But 2-3 levels can be allright as well. It depends, what you are doing amd with whom.
- belter 3y agoYou don't like code reuse?
- insanitybit 3y agoThat question seems blatantly disingenuous.
- akavi 3y agoLove it, and love using plain ol' functions for it
- dgb23 3y agoInheritance is not necessary for code reuse. All you really need for that is named procedures. A way to combine modules and different files certainly helps too. Type hierarchies can even hinder code reuse, because you can’t just pick the stuff you actually need.
- 3y ago
- lelanthran 3y ago> ideology built on top of the idea that inheritance is really good idea and belongs everywhere. Those are two different ideas.
- insanitybit 3y agoI used the word "and" to indicate that, yes.
- lelanthran 3y agoI think a comma would make it less ambiguous. Starting that single though with "The" indicates to me that you don't think those are two different ideas. Compare: the idea that inheritance is really good idea and belongs everywhere. With: the idea that inheritance is really good idea. and belongs everywhere.
- insanitybit 3y agoI think that would actually be an erroneous comma?
- wslh 3y agoPlease ELI5 what is wrong with inheritance and/or how Java have it wrong. Do we need to go back to a new object oriented undegraduate course? Genuinely asking.
- insanitybit 3y agoInheritance provides "is a" relationships between classes. At a time when people would spend months designing their software upfront, building big diagrams of classes, etc, this was not so bad. You'd have a very clean system design that maps directly to your class design. The problem is when things change. A simple example - you build a classification of all life and it is built upon the idea that everything "is a" plant or animal. And then one day it turns out there are fungi. This is not a fun situation to deal with and inheritance makes it a lot harder because the "is a" relationship is driving a ton of your logic. IDK I don't want to get to into it beyond that, many people have written quite a lot on the topic.
- wayfinder 3y agoThis problem is partly a type system limitation however. In a more flexible type system with union types and other magical features, your example problem would be less of an issue. However Java has an extremely limited type system so there is no middle ground between composition and inheritance. Once you choose one way, there is no “middle step” to migrate over.
- kaba0 3y agoUnion types are very rare (scala, typescript are the notable ones I can think of that implement it), most other languages only have sum types (which includes java as well, see sealed interface/classes), where you have to create each unique set of types you want to use separately, wrapping each option into a marker type basically.
- kaba0 3y ago> you build a classification of all life and it is built upon the idea that everything "is a" plant or animal With all due respect, this and similar examples are just plain wrong, and I really can't take anyone seriously when that example is used. The point of programming, and its abstractions is to help you complete a task, and make the implementation maintainable and easy to reason about. I think grabbing the "is a" part is fundamentally bad -- there is no point in creating a taxonomy in and of itself, this is no database for that data. Inheritance sometimes is the correct abstraction and while it is definitely overused, when it's correctly applied there isn't really another abstraction that would fit better. E.g. see a graphics library's Node class as the stereotypical correct application.
- kaba0 3y agoPrefer composition over inheritance has been a common mantra for at least a decade, if not more. Maybe you should undergo the last decade of development and training. There will always be shitty devs, and since java is one of the biggest languages, it definitely has more than some ultra niche academic language no one uses. I don't think it is the fault of the language though, or if that somehow were a reason to choose a different language. Should a decent BMW driver sell their car, because plenty assholes buy that car also?
- insanitybit 3y agoJust because everyone has been saying "composition over inheritance" doesn't mean that that's how things get done. Jump into any Java codebase and you're 99% likely to see inheritance used as one of the primary abstraction mechanisms. > Should a decent BMW driver sell their car, because plenty assholes buy that car also? A better analogy would be "Should you drive on the street where all of the shitty drivers do donuts and street races?"
- mrkeen 3y ago> Prefer composition over inheritance has been a common mantra for at least a decade, if not more. Maybe you should undergo the last decade of development and training. What are some other well known mantras? The null reference is a billion dollar mistake? Minimise mutability? Maybe the language designers and library writers could catch up too.
- kaba0 3y agoInheritance is a useful feature, using bad abstractions is a user error. Nulls are an error in that no language feature solves them (though third-party tooling does), so far at least.
- za3faran 3y agoRecords and value/primitive classes are immutable, not to mention https://openjdk.org/jeps/401 https://openjdk.org/jeps/401
- packetlost 3y agoYeah, I think this is the part that gets me. To be fair, I think well designed Java is flexible in exactly the right ways for an enterprise development. The trouble is that enterprises don't typically pay well enough to get people who are really good and the popular ways of building Java applications are not great.
- deleted 3y ago[deleted]