I make everything final and annotate every return type and parameter as either @NotNull or @Nullable. Some people say it's too verbose, but it has cut down the amount of mistakes I make by probably an order of magnitude.
Or I just use Kotlin and get all of this and more for free. :)
That's not a bad idea. I find myself using @NotNull WAY more often than @Nullable so maybe only annotating the latter is a good way to cut down on verbosity while also getting the same linter warnings.
Though I think it still might be worth annotating parameters as @NotNull because I believe you can still pass @Nullable values to non-annotated params without triggering the lint warning, right?
Exactly. A Lint warning is not as good as a compiler error like you get with Kotlin, but it's the best thing that we'll get in Java, and if you're paying attention, you still have almost no excuse for NPEs. The only exception I can really think of is if you call a library function that either isn't annotated, or that your IDE can't infer the nullity for. Those are always a huge bummer. :(
Since final isn't an extra keyword in Kotlin, but a choice between val/var, you're forced to make the decision every time you make a variable whether it should be final or not. It seems like a trivial distinction, but tell me when the last time you made a local variable final in Java was. For most people, that's not a thing you usually do, because it's verbose.
As for nullity, in Java, this is legal:
String foo = null;
In Kotlin, this is not:
val foo: String = null
You must change the type to String? (with a question mark at the end). And if you want to pass a String? to a parameter marked only as String, you have to null-check first. Similarly, if you want to take a return-type of String?, you can't put it into a variable of type String without doing a null-check.
For all intents and purposes, String is a subclass of String?, and all regular polymorphism rules apply here.
Since Kotlin makes you actively choose whether something should be final or not, or null or not, every time you declare it, it means most variables and return types end up being non-nullable and final, which is great.
You have to add a new dependency to your build.gradle. This is kind of annoying for Android developers. Fortunately, it's surprisingly small, at 4813 methods. This sounds like a lot, but it's a whole language, with a staggering amount of functionality (pretty much anything you could want from Guava, for example, is in Kotlin), and most of us already use RxJava, which has 800 more methods than Kotlin!
public is the default modifier. This is certainly better than the infrequently-used package-protected modifier in Java (which has been replaced with something called internal in Kotlin, which is WAY more useful than package-protected), but private really should be the default in my opinion.
When interoping with Java code, you get back objects that are typed as String!. This basically means "we can't tell if this is null because the Java compiler doesn't tell us that information." The sane thing to do, in my opinion, would be to treat them all as nullable. However, for "convenience's sake", Kotlin chose to treat them all as non-null! This means that you can still get NPEs in Kotlin if you're calling Java code. This is, in my opinion, the worst part of the language, as it kind of subverts all of the null-safety the devs worked so hard to establish.
Aside from that, fantastic language, and easily my favorite language that I've ever worked in (with a close second being Rust, which has completely different design goals but is still incredibly elegant).
Both(#2 & #3) of your points are not really bad parts of kotlin though, you know the reasoning behind these decisions:
2) Yes, from puritan point of view, private-as-default would be better for OOP languages, but kotlin is pragmatic language, not puritan one and statistics show that public modifier is used in much more cases than private one. So using public as default saves a lot on keystrokes in a long run. Can't link that, but there's some JetBrains post about decision behind "public-as-default modifier".
3) String! will not appear if java code is marked by @Nullable or @NotNull annotations. Instead kotlin will fallback to corresponding type. Also, Platform types (String!, Type!) are just another pragmatic decision. Otherways, if everything cast to optionals, developers would be annoyed by explicitly unwrapping every variable they interact with. Redundant safety is also bad.
P.S. #1) point is also meh, as 5k methods is literally nothing thesedays.
Point 1 is very important. Methods count means a lot to a lot of developers, who are trying to avoid Proguard and multidex.
2) I understand why they chose public-as-default but it's still not a good idea. The language is all about safety, and adding one more word in a few places is no big deal. You shouldn't be using public that often that it gets annoying.
3) This is absolutely an indefensible decision. By that logic, we shouldn't have String? as a type at all, because unwrapping it is annoying. We should just get rid of null-safety altogether. Most NPEs in Kotlin come from Java interop. The fact that platform types exist is a HUGE source of bugs. Doing ?. instead of . is not inconvenient. Being forced to check to avoid crashing your app is not an inconvenience, it's a feature.
5
u/Paradiesstaub Nov 26 '16
How about making everything possible
final(class, method, variables).