7 ms·
What I miss in Java, the perspective of a Kotlin developer
- None4U 4y agoThis title confused me very much, although it's technically correct it took me three tries to read it properly
- valbaca 4y agoglad it wasn't just me. Wish it'd been named "What I miss from Kotlin while in Java" or something
- sdf4j 4y agoFor some reason I also kept looking what this person missed from Java.
- mellosouls 4y agoMe too. Poorly worded title; suggests the opposite. I thought it might be something different so was intrigued and then disappointed to see the normal angle.
- fithisux 4y agoI really like that Kotlin, Groovy and Scala keep advancing the JVM ecosystem. Java in my opinion should evolve by keeping the original approach (a better C++) and leave programming language experimentation to the big three.
- qsort 4y agoHonestly IMO Java is in a very good spot right now. Mature technology, maybe a bit "unsexy", but rock-solid and future-proof. There are still pain points, but the language is leaps and bounds better than the Java 1.6 days.
- hurril 4y agoYet, Java-codebases look the same now as they did then. The program structure is still "externalized" to a container because of a lack of abstractions. It's still exactly the same.
- kaba0 4y agoI really don’t get what you mean. Java is not the most expressive language, but it is eons ahead of the expressivity of C or Go and yet I don’t believe it would be fair to claim that those languages also externalize their structure?
- hurril 4y agoWhat I mean is that a Java program is almost always sitting in a Spring container with most important stuff happening as "decorated magic"; this has been the case since even before 1.6. To say that it is somehow better now than it was, is therefore not worth much. Sure, I'm sure you guys'll put a map or a stream in there somewhere but it's ... still the same thing. There's no real evolution at all. The springification is what I mean by it being externalized and you generally don't see that in C, no. The pre-processor might be sort of the same thing but you generally don't bootstrap the entire thing using it.
- kaba0 4y agoWell, Spring is mainly for backend systems that have a very specific functionality that is best handled by a generic solution. In my experience this is in line with how most other web frameworks operate. But this is absolutely domain-specific, Java the language is indifferent to exposing a library that will handle incoming request and dispatch them based on some library logic, or creating a “traditional” program for the litany of other domains where java is used st.
- hurril 4y agoSpring isn't a web framework, it's gaffer tape for a language that just isn't good enough without it; that's why it's [spring] still around and its use is so prevalent. It's not at all mainly for very specific functionality in the sense that your post sort of implies [for web, and databasy things I presume]; in fact: it's just for wiring and for applying/ attaching things to "abstractions". As in: please decorate these sets of things with this behaviour. If you'd chosen languages that permit abstractions in a real and usable sense, then you would not need this. In fact: with _one_ exception, I've never had a container in any Haskell, Scala or F# project ever. Do you think we ever needed one? And this is my point: those other languages have seen _tons_ of development in the "abstraction arena" with (internal) DSL:s and super powerful frameworks for making powerful DSL:s. Java has had none of this because these concerns har all (poorly) handled at the container level. The tacked on "functional stuff" is for sure cool coming from a Java backround, but they're pretty much useless.
- cies 4y agoGroovy and Scala have nowhere near the potential of Kotlin. I think Kotlin actually has the potential to dethrone Java on the JVM, it is that good. Kotlin is not much different from Java, conceptually. It is basically Java 2.0. Where Goovy does away with compile time typing, and Scala bolts a Haskell onto Java; Kotlin is "just a better Java".
- jillesvangurp 4y agoProbably a good way to characterize Kotlin. Scala has a lot of interesting features but also a lot of complexity. And not all of those features are necessarily very good. Arguably, Kotlin was born out of frustration with that as Jetbrains at the time rejected the notion of using Scala internally and instead started the long process of developing their own language. It was mainly intended to be a better Java but also to avoid some of the complexity of Scala. Groovy's lack of compile time safety is like throwing out the baby with the bathwater. It's just not worth it for me. Kotlin gets you most of the language features but without doing that.
- aurea 4y agoDo you have any good articles on what would make Kotlin better than Scala? I work only in Scala and have never touched Kotlin.
- jillesvangurp 4y agoNot really. I likewise don't use a lot of Scala. Probably better to think in terms of how the languages are different rather than better. Here's a good overview of differences: https://www.baeldung.com/kotlin/kotlin-vs-scala https://www.baeldung.com/kotlin/kotlin-vs-scala
- arunix 4y agoGroovy has an optional compile time checking feature on a per-class basis via an annotation (this has been around for a long time) https://docs.groovy-lang.org/latest/html/api/groovy/transform/CompileStatic.html https://docs.groovy-lang.org/latest/html/api/groovy/transfor... https://livebook.manning.com/book/groovy-in-action-second-edition/chapter-10/ https://livebook.manning.com/book/groovy-in-action-second-ed...
- dunefox 4y agoFlix will blow your mind then: https://flix.dev/ https://flix.dev/
- MrBuddyCasino 4y agoNever heard of Flix, very interesting!
- JohnL4 4y agoThrew me down this rabbit hole: https://en.wikipedia.org/wiki/Datalog https://en.wikipedia.org/wiki/Datalog Looks like Flix is in a galaxy of languages implementing Datalog, a variant of (or somehow related to) Prolog. I guess Flix is Turing-complete while Datalog is not?
- dunefox 4y ago> I guess Flix is Turing-complete while Datalog is not? Flix is a regular programming language, and supports datalog as a DSL basically, supported by the compiler and type system.
- rockwotj 4y agoThe only thing I like more about Java than Kotlin is compile times. Man is kotlinc slow.
- moss2 4y ago>In Java, there’s no way to know whether a variable is null. What? What about `foo == null`?
- _han 4y agoThe author means not knowing it at compile-time. The expression you mention must be evaluated at runtime.
- kaba0 4y agoIn practice, there are plenty of static analysis programs that will give you the same thing at compile time with @NonNull and similar annotations.
- giords 4y agoToo bad you need external tools anda lot of boilerplate that you must remember to enforce to achieve this, because it's an after thought and not a language feature. And there's 7-8 different annotations to mark nullability, all slightly different in some detail. It's a bloody and messy hell.
- kaba0 4y agoSure, language-support would be better indeed, but in practice the often encountered annotations are supported by every tool, and the only relevant setting is whether everything is nullable by default or nothing is.
- V-2 4y agoIt's still not enforced by the language itself, so nothing stops a third party library (that you have to integrate) from not using the annotations, and then the unknown nullability exerts a domino effect on your own code, "infecting" it with uncertainty.
- 4y ago
- popotamonga 4y agoIn java i miss property literals, without them you can't have linq to sql easily. Does kotlin have them?
- vbezhenar 4y agohttps://kotlinlang.org/docs/reflection.html#property-references https://kotlinlang.org/docs/reflection.html#property-referen... ?
- kaba0 4y agoThe current language team doesn’t even want to hear about properties and frankly I can understand their point. Properties while heavily used, are a misfeature. I would rather see the ecosystem adapt records. Or once Valhalla happens, one can probably have a near zero-overhead Property<T> field for properties inside a record, properly supporting everything required by Java Beans.
- singularity2001 4y agoThe author misses one main advantage of Kotlin: more concise syntax
- frant-hartm 4y agoI would argue that more concise syntax is actually why Scala became a niche and didn't get wider adoption and why some some examples of advanced Kotlin are really off putting. At certain point more concise syntax goes against readability. ... so preoccupied with whether they could, they didn’t stop to think if they should.
- mojuba 4y agoTangent: I find the choice of keywords val vs. var in Kotlin a bit unfortunate. The way we read text (or rather skim) is a lot of the times by the shapes of entire words. Val and var are a bit too similar in this regard and I think let vs. var in other languages is much nicer.
- tn1 4y agoOn the other hand, when skimming some code, does it matter on the first pass whether a given variable is mutable? Probably not if we're just trying to get an idea of how it works; later on a closer read we can look closer if it matters.
- mojuba 4y agoIt absolutey matters when skimming a well written code. Whether a variable is immutable or not is almost as important as its type.
- hoseja 4y agoWell, it is part of its type.
- nicoburns 4y agoI find this super-important. If a variable if mutable then I need to look closely through the whole of the rest of the scope to see if/where it is mutated. If it's not then I can often mostly forget about it. Thus marking variables as immutable where possible dramatically reduces the mental overhead of reading a function.
- vbezhenar 4y agoWhat I've found helpful is to configure IDE to use a separate formatting for mutable variables. You don't even need `var`/`val` for that, just smart enough IDE. Something like single underline for those variables. This way they're obvious and you'll know it when reading the code.
- V-2 4y agoDue to limitations of JVM itself, reified generics in Kotlin is limited to a very narrow usecase. Basically functions that get inlined. So it's not "true" reified generics as in C#. You can't go "if (T is Whatever)" and put it in your SomeClass<T>. This is not Kotlin's fault, just legacy of Java with which it has to put up in order to maintain full interoperability.
- imtringued 4y agoCeylon had reified generics though.
- rad_gruchalski 4y agoFor reference: it's type erasure.
- V-2 4y agoYes, exactly. T always becomes Object in runtime, type information is physically gone.
- kaba0 4y agoNot quite true, one pedantic point of mine is that it may be a concrete class if that was marked as T’s superclass, the other is less nitpicky, generic information is actually available at the component itself, so plenty of tools can use it in certain circumstances.
- jillesvangurp 4y agoI can echo the Author's sentiment. I've been working with Java and lately Kotlin for the past 25 years. The last five of which almost exclusively with Kotlin. Switching back to Java feels increasingly awkward at this point. I can do it, but there are no use-cases where I would prefer Java over Kotlin. I certainly don't enjoy it. Sure, they've added features to the language and they help but it's just not even close to the language ergonomics that Kotlin has. The val vs. var thing is super relevant because this is actually something that is more cultural than language related. As the author points out, you could just litter your Java code with final and it would be similarly safe code. But the reality is that very few people do that. Java makes you do the wrong/less safe thing by default. Kotlin has this opposite culture where you feel slightly guilty/dirty every time you even make something a var. Having a difference between mutable and immutable lists and maps is likewise something that Kotlin inverts. In Java you could always make lists and maps immutable but they'd be immutable by default. With Kotlin it's the reverse: everything is immutable unless you make it mutable. It's this safe by default attitude that pervades Kotlin that just shows up in a lot of places. Nullability as part of the type system removes an entire category of bugs. The only time I still see NPEs, it usually involves some Java library that I'm using. Considering how easy it is to switch from Java to Kotlin, it's not surprising that you see essentially all the major Java frameworks come with excellent Kotlin support. I use Spring Boot with Kotlin. It's quite obvious to me that using spring without Kotlin is a bad move at this point. There's so much good stuff you miss out on that is added to Spring via extension functions. E.g. having co-routines sit on top of webflux, makes the whole business of using asynchronous IO with Spring a lot less painful. Without that, I'd probably still be doing synchronous IO.
- V-2 4y agoKotlin also discourages inheritance. It's opt-in, not opt-out, because classes are final by default: you need to explicitly mark them as "open" if you want to extend them. The same with functions if you want to override.
- sgt 4y agoWhat I miss in Java is actually more the nullability, as the author points out. I prefer the Swift way of doing that. To me, it feels cleaner than the Kotlin way, despite being extremely similar. I think it boils down to var/let vs var/val.
- HdS84 4y agoAs a C# Developer who occasionally uses Java I understand the sentiment. Java just feels dated. You have to do a lot by hand which should be done by the language. And new features like streams are just a bad riff of .NET Linq, which is much better in ergonomics. Finally, Generics are a big turn off, who ever thought that the Java implementation is a good idea was delusional.
- oldsecondhand 4y agoOn the other hand Java's generics made it easier for Clojure to be developed.
- pflanze 4y agoCan you say more about this? My first thought is that you're talking about the various data types in Clojure being implemented with Java interfaces. But my understanding is (I'm not a Java developer) that interfaces existed pre generics, just without static type checking support, and thus Clojure could have been developed with a pre-generics Java version just fine?
- jiggawatts 4y agoJava streams have some advantages over C# Linq. IMHO, there's a more fundamental concept that everybody seems to be missing, which is that general purpose programming languages somehow fail at general purpose queries across trival, in-memory data structures. A random example I like to bring up is: Can your LINQ-equivalent efficiently and idiomatically calculate both the "min" and "max" of a list without enumerating it twice? Imagine you're writing some abstract library that can take any IEnumerable as the input(s). Will it scan a 1TB database table twice because the idiomatic coding style is: var min = somelist.min(); var max = somelist.max(); This is such as basic, trivial example, yet C#, Java, Kotlin, etc... languages just fall flat on their face. Meanwhile, in SQL, a 48-year-old query language: SELECT min(...), max(...) FROM sometable But this is just scratching the surface. Ideally, all LINQ-style data inputs should be treated as something like a B-Tree index. Something that can be "walked" (not just enumerated!) efficiently using prefixes, ranges, and similar conditionals. Multiple queries should be able share such as "walk" to minimise the cost of streaming data in from memory... or wherever. Data should be partitioned across CPU cores just as efficiently, and the query language should make this natural. I'm yet to see a language that scratches this itch.
- TacticalCoder 4y agoThe devil is in the details: the capitalize example will badly fail on an empty string right? (substring will throw a StringIndexOutOfBoundsException). So we guarantee you you'll get a String back. And hear this: we also guarantee you, 100%, it's going to be non null. And we guarantee you that the reference to that non null String is going to be immutable. Oh but wait: maybe you'll get a RuntimeException instead...
- CrimsonRain 4y agoCan just return "" in that case? I think you are focusing on the wrong details of an _example_. The details here is being able to extend and function chaining; not the algorithm of capitalize().
- TacticalCoder 4y agoOh I didn't explain myself correctly. I've got nothing against the example in the blog post. What I mean is that no matter if Java or Kotlin makes guarantees about null safety, invariability and the type of the object returned, in the end you still have to deal with the glorified GOTO statements that exceptions are.
- CrimsonRain 4y agoI like Java but being able to tell at compile time that don't allow null on something no matter what, gives you a lot of advantage (and some disadvantage if you design wrong). Exceptions are not necessary in this case as null safety can not be isolated (unless you choose it). It is either everywhere or nowhere or you force an arbitrary line by telling something not-nullable when it can be (not a good idea). A good example is the state Dart is in right now. Almost everything has been transformed to null-safety including most of the community packages and it works really nicely.
- kaba0 4y agoThey are the same thing as returning a sum type, just your goto hides in an if statement in that case you explicitly have to write out one way or another.
- skitter 4y ago"finally" in Java and afaik "val" in Kotlin because of it are a bit of a sham: They work fine for primitives, but for reference types only apply to the object identity. You can still mutate the pointed-to object willy-nilly.
- oweiler 4y agoIt means non-reassignable, nothing more.
- vbezhenar 4y agoThe way this is solved in modern code is that you very rarely have mutable objects. So basically you can expect all objects to be immutable (excluding obvious exceptions like builder objects). Some frameworks like JPA are not up-to-date with this methodology, sadly. But Java direction with immutable records is obvious and they'll adapt.
- sischoel 4y agoIsn't that the case for most programming languages? The only exceptions I can think of are C++ and Rust.
- arrow7000 4y agoIt's not like this in the functional languages I'm familiar with, F# and Elm
- kaba0 4y agoIt is not like this in Java either if the components are all immutable as well. It will be shallowly immutable, but applying it recursively will make it totally immutable. The reason I say that is that in practice it is not that different than FP languages, they just differ in the ratio of FP data structures.
- Sankozi 4y agoconst in C++ is more like Readonly from TypeScript - you cannot modify state of an object, but it still might be possible through other reference/pointer - you have to check source of that value to be sure
- stefs 4y ago> Such functions can be called in the usual way as if they were methods of the original class. This mechanism is called an extension function. well, that's not exactly true, because extension functions can't access private fields.
- arunix 4y agoSome comments in a previous thread suggested that the refactoring support for Kotlin is not on par with those for Java. Has there been any change in that area? https://news.ycombinator.com/item?id=27183571 https://news.ycombinator.com/item?id=27183571
- throwaway4good 4y agoI like fast compile times, backwards compatibility, all my code base in one language, vast tool support … If I wanted to mix in or use a new language, I would want something that was a substantial upgrade in terms of expressiveness and platform. To me Kotlin is a bit like what Coffescript is to JavaScript.
- DarkmSparks 4y agoHmm, I been on a total of 2 kotlin projects, both went badly. On the other hand, we have several hundred java projects, with a very low project failure rate. Maybe java is missing some things that are trendy and you get used to using, the things we missed most in kotlin was working code that was easy to debug, repurpose and deploy.... Stupid issues like the compiler not working properly or just taking ages burnt to much developer time, and to hard to find staff to make it up. But heh, maybe I'm old fashioned, it doesn't seem like a popular opinion. But nothing in this thought piece seems to address those fundamental issues.
- ryandvm 4y agoSame exact experience. I recently tried my hand at rewriting one of my old Java Android projects in Kotlin. Maybe it's just my brain getting older, but I found Kotlin development experience to be far less enjoyable than I was anticipating based on all the high praise. Don't get me wrong, Kotlin has some nice language improvements, but overall I found it to be more confusing than helpful. A weirdly confusing co-routine implementation, new unintuitive syntax, lots of new keywords, etc. I dunno. I'm sure it's a fine language if that's what you're starting with, but as a former Java dev that was actually productive, Kotlin was anything but a breath of fresh air.
- rwalle 4y agoYour comment seems to be the definition of word "anecdotal".
- tsaixingwei 4y agoI agree with you that it is an anecdote, but I don't see anything wrong with sharing personal experiences of using a certain technology... this is a discussion forum after all and not a scientific paper.
- AndrianV 4y agoWhen used in conjunction with named parameters with default values, it makes it possible to write function signatures that are significantly more readable than those written in Java, and with far less boilerplate.
- davidgrenier 4y agoNever let this guy learn F#... it'll ruin all programming for him.
- mrighele 4y agoI disagree with point 1). The real problem is not that immutable references are inconvenient to use, but that you cannot mark the whole instance as read-only and get something similar to the const-correctness that you can achieve with C++. There are tools that help with this, but having something either in the language or in the JVM/standard library would be much more useful. Point 3) and 4) are in my opinion not that big of an issue. Annoying, yes, but not deal-breaker. Null safety is really something that I miss in Java and what I consider the biggest flaw in the language. Again, there are libraries and tools that help with that, but it is not the same as something which is part of the standard.