So thank you for coming. If for some reason we cannot fully
cover a programming language in ten minutes, come to office hours. I'll be here at 11:20.
So come and ask questions. Kotlin is a language developed by JetBrains. They develop a lot
actual tools that are really high end. It's released under the Apache 2.0 license so you
can go and commit changes to this language. I mean there's a process, but you can do it.
They really welcome people. It's designed as a general purpose language. You might have
heard it's good on Android or it's good on this, but it really is meant to be a general
purpose language, because it targets Java 6, Java 8. You can target iOS if you want,
so that part's in beta but the beauty is you can have a set of models or behaviors and
you can write it in one language. It's been designed to avoid entire classes
of detects. It's trying to make things that are bad or not optimal, it makes them hard
to do or impossible to do and by that I mean there's things like null safety. Like I'm
a Java programmer before I was a it Kotlin developer.
It's 100% interoperable with Java, so if you have -- there's 23 years of languages -- of
Java libraries that you can pull right in, you don't have to change them in any way,
they just pull right in, you can use them completely seamlessly. And the language itself
is pragmatic. When I look at some syntax with you, you can be like, hey, that's stolen from
C#, yes, it totally, totally is, because they're taking what they learn from other things and
rethinking them. And one of the things you hear about Kotlin is it's a better Java and
it's true. So they're taking things with the benefit of hindsight and thinking what can
we do to make it better and you'll see that in the language.
This is just the one I skimmed my copy. And I thought this is really interesting, because
none of these things are possible in Kotlin. I don't have to go out of my way to code around
them in Kotlin like I do in java. So one of the things when I pick a new language or library,
I like to have some herd immunity. I don't like to be ut only one out there with it,
because I just don't. So I'll just say that Android recommends that you use Kotlin now.
If you're a Java backend developer, you know about spring. Spring framework is probably
50% of the Kotlin backend marked. So you're not alone when you're using this. Is and so
we're software developers, let's look at some syntax, so let's start with variables and
values. Variables mean something can change and values mean something can't change. So
24 in this place I've got a string it's Chicago. I can reset that to Austin. That's totally
fine. On the other hand I've got a value, it's a name. But if I set it to my daughter's
name, I can't do that. What I really want you to look at is these compilers here. It's
pretty obvious we're looking at string, so in Kotlin I can leave those off. That's a
lot less noise, a lot less visual pollution. It's just inferred, I don't have to talk about
if I don't want to. The second thing is null safety. So here I
have a string it's not allowed to be null. I can't set that to null. On the other hand,
city, because it's got a question mark on the end of string, I'm allowed to set that
to null. Now, null is not really a problem, it's allowing programmers to chase the null
preference that's the problem. So Kotlin gives us some of tools to deal with that. This is
the safe traversal operator. So in the case that city is null, that whole expression will
evaluate to null. And pairing that with Elvis, if apparently
if you turn it on inside, it looks like Elvis, and I'm like the only person in the world
who doesn't say that, but hopefully you do. This can be arbitrarily complicated. I have
a simple example here, but these are the simple simple little tools that Kotlin gives you
to avoid this null problem, like the types that are not allowed to be null and the tools
to work with nulls. I'm going to show you the switch thing here,
or had this when thing. This is like Kotlin's version of switch. So Java's version of switch
was pretty much a copy of C.'s version of switch which is really just a jump table and
it doesn't really do much. It's just a lookup and you go to a piece of code and look at
T if you're comfortable with Scala or some other languages, it's not quite as powerful
as those. So I think this is a really good example of how Kotlin is trying to be pragmatic
of that it's not taking everything, because it's not going to be hard to read or hard
to comprehend. And that's one of the things I like about Kotlin is the ability to say,
this is going too far, we think for now, this is to keep it easier to read.
Let's go back to Java. I've got this person, first name, last name, that's good, right?
We can just check that in. Nobody's going to complain. No, I need all the setters and
getters, so I'm going to get my editor to write me setters and getters and constructors
and equals and hash code and then two strings and when we get to this point, I've written
three lines of code, right and my editor has written 47 lines of code and the other problem
with this is when somebody pulls this code up, they have to inspect it to make sure I'm
not doing something stupid in the getters and setters, like too-upper casing something.
In reality we just want a first time and last name. So in Kotlin we've got this thing called
a data class. It's the same thing as I just hold showed you. It's got to string, cash
code and equals and getters and setters, all built in for me. If I want to override them,
I can override them. But it's simple, I can just look at it and I know there's nothing
weird going on. Now, if we do want extra behavior, that code will be in there, and we'll see
it and look at it. So you're only looking at things that are not adhering to the contract
that we're entering into, right? I don't have to inspect every POJO that somebody is doing
something weird. The next thing is extension functions. I can
write this in Kotlin and I can call it int, called 2.is asks Even. it's a nice little
to add to productivity. These little things they add up. There's no semicolons, there's
one method for structural equality. In nav you've got = = for primitives, in Kotlin you
just say double =, it knows what you're talking about.
These things all add up. Like string interpolation, I don't have to add strings together anymore.
I don't to worry about escaping any of that. This reduces bugs and increases
clarity in the system that I'm developing. Now, one things about is systems that I'm
developing, I don't like to develop them in communities that are negatively if you go
into the Kotlin Slack there's nearly 20,000 people there, the people who designed the
language are there answering the questions, people who write it every day answering questions.
So one of the interesting facts about Kotlin is StackOverflow did a survey years ago. Kotlin
was the second least disliked language. So if you like what I've said, go to play.kotlinlang.org.
The title of the talk is Kotlin right for you? I'll answer the question first, right
for me, because I write far far -- that sucks! I'll stop moving. My boss isn't paying me
to write code. My boss is paying me to provide business value, so what I'm writing is more
directly related to business value and the code I'm writing is way more expressive and
clear to myself and others. I avoid whole classes of defects.
It's just more fun. I enjoy writing it more. It plays well with the tools I already have.
Is Kotlin right for you? Only you can ask that question. I hope I've given you the tools do so today.