What I Miss In Java (The Perspectives Of A Kotlin Developer)
This is a modal window.
The media could not be loaded, either because the server or network failed or because the format is not supported.
Formal Metadata
Title |
| |
Title of Series | ||
Number of Parts | 542 | |
Author | ||
License | CC Attribution 2.0 Belgium: You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal purpose as long as the work is attributed to the author in the manner specified by the author or licensor. | |
Identifiers | 10.5446/61585 (DOI) | |
Publisher | ||
Release Date | ||
Language |
Content Metadata
Subject Area | ||
Genre | ||
Abstract |
|
FOSDEM 2023171 / 542
2
5
10
14
15
16
22
24
27
29
31
36
43
48
56
63
74
78
83
87
89
95
96
99
104
106
107
117
119
121
122
125
126
128
130
132
134
135
136
141
143
146
148
152
155
157
159
161
165
166
168
170
173
176
180
181
185
191
194
196
197
198
199
206
207
209
210
211
212
216
219
220
227
228
229
231
232
233
236
250
252
256
258
260
263
264
267
271
273
275
276
278
282
286
292
293
298
299
300
302
312
316
321
322
324
339
341
342
343
344
351
352
354
355
356
357
359
369
370
372
373
376
378
379
380
382
383
387
390
394
395
401
405
406
410
411
413
415
416
421
426
430
437
438
440
441
443
444
445
446
448
449
450
451
458
464
468
472
475
476
479
481
493
494
498
499
502
509
513
516
517
520
522
524
525
531
534
535
537
538
541
00:00
Software developerGateway (telecommunications)Java appletSoftware developerJava appletLevel (video gaming)Android (robot)Formal languageConnectivity (graph theory)Multiplication signServer (computing)Insertion lossPublic key certificateFront and back endsCodeComputer animation
02:49
Object (grammar)Social classJava appletSoftware frameworkSpring (hydrology)String (computer science)Formal languageJava appletRow (database)Multiplication signSocial classLibrary (computing)Goodness of fitParameter (computer programming)Object (grammar)Default (computer science)Error messageCoprocessorType theoryWritingReading (process)Compilation albumImplementation2 (number)Computer animation
06:32
String (computer science)Operator (mathematics)Interior (topology)Element (mathematics)Glass floatRepresentation (politics)Object (grammar)NumberType theoryFunctional (mathematics)Library (computing)WritingSystem callFormal languageRun time (program lifecycle phase)CompilerSheaf (mathematics)Extension (kinesiology)Pointer (computer programming)Computer animation
08:30
Extension (kinesiology)Social classSpacetimeSocial classDivisorFormal languageUtility softwareExecution unitLibrary (computing)Java appletObject (grammar)Software developer1 (number)Computer animation
09:21
Extension (kinesiology)Social classSpacetimeString (computer science)Price indexDesign by contractNormed vector spaceMeta elementFluid staticsSocial classFluid staticsString (computer science)Computer animation
09:39
String (computer science)BytecodeDot productLevel (video gaming)Functional (mathematics)Constructor (object-oriented programming)Social classType theoryState of matterSoftware developerObject (grammar)CodeRootFormal languageDefault (computer science)Java appletString (computer science)Computer animation
12:18
Generic programmingData typeGeneric programmingRight angleJava appletMultiplication signComputer animation
12:38
Generic programmingData typeInterface (computing)Demo (music)String (computer science)Wide area networkExecution unitTwin primeMultiplication signType theoryFunctional (mathematics)Electronic mailing listRight angleLine (geometry)CodeSocial classElectronic signatureBytecodeOvalProduct (business)WritingBitBootingFluid staticsLambda calculusJava appletContext awarenessExecution unitSpring (hydrology)Parameter (computer programming)CompilerString (computer science)Video gameComplete metric spaceResolvent formalismImplementationGeneric programmingComputer animation
20:36
TwitterProcess (computing)Multiplication signComputer animation
21:01
Program flowchart
Transcript: English(auto-generated)
00:05
Thanks to be here. Thanks for missing your lunch and let me rant about Kotlin, I'm Nicola Frankel. I've been a developer for like now more than 20 years. Perhaps. I'm not a developer anymore I'm a developer advocate, but I still see myself as a developer. I love developing right now
00:24
I work on the Apache API 6 Gateway, it has nothing to do with Java. It's just an infrastructure component But since they allow me to be here, here is the slide. Anyway, Why am I talking about Kotlin in a Java room? Well, they didn't want me to talk about Kotlin in the Kotlin room
00:46
So that might be one of the reasons and also I've mentioned I have like two decades of coding experience and Those two decades were spent in Java. So I started with Java
01:02
1.3, 1.4, 1.3 perhaps and well there were like big wins and small losses and I tried Scala and I didn't like it at all. I still have the certificate Saying hey, you are like Scala certified
01:22
I didn't like it for multiple reasons. Then I went to a couple of conferences I had a friend and she made also like talks about Kotlin and I said we don't need Kotlin we have Java, it's enough. Then I Wanted to teach myself Android developments and
01:42
Android developments at the time was only in Java and the API was super low level and I said no I cannot write such like codes that it brings me back 15 years ago when I was writing Java on the server side and I looked for solutions and I found Kotlin And I thought wow, that's cool
02:02
And I stopped learning androids and now I write Kotlin on the backend so just a disclaimer If Java is the best language in the world for you just leave the room I'm not trying to bash Java, but depending on your culture, I'm pretty straightforward. You might feel offended
02:22
So better leave now, it's up to you Okay, let's start slow immutable references. I'm telling you. Hey It's better in Kotlin and you can tell me hey Java has it, right? immutable references Okay, let's try to check how it works
02:42
immutable references in Java fun stuff Immutable references in Java So Here this is my immutable reference class so I have To put final here. I have to put final here and I have to put final here now
03:04
I have immutable references Who puts final everywhere? Couple of people No, really We we add this this idea 15 years ago, and then we decided no, it just makes this like the reading so much harder
03:23
So perhaps not even worse like if you don't put final on the parameter you can actually relocate stuff Which I believe is one of the worst thing you can do like really really really bad So I'm not saying that
03:40
Java doesn't have it, but I'm saying that in Kotlin it's From the beginning you need to decide whether it's a vowel Which means it cannot be reassigned or it's a vowel which means it can be reassigned if you are using IntelliJ I don't know about Eclipse or NetBeans. The good thing is that If I say it's a vowel so it can be reassigned. It tells me hey like
04:05
There is something fishy. It's not an error per C, but at least it's visually like pleasing And of course by default Yeah for who's who don't know any is the equivalent of object. So it's not very important
04:22
I could write object by default. You cannot reassign parameters any language that allows you to reassign parameters Should be taken with like at most caution It's actually not a great design ID So, of course Java was designed a long time ago doesn't have it
04:40
But Kotlin takes the lesson and if you think that Val and Vore are coming from Scala because you are a Scala fanboy That's completely true. Kotlin has stolen every good ID So don't pretend otherwise, that's fine. Okay Seconds immutable classes. Well, that's fine. Now we have Java records. We've got them. That's fine. Let's continue now safety
05:08
Now safety in Java is not that fun, right? How many ways do you have to implement null safeties in Java?
05:22
yeah, that's a good diversity is a good thing because we work in IT and if you if you want to really Like a fun you might check like nullable nullable nullable. No, no, no, no, no, no, no, no Well, I'm sorry, sorry
05:43
Yeah, exactly, I'm not sure it's an error I think I just copy pasted so I'm not sure yeah That's really fun stuff. And of course, they won't work with one another. So you needs to have the processor Sorry up You need to have the compile time processor and you need to choose which library you will be using and then you hope that somehow
06:05
It will work Good in Kotlin, what do we have? Well, it's baked into language So basically here I was too lazy to write it in Java But basically if you write something in Kotlin, you have additional types. So basically for every like
06:23
normal type This is a non nullable type and this says it's a nullable type It means that if you are calling something on A non nullable type you can call whatever you want
06:40
Plus whatever and If you call something on the nullable type Kotlin will say oh it was saying something and now it doesn't say it Yes, because plus plus is smart
07:03
plus knows how to operate on like nullable types But let's do something that it's not safe Which is it's also very smart The library is too smart for me
07:26
Yeah empty that and We've reversed Yeah, finally things And it tells you hey, you know, this might be nullable. So please don't call it like this
07:41
Because there is a chance you might encounter a new pointer at at runtime and yeah, you should take care and well Afterwards, it's quite easy. You can do okay this stuff I mean if you have been doing groovy or I think Scala does it to the it's But the compiler tells you you should be careful about this
08:02
So and it's again it's baked into the language So like for every type There are like two real types one that might be nullable. The other might not be nullable the good thing we've I will show you after where a section function you can write extension function that work on nullable type, which is really really crazy
08:23
Good so second stuff better The utils classes Who has not written a single unit utils classes in their life?
08:44
Nobody so even people younger than 30 have written them In general there might be a divide because like yeah all the developers they have written them the younger ones They are smart enough to use the library But the thing is well at our age. There was no library
09:02
So so basically we say that Java is is a lot in the object oriented language And then we put everything in a class. We put static methods in the class and we pretend that it's object oriented Right
09:20
Well, let's not pretend it's object oriented if it's no object a static class Class with static methods is not object oriented. So here I have created my my amazing String utils class and of course, I need to remember because the users of my class Well, they might instantiate it
09:41
So I will just remove the constructor make it private again Very object oriented and then I create these Capitalized stuff and I do whatever I want and then I can call the capitalized method Good Scala and Kotlin have I think like center stuff
10:03
Let's not pretend We can we are An object oriented language we can just add methods and states but mostly methods On existing classes
10:20
That's crazy Yeah, of course and the bytecode level it boils down to a static method That's not the problem. The problem is the user experience is a developer experience We can see now that we are really doing Here, we actually are doing object oriented development. So through this functional stuff
10:48
Because here we create a function That is at the root level We are able to write better object oriented code Which is mind-blowing even better
11:04
As I mentioned we can say hey this only applies so here we can have vor F of Type string might be null. So it's this is a nullable type And here we cannot say F dot capitalize
11:23
Because it's it's now right It's a nullable type here. It only applies to real strings, but we can do something like this we can say hey, it might be a nullable type and we can check if this Equals null
11:41
Then we return the default which might be an empty string Yeah, I see like what the hell? That And in the end, that's a static method But how you call it is like object oriented which in my opinion makes like costing
12:03
Much more object oriented than Java will ever be well will ever be no, I'm not sure Let's see what we have in the future. So that's already good stuff and then Oof Ray-fied generics, right
12:22
Who has been bitten by the lack of ray-fied generics already? Yeah, so I have a collection of thingy and a collection of foo and well at one time you have nothing So here the trick when you do Java
12:45
Is to pass the class so here is taken from spring when you get a bean you say hey I want a bean of class whatever and then you will get the whatever Okay, how can we do it in Kotlin?
13:02
Kotlin has this ray-fied T So when you call a get bean you can pass The type that you want and It will get you that it's a string if we are a bit
13:28
Like tricky we can do this and it's a list of string And if you tell it because it's all about compiling because in the end, of course, the byte code is still the same
13:42
The byte code must be compatible with Java byte code. There is no ray-fied generic in the byte code So it's just about compiling and here we can say Either we set it here in the signature when we call it or we tell that type Of that S is of type string and we will get a string and I think that's pretty amazing
14:09
The only thing that we need to do Is do this We need to tell it's ray-fied Sorry
14:20
Again we need to tell it's ray-fied and for ray-fied you need to have inline Why because as I mentioned there is no trick At compile time it will actually replace the code It won't be a call. It will be just copy pasted and so it knows which type you are and in the end
14:49
So I still have a bit of time we can do like really really fun stuff I will Yes fun stuff
15:05
okay, I will create a Function beans and For the moment, I will return any okay, so some syntactic sugar. I don't think it's really important I don't think it makes me want to use Kotlin. It's just easier to use it like
15:25
Either you return the type like this Here any idiots can understand it returns any
15:42
So the Kotlin compiler is not an idiot, but the Java compiler is On one-liner, it doesn't make any sense to specify the type explicitly every time but still the Java compiler requires you to do it Makes no sense
16:00
Okay Sure sure if if for whatever reason if for whatever reason you want to specify what it is because it might be complex But then if it's complex, perhaps it doesn't belong to one-liner then you can still are a lot to Especially if you are using if you return a concrete implementation and you want your signature to be an interface. That's perfect
16:27
Okay And now I will have a class which I will call Let's say beans DSL, okay there I want this to return
16:42
Beans DSL Okay, so now I can write something like this. I can have a main function main Main, okay private static void name. Okay, and I can call the beans function beans function great
17:02
I have nothing mind-blowing Now what I can do is I want to write something like this Okay, so I want to write something like this. I will just use the compiler Okay, so I create
17:23
Here I accept a parameter that takes nothing and returns units Here what I can do is normally I would write something like this But in Kotlin if the last argument is a lambda you can move it outside the parentheses
17:42
So that's what I did before and then here if there is no arguments and there is a lambda I can remove the parentheses Good Now I can add The bean method inside and I can say that I can actually
18:08
Call the bean method on the bean DSL. So here I can do something like this and What is it telling me and resolve method beans, so I still have an issue
18:24
Yes up little trick Yeah, sorry, that's a life coding completely. I thought I would be less fast. Okay, and
18:45
Methods is generic, right so we can say it's of it accepts the type T and I don't remember how it's written. So I will be doing my stupid stuff
19:06
line functions or here in line fun raffite T Here and it returns it and if you continue like this you can have
19:20
this kind of stuff So here this is the spring boots Kotlin DSL so you say this will create beans each been you can define either through a lambda or directly and Through the refined stuff
19:40
here you see the product handler actually requires two dependencies and At compile time it knows that it requires. I don't know a foo and a bar So because those ref method are raffite For generics it knows it needs to care the ref to call the ref method
20:02
Calling getting a foo and a bar and it will inject them so here you have the magic at compile time and at runtime, it's the usual spring boot stuff and I believe it makes my Code much easier to read. Of course, you need to understand the trick
20:21
Every time you need to be very explicit in the beginning because you like a lot of context when you have the context Then it makes your stuff much much easier and That's all I don't want to bore you with more details You can follow me on Twitter You can follow me on masterdom because well, you don't know what will happen to Twitter and the other talk was not about Apache
20:45
API 6 you can Check Apache API 6 which makes my job so much easier and I can come back here to talk about unrelated stuff Is there any time for questions there is