Kotlin in the real world – Astrid Novicky

I’m Astrid I’m a developer in thought works in in London and I want to talk to you today about coupling in the real world Antti 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 were the advantages of Kotlin what were the disadvantages and oh so why did we use it and we’ll be 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 in May when Forex and the mentoring charity 1 million enters are sort of met and discussed the idea for cooperation so 1 million mentis is the uk-based charity it’s a mentoring charity I’m gonna 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 there was a wordpress website with a PHP back-end and it was a lot of fun problems with security day as well as for example the database was publicly available if you knew the endpoint you could just sort of call it and a couple of other sort of problems with maintenance for example something else that 1 million mantas wanted is they wanted improved workflows and processes and this is where I’m sort of going to explain to you what 1 million enters is actually about so 1 million enters is a charity which connects mentees mentees are students between 14 and 24 years they go to school they go to universities in any some kind of education and they usually the aspire to a certain career they have their they’re high achievers and really good at the courses but they are often from lots of privileged backgrounds so you don’t have the network’s they don’t have the tools to sort of know what you have to do to sort of get into a cyber career and this is where the mentors come in and Dementors 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 there the mentoring coordinators so they sort of connect to mental wave demon teas you can see in a pictures there are three mentees and one mentors and usual ratios one two three I want one mentor two one up to three Monty’s basically something that was very challenging about a year ago was this matching of mentors and mentees was 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 reach and if the mentors have been allowed to work with under eighteen year olds so we created a mentoring platform during the last year for 1 million mentis 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 a different criteria that you want and are then select Dementors which are available match them to mentees so this is what the mentoring coordinators are doing and then generate this year we did actually go live with the new platform we went live first in Wales in Cardiff and have now successfully matched 100 mentors if any of you are interested in becoming mentors they it’s already possible to sort of sign up in Manchester area and West Midlands and we’ll also go live in London are soon so let’s now talk about the actual challenge that we’re facing because as we all know filter X is a it’s a delivery companies is a not norther charity but a consultancy but we did this project pro bono for Fortis 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 our type of people later on we got one more developer which was great what we did have though is many many volunteers we had hack nights every Tuesday and 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 there were sort of more assigned to client projects or food workers which were sort of in between projects this obviously required a lot of on-and-off boarding so what we needed is a system and languages which I’d already known by the developers or which is easy to pick up and it’s just sort of what brings us to the architecture and the system that we were actually building so we had a UI which was sort of easy when we build it in react with Redux saga and react is a very common framework so a lot of thought workers did already know it and and could contribute easily then we have a mochila monolith and at first we thought that we might just write it in Java voice ring boot because that again is such a you know well known programming language and a lot of thought or a goose to know it but then we thought hmm how do you develop us work developer Securi us and developers want to learn new new stuff they want to be intrigued and so we decided to use Scotland instead we use Scotland whispering boot a well-known framework at this point back then we all hadn’t really used Scotland before and let them brings me to the question why why do we use cotton oh why did we decide to use it before before I’m gonna sort of elaborate on this question I want to give you a brief introduction to two Kotlin so Dee the project of cotton was sort of first unveiled in 2011 cotton was built by chat brains which also famous for the idea IntelliJ editor then in 2016 there was a first release and in 2017 there was a second release which sort of also allowed cold sharing between a JVM platform and a JavaScript platform 2018 was in a second release which was allowed asynchronous programming yeah Cortland is known or is sort of the the idea behind it is the a 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 it should have make it easier for the developers the source code is now so it has reached critical mass the compiler is now written in couplings it’s I don’t know if you can actually see but now 82% are written in coupling 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 of you would like to contribute this is the link down here Kotlin as well this is very impactful on a global food works scale so it’s part of the technology raid which is sort of a guide that Felix publishes every four months I think and it’s sort of yeah it’s an opinionated guide is sort of that technologists that we use that we implement globally and as you can see here Kotlin is climbing I would still encourage to spell climbing with C even if you’re very big coddling fans but you can see in this pink circles these are sort of the different content frameworks which are being currently used at ThoughtWorks globally okay 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 of over the process of developing the application it is relatively easy to pick up for beginners but also for Java and c-sharp developers what’s quite fascinating about it is that it stands very much in between I know being in a real language being a functional language or implementing some functional concepts as well initially it was developed to be in a ruby language but it sort of in a lot of ways lends itself quite nicely to to write some functional code too and it is well documented a lot of disk 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 Catalan language website but copling is fully until variable-width 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 into a purple which i think is probably one of the most impactful features about cuddling 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 cotton it makes it very easy to start and the reason for that is is that you can completely mix and match your Java your legacy Java code with

maybe a new Kotlin code without losing any functionality so I’ve built this very this is completely meaningless application here it’s a HelloWorld application but it says HelloWorld in Java and it also says it in coupling oh yeah nice to concede here so basically I used I used maven for it so it’s very easy to setup I added my Java dependency I added my Kotlin dependencies and then I wrote a Java service which basically just is a printout HelloWorld in Java and the complan is the same in Cortland and I have an application class where basically call though 50 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 multi-purpose and then you might say could we build an API with it so we mainly used it as a 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 is that the Coughlin community is really diverse so the coupling community consists of Android developers of sort of JVM maybe Java developers and 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 just because I write about coupling and that’s I think something that’s that’s really great um then cuddliness 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 that is sort of very popular already which is which is great I think so we use we use Gradle to build application you can use we use maven anent so you don’t basically have to use something maybe new and exploratory frameworks we use spring for application you could also use vertex or Kate or if you want something more coddling specific but again it is not necessary which sort of makes the learning curve not as steep because you can sort of use um a framework that you might already know for object relational model we use hibernate and testing works perfectly fine with JJ you need and more key to that’s that’s mojito for Kotlin so if we look at testing so if another great feature about cotton so this is the valid test name so you can have spaces in tests it kind of literally any kind of character did you want which you want in your test them you just need to pack ticks around it and this is basically how we use mojito the difference here is syntactically that you would use whenever instead of when people sometimes start so you should clear assume when they write too many tests but that’s okay then as well editors it obviously works really well with IntelliJ as it’s the same inventors basically and was really great for people who would like to build a culling application but he just don’t want to write any code and could 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’s support for debugging all the tooling after source code in IntelliJ as well and sort of look at the source code for documentation the same as possible in eclipse as far as fully supported Visual Studio code syntax highlighting and their plug-ins for them and Emacs to use cotton 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 immutable types of all will be immutable var will be mutable and that is quite interesting because it sort of makes you choose so it’s not like for example in Java where you maybe create string city equals and you’re not necessarily aware on about it so sort of you don’t make this decision consciously on whether something is mutable or nothing codling you’re sort of forced to because you have to decide do I want to use var or want to use eval then also we have type inference or the first two lines basically the same as I know there are different cities but the definition is the same so you don’t have to spell out string it sort of in first 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 overwrite if you want to so this is sort of all still in scope of the property declaration so here I’m overwriting the setter and 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 may be a bit of

a subtle difference or may be a bit of a subtle hint here is there by default immutable so a list of would be immutable where it’s mutable list of will be the mutable type so it sort of lends itself again subtly to write maybe a bit more functional or a bit more immutable code then we have data classes data classes sort of come with there they’re sort of one-line declarations of a class but it also comes with a lot of sort of boilerplate code which is reduced so often they are being used for stateful classes which don’t necessarily operate on there methods but it doesn’t have to be so they have to sort of the full capability of any any other class what they do have is they come with getters and setters with hashcode and equals already overwritten with a two string method and also ever copy method this obviously leads to a really good readability and and also cohesion in terms of that you can so you can have multiple of these classes in one file which yeah makes it it 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 mentor class here and this is just to find out how much boilerplate code reduction there actually is and how much more concise code Lanier’s to to Java and here and that’s that gets I think personally just quite impactful for developing our application it affected our our architecture in a way so we use supports in adapt this architecture so we had this modular model if we have our domains of an application and the 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 clean and I think the Kotlin did help us with that again maybe not as subtly as sort of leading us to to write more functional or more immutable code but just in a way of being so much more concise and and sort of also defective we can have multiple classes in one file just led to us having a very clean domain and sort of and this is our data model and having all of our annotations and an hour or M annotations in in the data model so we have multiple of these actually one-liners online classes in in our data model and so we were never tempted to actually write our annotations in our you know domain just because you can have multiple of them in one file and it’s just so much more concise then functions so in Catalan you can have a write functions without needing them to be within classes which is which is great so Kansas just have a file with with a couple of functions helper functions in there it’s just very easy to write and setup functions are first class citizens there is a function type functions can be arguments of other functions can be returned for functions and functions can be sort of set to two variables then we can see here we also have lambda function so basically all all three of these are sort of the same the same function have the same functionality this is sort of the long way of sort of writing it and this would be like the D lambda expression what we see here here we infer sort of the type from the property here we would infer the type from the lambda expression which would make it even more concise it’s just quite simple to you so I think and that’s the main point here then and it is something that I actually just added to my presentation because a lot of Java developers told me that the absolutely love this fact we have named the default arguments yeah which is it’s just again just something that 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 in Kotlin so basically the idea of keeping packages separate work or separately works a bit different and Kotlin to maybe other languages so it’s more for modular approach so what what Kotlin has instead of package privacy’s it has visibility modifiers so you can sort of labor classes as internal as public as private as protected and that’s sort of the idea of how you would use encapsulation in in Kotlin but yeah it is 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 about package privacy okay let’s talk about the last point safety so basically

there are no more pointer exceptions in Cortland and when I say no no planned exceptions that I mean obviously yes there are more point exceptions but only if you explicitly frodan we’re gonna talk about null safety that’s sort of the first point then the null point exceptions and then this exclamation and question marks and Andy Albus 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 an all safety first so basically we have a compile-time nor safety so if I set this city string Manchester for set this to law then it will complain because string sort of needs to be a string and can’t be nor 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 inaudible type or actually there are innumerable types we which you just define with the question mark sort of behind the type there will be inaudible type so this is compiled and runtime this can be basically null as well some of you some of you might be Java developer some might say oh there is an optional in in Java that can be used so this is this is some Java code here optional so I’m actually writing Java code at the moment on my 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 sort of only provides run time safety to sort of compare those two are used IntelliJ to translate my code into Catalan and there is also an optional rap playing katli and if people would want to use this but the nullable types are usually seen as more idiomatic then let’s talk about null pointer exceptions here basically I’m throwing a null point exception and this would be the only way in Kotlin if you want a null point exception to be thrown at it is thrown so I have validate and create of account details I have an account eater which can be unknowable I know it it doesn’t make a lot of sense because then it is null and then I throw a null point exception but just to show you this is how you for no pointer exceptions basically then the Elvis operator we have the lovely picture again so basically here you find Elvis operator and what we are doing here is we’re saying account details if the account details do exist so this is the question mark it can be no then look at the detail so we’re accessing the detail if account details exist and if a count detail detail is not know then account is gonna be set to account it’ll store detail otherwise we will print this line this is sarong so basically I was operate as a ternary operator using sort of the inaudible time type as discriminator then we also have a DT so a crib a acrobatic data type so we strove to sort i’m pattern matching this is something that also exists in scala and basically we have sealed class in Copelan which means it’s very similar the ideas were similar to an enum in the end so we have a we have a sealed class user and if we want to create a new user this user has to be either mint or mentoring coordinator can’t be anything else which basically makes its site is safe on compile time as well so it can’t create anything else apart from a mentor or mentoring coordinator and the way to sort of then test it or did the way to actually to bring this further take this further would be with this when is sort of pattern matching approach if it is a mentor and we print a mentor and if it is a mentor occur on it so we do something different and it’s some sort of dB the idea behind this then the question would we use Coughlin again yes we would use it again so basically the experience that we had with cotton 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 maybe with Java or C sharp 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 Laurie knowledge-sharing from sort of the different platforms and i think yeah which has got a really good vibe going with um using 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 codling so the first one is actually just the official Kotlin language documentation the second one is a playground so it’s like sort of

like a rap or so so you can just use codling online and it compiles and the third one is code link Oh Cowen’s I actually never used it but a lot of people have told me that it’s that is really good to sort of start learning cuddling but then again I think if you have any language background on oh no and another jvm language for example you will find Kotlin are 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 in opie 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 that’s pretty good as well okay and this was it from me from my side so thank you very much and stay curious and write some Kotlin [Applause] you