10 ms·
I am an old school java developer. Can someone explain me why this is a "feature"? I can remember by counting my fingers the times I had to use "instanceof", an
by quilombodigital 4y ago
I am an old school java developer. Can someone explain me why this is a "feature"? I can remember by counting my fingers the times I had to use "instanceof", and it was some classical reflection/injection/instantiation framework 'vodu'. If you are using instanceof in a normal java program, you are just making it wrong. It looks like some new javascript guys arrived in the block and are trying to make java look something messy like Scala. What real world problem is this trying to solve? Are you just fucking bored because you are all out of ideas and every JDK release we need "something"? Why these JEPs only have "boxes,rectangles,A,B,I" class examples and not a simple real world class scenario? Why we need to make java "friendly" to work with generic objects? it should be hell! I cant wait for JDK 50.
- metadat 4y agoThis doesn't take anything away, it just adds extra convenience and flexibility.
- quilombodigital 4y agoMy problem is with the "just adds"... Scala made this mistake, and ended with many different ways to do the same task. It is a powerful language, but I believe languages must have only one (or two) concise way to to things.
- halfmatthalfcat 4y agoThe "multiple ways of doing things" is a feature, not a bug and something which lives in almost every programming language. It's become a meme at this point and a lazy argument against a language.
- metadat 4y ago> It's become a meme at this point and a lazy argument against a language. This might be a casual dismissal of what is in fact a nuanced aspect of programming languages. Multiple ways of doing the same thing can make the language less accessible because it becomes confusing to reason about what the "right" way is. That said, this particular addition merely reduces repetitive toil, and it follows similar existing Java conventions, for example see the Java-7 equivalent of Python "with-statements". https://stackoverflow.com/a/35116251 https://stackoverflow.com/a/35116251
- quilombodigital 4y agoyep, agree with you. I become more aware of this when I worked some years in a huge ruby project. Everything was allowed, and with tons of DSLs you dont know anymore what is happening.
- Banana699 4y agoThis is obviously false in full generality, C++ has many (M A N Y) ways to do anything, and I have never seen even the most fanatic C++ fans defend this as a good thing. Nearly every single C++ dev hates some (large) subset of the language, which subset is another matter entirely, but a subset nonetheless. Bjarne Stroustrup once noted in Design & Evolution of C++ that people want different things from the seemingly simple artifacts we call "Programming Languages". Some people want an algorithmic language to express procedures cleanly in, others want a design language to express large scale systems in. Some want a terse and uniform "executable mathematics" notation, others want a down-to-earth worse-is-better lets-make-some-goddamn-apps-and-put-some-fucking-bread-on-the-table working class language. Some want a language with an emphasis on the lone author, others an emphasis on the corporate business team. A language as a tool that you wield skillfully for the single purpose it was built for, a language as a toolbox full of gadgets, a language as a material to build the previous two from, a language as a community hub to organize around, etc etc etc. I have seen people argue that "Language" the wrong metaphor to understand computer notations entirely. This is why you can't say anything general about programming languages.
- adra 4y agoIf you wanted the idiomatic only-one-way-to-do-things approach to do things, Java is not and has never been the language of choice for that. Why do we have 10 different dependency injection /logging / web frameworks? Who cares! Let's add an 11th for good measure. I don't want java to be idiomatic and purist conformant tedium. You're welcome to switch your language of choice to go if that's really your bag.
- quilombodigital 4y agoThe examples you gave are not language features, are frameworks/libraries... not the discussion. (the only exception I can see is the "logging API", but this is debatable)
- wiseowise 4y ago> but I believe languages must have only one (or two) concise way to to things. Java has none.
- quilombodigital 4y agoI humbly accept the critic. :)
- nchi3 4y agoBecause there are scenarios where pattern matching (or instanceof) is the better and more ergonomic thing to use. One obvious example I can think of is event handling. If anything, this is _not_ something JS devs are asking for, but rather devs using functional languages.
- Fordec 4y agoI also think of API ergonomics. Unspecified input with the same single endpoint, while under the hood will be absolute spaghetti, from a developer adoption standpoint can be make or break. And if you're strongly typing your system rather than relying on strings, extra so.
- quilombodigital 4y ago
- grumpyprole 4y agoSometimes it isn't desirable to scatter the logic across many classes, or conflate the data and the handling. The Java world has the "visitor pattern" to help deal with that, but its double-dispatch is clunky, complex and verbose. Pattern matching just generalises switch and makes it more useful. Java is genuinely a better language with this change.
- misja111 4y agoExactly, this is what OP was trying to point out. With the record matching feature, you don't need the extra abstract base class anymore. This is not to say that abstract base classes are never a good idea, but sometimes in Java they were only there for the convenience of one handler method, to prevent the use of instanceof. This didn't improve readability. That's when the record matching can be a good improvement.
- mathgladiator 4y agoI wish they had better examples, so here is one. Suppose you are making a scene graph, and you want to have a visitor and run code based on the type of the nodes in the graph. Well, consider two approaches. The first approach is to use a bunch of ifs and instanceof, but this isn't clean and it is fragile. The second approach is to make an interface that has all the types listed under a handler (void handle(Type1 t), void handle(Type2 t), ...) and then write some boiler plate code to do the dispatch. This feature aims at the conciseness of first model (instanceof) with the safety of the second approach (i.e. all types are handled). I would love this feature for my project! ( https://www.adama-platform.com https://www.adama-platform.com ) since I use Java to implement a programming language.
- quilombodigital 4y agoThis is the point, you dont consider the instanceof approach, is just wrong. You already know your types in advance.
- hztar 4y agoyou sound like a Cobol developer I once knew.. RIP
- quilombodigital 4y agoThis is true.... Old guys know nothing... for years we told the javascript guys the mess they were doing, and voila, all dynamic languages suddenly started to convert into statically-typed, and at the end, they have this webassembly, that runs in a schizophrenia-type VM wannabe. Congrats. You lost 10 years of your life because you didnt listen to us when we said that we have learned something from Self and Smalltalk.
- Skinney 4y ago> and voila, all dynamic languages suddenly started to convert into statically-typed This is... just wrong. Plenty of people are using JS without any sort of typing whatsoever. Other dynamic languages, like Clojure, Gradle, Elixir, Pharao (a smalltalk), just to mention a few, are still going strong. > they have this webassembly, that runs in a schizophrenia-type VM wannabe wasm is mostly being used as a target for C(++), Rust and even C# programs that want to run in the browser. It's there to expand the reach of the browser. Most JS projects don't touch the stuff.
- jstimpfle 4y ago> wasm is mostly being used as a target for C(++) because... JS isn't good enough?
- samus 4y agoWhy should JS be used as a compilation target for C/C++?
- bigDinosaur 4y agoTypescript is absolutely coming to dominate the field IME. Obviously I have no true idea about the relative use % everywhere but I haven't worked at a place in the last few years that wasn't at least trying to use it. There's enough legacy JS out there to provide work for people who hate static types for many years though.
- jayd16 4y agoThe sealed interface, final implementation case is an interesting one to inspect. Now you can, for example, write a parser with a fixed set of tokens and write switches that exhaustively handle every token. You now get compile time guarantees.
- quilombodigital 4y ago
- kaba0 4y agoYou really could do away with the arrogance.
- quilombodigital 4y agoI dont think saving a few lines for a very rare corner case is a strong reason to add a language feature
- jstimpfle 4y agoMaybe they have a point? Arrogance != Wrong in Your Opinion.
- kaba0 4y agoSure, though as I mentioned in other comment, pattern matching is basically a better Visitor pattern. Which is while not the most common pattern, is not that much of a niche either.
- jayd16 4y agoIt's just an example... And you don't seem to understand that it's about the safety of the exhaustive check not just the sugar.
- halfmatthalfcat 4y ago"...look something messy like Scala", I feel bad you feel this way as Scala is an incredibly powerful and elegant language. It's ironic because you're throwing shade at Javascript when JS developers are doing the same thing against Typescript. Fearing what you don't know or understand under the guise that change is bad or things are "good enough".
- quilombodigital 4y agoI agree Scala is very powerful. I said messy because it is "too powerful", added too many features. I think this is their mistake. I respect the community, but can show my opinion as you can about java.
- dopidopHN 4y agoI really liked scala… until I had a to maintain a large and « old » code base in it. I think messy fits.
- grumpyprole 4y agoJava is also a messy language and the reason for a lot of the mess in Scala. If you want elegance, then you should probably learn something like Haskell.
- quilombodigital 4y agocan we have a not-messy-not-elegant language? :)
- deleted 4y ago[deleted]
- BlackFly 4y agoIt shows the majority of its strength in switch statements. You can mostly just use the instanceof for a one line destructuring of a compound object into its constituents. The corresponding rust statement is the `if let` or `if case let` if you are more of a swift fan. Generally you would use it with sum types which were previously extremely nasty to work with in Java but are more useful now with sealed classes. The pattern shows up a lot with result and option types but you can do it any other time where a tagged union makes sense. The classic way to do a tagged union in Java was just a personal taste amongst bad options: tagging interface and instanceof on implementations or an encapsulating class that will return null (or throw an exception) if the gets are called for the types which aren't encapsulated. If in your career you never needed to enumerate cases that required different data in each case and you couldn't see how binding those requirements into an invariant via the type system was helpful then you probably won't use these new features either. Some people thought it was helpful but the boilerplate cost was too high so wouldn't do it in practice (I am in this category). Edit: you could also make tagged unions more type safe with callbacks, but then implementing a closure to pull the value out of the callback was just annoying.
- quilombodigital 4y ago"If in your career you never needed to enumerate cases that required different data..." . Maybe one or two times... but it is not like "hey, it is impossible", or "shit, I have to write 10 more lines" in this rare code I will never put my eyes again. What I get angry is that people will start to use everywhere, in places they should not be using. because the guy did not wanted to stop five minutes to think about it. The river flows through the easiest path, but this does not means it is the smartest path.
- Twisol 4y agoUnfortunately, I've seen plenty of Java 8 code where people did not "stop five minutes to think about it". I don't think any language can protect us from ourselves. It's fair that you've found sensible ways to achieve your goals without ever needing pattern matching. Nobody should fault you for that. But can you grant that other developers might have other sensible ways to achieve other goals (or sometimes other ways to achieve the same goals!)? I think most Java developers will be familiar with the visitor pattern. In almost all cases (there are exceptions!), I detest it; I find sum types with pattern matching to be a far superior way to say what I mean. Java added `sealed` interfaces recently, so you can actually model a closed family of types. Now pattern matching closes the loop, giving you a supremely ergonomic way of dispatching over that closed family without using a visitor. Maybe it's not clear from other responses, but the part of the JEP about an "exhaustive switch" is critical here. Java statically guarantees that you've handled each member of your closed family, just like if you forgot to implement a method for a visitor.
- kaba0 4y agoinstanceof is but one possible use case. Arguably the most useful part of pattern matching is inside switch expressions, and there is an equivalence between the Visitor pattern and this, but the latter is much more readable and maintainable. Sure, the visitor pattern is not that often used, but in certain niches that make use of ASTs often it is pretty much a must.
- Frost1x 4y agoHighly compositional design patterns in Java. When you have a fairly large set of custom types that are hierarchically structued in nature, it can be quite useful. In most standard IT applications it may be less common. I've seen it used fairly well in scientific applications. There are of course other ways of approaching working with and acting on wide taxonomies of objects that could exist but this is one of them.
- equalsione 4y ago> If you are using instanceof in a normal java program In applications yes, but instanceof is heavily used if you write frameworks. And as someone else has mentioned, the more functional style java you write the more use you start to make of instanceof
- jstimpfle 4y agoI've never been a fan of Java due to the GC and the boilerplate and runtime overhead (time but also space) that come with it. But I've always had respect for it because it felt so minimal and consistent in its own way, bulky but solid kinda like a BMW car. Now that functional stuff and instanceof fluff is something else. I don't like it either.
- kaba0 4y agoUnless you only prefer low-level languages, Java’s runtime is anything but bulky. It is one of the fastests runtimes out there, so runtime overhead is relative.
- jstimpfle 4y agoThe runtime might be an efficient implementation of the language, but the language itself (given GC and still no custom value types AFAIK) implies a distinct bulkiness compared to what I am used to. This is as I said my personal feeling when using the language, and is based on the number of keystrokes I've needed in the past to get from A to B, as well as the runtime overhead (memory overhead for many small objects, add in GC and chances are it's very noticeable time-wise too). It much depends on the task of course. My biggest project in Java was a SAT solver years ago, and millions of little clauses did not sit well with it at all. I ended up changing the data organization internally. My first attempt, the natural AOS would have been also the best choice performance-wise in C, but in Java it choked the GC. I had to switch it around to SOA which was quite awkward but it reduced the number of objects from millions to a small constant. Also thinking of the number of lines of code running for JIT, GC, whatever... in the background all the time...
- kaba0 4y agoI get that, and sure enough for certain workloads it is definitely not ideal. But I think you would be surprised at how fast it can actually be. In projects where you can’t get away from object allocation (due to it being less deterministic for example) with some clever trick like arenas and the like, chances are the GC will be actually much faster than malloc-ing them all — and while it may not sit well with you, GC running in the background just amortizes the cost of each deallocation to basically zero time overhead (of course at the cost of memory overhead). But I’m not trying to dismiss your experience or anything, the JVM has its place and of course there are plenty of use cases where it is not the best fit at all.
- valcron1000 4y ago100% agree. Using 'instance of' in Java is a terrible anti-pattern, same as trying to do pattern matching. You should lift that code into instance methods and let the runtime pick the right implementation.
- Fellshard 4y agoThere are some cases when it's actually simpler or more maintainable or more useful to do the dispatch locally. A language that allows you to both is tougher to learn and can have a higher risk of pitfalls, but also means that a seasoned practitioner can apply the right tool in the right situation. In terms of the classic 'patterns', it's the difference between standard dispatch and the 'Visitor' pattern. There are cases where you want to specify each case of data structure locally, and others where you want to specify each method of each variant locally. Welcome to the expression problem! There are cases in Java where you do want a true POJO - just a bag of structured data. This is probably a very strong candidate for using along side that.
- peterkelly 4y agoThe downside to that is if you want to write a new function that deals different subclasses, you can't keep all the logic in one place; you have to spray its implementation across all the different types it has to handle. This is the #1 reason I prefer languages with sum types and pattern matching (or the ability to emulate it via unions and instanceof checks, e.g. TypeScript or Python + mypy).
- pharmakom 4y agoYou could implement a crappy union type in Java with a type-safe API, but if you want to program in that style you are really better off using an ML or Scala.
- pharmakom 4y agoI actually disagree. instanceof is a fast operation on the JVM and can make code easier to read than equivalent OOP (less hopping around). However, you must be careful that all cases are covered since Java lacks exhaustive pattern matching.
- rr808 4y agoI kinda feel the same. Modern Java is written like some Python/JS Mashup. Its one reason old code bases are a huge mess because code is written in many different styles.
- peterkelly 4y agoPattern matching is a key feature of functional programming languages; it predates Java by 20 years and remains widely used today. See https://en.wikipedia.org/wiki/ML_(programming_language) https://en.wikipedia.org/wiki/ML_(programming_language) For some examples relating to business logic, see https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/tutorials/pattern-matching https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/... Lots of compilers are written in functional languages and use pattern matching for traversing abstract syntax trees, dispatching on the node type. The OO way to do this is to put all logic relating to a particular class in the class itself. The functional approach is to put all logic for a given method/function in one place and handle all the different types. They are two different philosophies both with different tradeoffs, and which is best depends on the type of program you're writing. But it's nice for a language to support both, so you can pick and choose which approach is best for a given problem.
- 62951413 4y agoThe way this is phrased gives a bad name to those mythical enterprise/old/Java developers. I personally don't want to be associated with this stereotype, I have another 20 years of interviews in the future. I started programming in Java in 2002. I had some of your stated attitude learning Scala ten years ago. Probably because I grew up on Turbo Pascal and C++ , majored in EE, and didn't hear about PL theory. I cannot fathom anyone wishing to go back to Java after using Scala (as a better Java, without cats/scalaz&Co) or at least Kotlin. The way you describe new Java features I'd think you come from a golang background. I have nothing against staying on the JVM for the rest of my career. For this to be feasible we need modern languages fully supported and popular. I personally want more concise syntax and more elegant abstractions in my daily life.