Importance of Natural Resources

Kotlin in the real world – Astrid Novicky

Hi I’m Astrid I’m a developer in
ThoughtWorks in London. And I want to talk to
you today about Kotlin in the real world. And the idea behind his talk
is to not only introduce a new modern
programming language, but to also give you a more
of a personal sort of idea of what it was like to learn it
what it was like to teach it as well was it easy was it hard. What really advantages
are there of Kotlin. What were the
disadvantages and also why did we use it and
would we use it again. So first I need to introduce
you to the challenge that we were facing. So it was last year. I think in May
when ThoughtWorks and the mentoring charity
one million mentors sort of met and discussed
the idea for cooperation. So one million mentors
is a UK based charity. It’s a mentoring charity
I’m going to explain that in more detail a bit later. So about a year ago, they
had an internet platform, but it was it was a bit
flaky, it wasn’t scalable. It wasn’t very secure. So it was a WordPress website
with a php backend and it had a lot of problems
with security, as well as, for example, the database
was publicly available if you knew the end point,
you could just sort of call it and a couple of other sort
of problems with maintenance for example. Something else
that one million mentors wanted is they wanted improved
workflows and processes. And this is where
I’m sort of going to explain to you what one
Million mentors is actually about. So one Million Mentors is
a charity, which connects mentees and
students between 14 and 24 years. They go to school,
they go to universities in any kind of
education and usually they aspire to a certain career
They are high achievers. And really good at the courses. They’re often from not so
privileged backgrounds. So they don’t have the networks. They don’t have the
tools to sort of know what they have to do to sort
of get into a certain career. And this is where the mentors
come in. And the mentors they can be professionals. They can be academics. They have a certain
skill set and they are sort of willing to
share this skill set The mentoring coordinators
or the catchy name would be MCs they
work in schools and universities and they
are the mentoring coordinators. So they sort of
connect a mentor with The mentees you can see
in the pictures there are three mentees
and one mentors and usual ratios 1, 2, 1,
1 mentor to one up to three mentees basically
something that was very challenging about a year ago
was this matching of mentors and mentees were still
done completely manually and offline in Excel sheets. So it often took up two months
to sort of match mentors and mentees regarding
skillset region. And if the mentors
even allowed to work with under 18-year-olds. So we created a
mentoring platform during the last year
for One Million Mentors and included profile creation. It included online
training for the mentors and it also included a
manual online matching. So now, it’s a lot easier. You can basically filter
for the different criteria that you want. And then select the
mentors, which are available and match them to mentees. So this is what the mentoring
coordinators are doing. And in January this
year, we did actually go live with the new platform. We went life first
in Wales in Cardiff and have now successfully
matched 100 mentors. If any of you are interested
in becoming mentors one day it’s already possible
to sort of sign up in Manchester area
and west midlands and we’ll also go
live in London soon. So let’s now talk about
the actual challenge that we’re facing because
as we all know ThoughtWorks Is a
delivery company. It’s not a charity
but a consultancy. But we did this Project Pro
Bono for this charity. So we didn’t have sort
of the quantitative sort of the amount of team
members that we would usually have in a team. So we started out
with four and then later on with five core
team members two of us were developers and two were
sort of business product delivery type of people. Later on. We got one more developer,
which was great. What we did have, though, is
many, many volunteers. We had hacknights every Tuesday
and later on every Wednesday as well in the evenings. So people that came in
and wanted to help us sort of building
building this platform. We also had loads of
people come in to help us for a couple of days
up to a couple of weeks if they were sort
of not assigned to client projects
or ThoughtWorkers, which were sort of
in-between projects. This obviously required a
lot of on an off boarding. So what we needed is a
system, and languages, which are either already known by the developers or which
are easy to pick up. And this is sort
of what brings us to the architecture and a system
that we were actually building. So we had a UI which
was a sort of easy win we built it and
react with redux-saga and react is a very
common framework. So a lot of ThoughtWorker
did already know it and could contribute easily. Then we have a modular monolith. And at first, we thought
that we might just write it in Java with SpringBoot
because that again, is such a well known programming
language and a lot of ThoughtWorkers do know. But then we thought,
how do developers work? Developer are
curious and developers want
to learn new stuff. They want to being intrigued. And so we decided to
use Kotlin instead, we used Kotlin SpringBoot
a well-known framework. At this point back then
we all hadn’t really used Kotlin before and that
brings me to the question why. Why did we use
Kotlin or why did we decide to use it. Before I’m
going to sort of elaborate on the question I want to
give you a brief introduction to Kotlin. So the
project of Kotlin Was sort of first
unveiled in 2011. Kotlin was built by
Jetbrains which Is also famous for
the idea intellij editor. Then in 2016 it
was the first release and in 2017 there was a second. Release which sort
of also allowed call sharing
between JVM platform and a javascript platform. 2018 was then a second. release which allowed
asynchronous programming. Yeah Kotlin is known or
is sort of the idea behind it is the better Java. So they wanted to
develop a language, which compiles and is equally
fast runs as fast as Java does. But implementing a couple of new
ideas and concepts to sort of make it easier
for the developers The source code is known. And it has reached
a critical mass. The compiler is now
written in Kotlin. It’s I don’t know if
you can actually see it. But now 82% are written in
Kotlin now about two years ago, it was like 50% of the
source code that was actually written in Kotlin. It’s an open source project. So if any if you would
like to contribute. This is the link down here. Kotlin as well. is very impactful on the
global ThoughtWorks scale. So it’s part of the Technology
Radar, which is sort of a guide that ThoughtWorks publishes
every six months. I think. And it sort of
an opinionated guide. of the technologies that we
use that and implement globally. And as you can see here. Kotlin is climbing
still encouraged climbing with a C even if
you’re a very big Kotlin fans. But you can see in
the pink circles. These are the different
content frameworks which are being currently
used at ThoughtWorks globally. But now let’s go back to
my initial question why. Why did we pick Kotlin. Not
only because it’s exciting and new, but also
because and that’s something that we actually
figured out sort over the process of
developing the application. It is relatively easy to
pick up for beginners, but also for Java and C# developers. What’s quite
fascinating about it is that it stands
very much in between. And being an oop language and
being a functional language or implementing some
functional concepts as well. Initially it was developed
to be in a OOP language, but it’s sort of
in a lot of ways lends itself quite nicely to
write some functional code too. And it is well documented.
A lot of this documentation also comes from the
Android community. So here are a couple
of benefits listed. This is maybe not the
most objective view because I’ve taken a slide from
the official Kotlin language website. But Kotlin is fully
interoperable with Java. It is very tool friendly
has a concise syntax and it is meant to be safe. So if we look at the first point
interoperable which I think is probably one of the
most impactful features about Kotlin or maybe
not so much impactful but it sort of lends
itself really nicely. If you have an existing
for example Java code base. And you want to start
implementing Kotlin, then it makes it
very easy to start. And the reason for that
is that you can completely mix and match your java
over your legacy Java code with maybe a new Kotlin
code without losing any functionality. So I’ve build this completely
meaningless application here. It’s a hello world application, but
it says hello world in Java, and it also says in Kotlin. Oh, yeah. Nice you can see it here. So basically, I used
I used maiden for it. So it’s very easy to set up. I added my Java dependency
I added my Kotlin dependencies and then I wrote a
Java service, which basically just as a print
out Hello world in Java and then Kotlin is
the same in Kotlin. And I have an
application class where basically call both of
these services and you can see down here. It sort of runs. And you can completely
mix and match them, which is really nice. Then as well. It is very multipurpose. And then you might say good we’ve built an API with it. So we mainly use it or we did use it as a JVM language
running on the JVM platform, but it also runs in a browser
on Android and as native code. And what’s really great about
that is that the Kotlin community is really diverse. So the Kotlin community
consists of Android developers of sort of JVMs maybe
Java developers and of of web developers and you can
have a really great knowledge share between all of these
groups and you actually learn. I actually learned a bit
about Android development as well because
I read about Kotlin and that’s I think something
that’s really great then Kotlin is
also tool friendly. And I absolutely love
that when I started, so I hope you’ll love it too. You can use a lot of ecosystem. You can use an ecosystem that
that is sort of very popular already, which is great. I think so we use reuse
gradle to build application, you can use
Maven an Ant. So you don’t basically
have to use something maybe new and exploratory frameworks. We used Spring for
our application. You could also use VertX or ktor
if you want something more Kotlin specific. But again, it is not necessary. Which sort of makes the
learning curve not as steep because you can sort of use
a framework that you might already know. For object
relational model, we used Hibernate and testing
works perfectly fine with junit5 and mockito. That’s Mockito for Kotlin.
So if we look at testing, another great
feature about Kotlin. So this is a valid test theme. So you can have spaces
and tests to kind of literally any kind
of character you want, which you want in
your test. You just need to
practice around it. And this is basically
how we use Mockito. The difference here
is syntactically that you would use
whenever instead of when. People sometimes start
singing the Shakira song when you write too many tests. But that’s OK. Then
as well editors. It obviously works really
well with IntelliJ as it’s the same
inventors basically. And what’s really
great for people who would like to build
a Kotlin application, but they just don’t want
to write any Kotlin code, I don’t know why
that should happen. But you could also
write some Java code and just auto translate it in
IntelliJ to Kotlin. There is support for debugging. All the tooling you have the source
code in IntelliJ as well. And sort of look at the
source code for documentation. The same as possible in Eclipse
as well as, fully supported. Visual Studio Code syntax
highlighting and there are plugins for them and emax to use Kotlin. So then let’s talk
about the syntax. It is a very concise syntax. Let’s look at some examples. So what you can see here. Basically so we have
immutable mutable types of val would be immutable. Var would be mutable and
that is quite interesting because it sort of
makes you choose. So it’s not like,
for example, in Java by you maybe create
string city equals and you’re not necessarily
aware about it. So sort of don’t make this
decision consciously on whether something
is mutable or not. And in Kotlin you’re
sort of forced to because you have to
decide, do I want to use Val or want to
use var. Then we have type inference
or the first two lines. Basically, the same as this. I know there are
different cities. But the definition is the same. So you don’t have
to spell out string. It sort of infers the type, and
then another quite interesting feature. I think is that the city
here is actually not a field, but it is a property. So it comes with a getter
and setter which you can override if you want to. So this is sort of all still
in scope of the property declaration. So here I am
overwriting to set tone I’m just saying if the
field is not blank, then the value of the field
is the value. Then we have collections we have lists maps and sets. And again, maybe a bit
of a subtle difference or maybe a bit of
a subtle hint here is there by default immutable. So list of will be immutable
various mutable list of will be the mutable type. So it sort of lends itself
again softly to right. Maybe a bit more functional
or a bit more immutable code. Then we have data classes.
Data classes sort of come with, they are sort of a
line declarations of a class, but it also comes with a
lot of sort of boiler plate code, which is reduced. So often they are
being used for state for classes, which
don’t necessarily operate on their own methods,
but it doesn’t have to be. So they have the full
capability of any other class. What we do have is they come
with getters and setters with hash code and equals
already overwritten with a toString method and
also have a copy method. This obviously leads to
a really good readability and also cohesion in
terms of that, you can. So you can have multiple
of these classes in one file, which makes it
quite easy to read. So again, here a
comparison to Java you do not have
to read this code. So I encourage you not to. But basically this mentor class here is the same as
this mental class here. And this is just to point
out how much boilerplate code reduction there actually is and
how much more concise Kotlin is to Java. And here that’s where it gets,
I think personally just quite impactful for
developing our application. It affected our architecture
anyway, so we use the ports and adapt this architecture. So we had this modular monolith. We have our domains. Have an application
and a framework around. And obviously, we don’t want
to have the framework leaking sort of into our domain layer. We want to keep them
sort of separately and clean. And I think that Kotlin
did help us with that again. Yeah, maybe not as
subtly as a sort of leading us to write more
functional or more immutable code, but just in a way of
it being so much more concise and sort of also
the fact that we can have multiple
classes in one file just led to us having a
very clean domain and sort of and this is all a
data model and having all of our annotations and an
hour or annotations in the data model. So we have multiple of
these actually one liners. This one line classes
in our data model. And so we were not attempted
to actually write down annotations in our domain,
just because you can have multiple of them in one file. And there’s just so much
more concise. Then functions. So in Kotlin, you
can write functions without needing them to be
within classes, which is great. So it can just have a file with
a couple of functions helper functions and dash that’s just
very easy to write and set up. Functions are first
class citizens there is a function type.
Functions can be arguments of other functions, they
can be returned for functions and functions
can be sort of set to variables. Then we can see here, we
also have lambda functions. So basically all three of
these are sort of the same. The same function have the
same functionality it is sort of the long way
of writing it. And this would be
like the lambda expression, which we see here. Here we infer sort of the
type from the property. Here we would infer to type from the
Lambda expression, which would make it even more concise. It’s just quite simple to use. I think. And that’s the main
point here. Then and now there’s something
that I actually just added to my presentation because
a lot of Java developers told me that they absolutely
love this fact. We have named and default arguments. which is just, again, just
something that makes it really, really easy to use in a
way and just very concise and something that’s
maybe more of a point of heated discussions. Is there is no package
privacy in Kotlin. So basically, the idea of
keeping packages separate work or separately works a bit
different in Kotlin to maybe other languages. So it’s more of a
modular approach. So what, Kotlin has instead
are packaged privacies it has visibility modifiers. So you can sort of labour
Classes as internal or as public as private as protected. And that’s sort of the idea of
how you would use encapsulation in Kotlin. It is still a
heated discussion. And it has been for the
last, I think three years. I have followed the threats
and people are quite sort of divided in
package privacy. OK, let’s talk about
the last point safety. So basically, there are no
point exceptions in Kotlin. And when I say, no, NullPointer
Exceptions in Kotlin. I mean, obviously yes, there
are NullPointer Exceptions, but only if you
explicitly throw them. We’re going to talk
about safety. That’s sort of the
first point then the NullPointer Exceptions and
then this exclamation and question marks and
the Elvis operator. Which has its name
from Elvis just in case you didn’t know that’s
where it comes from. But let’s start with
Null safety first. So basically, we have a
compile time null safety. So if I set this city string
Manchester for set this to null, then it will complain
because string sort of needs to be a string and can’t
be null. A way of doing this and sorry there is a bit of
Java code in here too. But we’ll look at the Kotlin
code first, which is down here. The way of sort of creating
an nullable type or actually there are nullable types, which you
just define with the question mark, behind the type
that would be a nullable type. So this is compile
and runtime that can be basically null as well. Some of you might be
Java developers and might say Oh there is an
optional in Java that can be used.. So this is
some Java code here. So I’m actually writing
Java code at the moment on my current project. So I use optional quite a lot. And the difference is
is that it’s a wrapper. So it’s not a compile time. But it’s sort of only
provides runtime safety. To sort of compare those
two I used intelliJ to translate my
code into Kotlin. And there is also an
optional wrapper in Kotlin if people would
want to use this. But the normal types
are usually seen as more idiomatic. Then
let’s talk about NullPointer exceptions here. Basically I’m throwing
an NullPointer Exception. And this will be the
only way in Kotlin if you want a NullPoint
Exception to be thrown as it is thrown. So I have validate and
create of account details. I have an Account Detail,
which can be nullable. I know it doesn’t make a lot of
sense because then it is Null. And then it refers a
NullPoint Exception. But just to show this is how
you throw NullPoint Exceptions. Basically then
the Elvis operator we have the lovely
picture again. So basically here you
find the Elvis operator and all we’re
doing here is we’re saying account details if
the account details do exist. That is the question
mark here it can’t be null. Then look at the detail. So we accessing the detail
if account details exist. And if account details
of detail is not null, then accounting
is going to be set to account details.
Otherwise we will print this line. This is so wrong. So basically Elvis
operators turn the real operator using
sort of the nullable type as discriminator.
Then we also have ADT so
acrobatic data types, always struggle with this
word and pattern matching. This is something that
also exists in Scala. And basically, we have a
sealed class in Kotlin which means, it’s very similar. The ideas is very
similar In the end. So we have a seal class user. And if we want to
create a new user. This user has to be either
mentor a mentoring coordinator An’t be anything else,
which basically makes it safe on compile time
as well. So can’t create anything else apart from a mentor
or mentor coordinator. And the way to sort of
then test it or the way to actually bring
this further would be
with this when is sort of pattern matching approach. If it is a mentor, we print mentor and
if it is a mentoring coordinator it would do something different. And that’s sort of The idea behind this.
Then the question would we use Kotlin again. Yes, we would use it again. So basically, the experience
that we had with Kotlin was we had so many people come
in just for a couple of days just for a couple of hours
in the evenings actually and they could
immediately contribute. I mean, I must say
they could immediately contribute if they had any other
prior experience with Java C# or Scala. It was very easy
to learn in the way that people didn’t have
to sort of go and read some external resources we could
just sort of pair with them, and they would pick
it up very quickly, within a couple of hours. We got a lot of
knowledge sharing from some the different platforms. And I think, yeah, we just
got a really good vibe going on using this language. Then I also have a
couple of links for you in case you got interested. And you would like to
start using some Kotlin. So the first one is actually
just the official Kotlin language documentation. The second one is a playground. So it’s sort of like a rapport
so so you can just use Kotlin online and it compiles. And the third one is
Kotlin. I have actually never used
it, but a lot of people have told me that it’s
really good to sort of start learning Kotlin. But then again, I think if you
have any language background on another JVM
language for example, you will find Kotlin
rather easy. I think something
else to say as well is for beginners it is
relatively easy to learn it as well because people can
focus very much on OOP principles and don’t have to sort
of think about syntax too much. Because I think as
we looked at these examples before Java has in a way such
a convoluted syntax that often as a beginner you’re very much
focused on getting the syntax right. More than maybe looking at
the different principles. So in terms of using
it as a beginner. I think that’s
pretty good as well. OK And this was it
for me, from my side. So thank you very much. And stay curious and
write some Kotlin.

Leave a Reply

Your email address will not be published. Required fields are marked *