Ce diaporama a bien été signalé.
Le téléchargement de votre SlideShare est en cours. ×

No excuses, switch to kotlin

Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Chargement dans…3
×

Consultez-les par la suite

1 sur 30 Publicité

No excuses, switch to kotlin

Télécharger pour lire hors ligne

Are you stuck in the Java world? I’ll share my story about convincing my team and the client of the benefits of Kotlin. Furthermore I’ll delve into how we migrated an existing Java Android app, with 300k active users, to Kotlin.

Even if you have never seen Kotlin before, come and see how you will create better apps with this modern and elegant language. At the end of this talk you’ll be able to convince your team / client why it’s a great to use Kotlin.

The power of Kotlin can be leveraged everywhere you use Java, since it compiles to JVM bytecode. So even if you’re not an Android developer, check out this session to get acquainted with Kotlin!

No excuses: switch to Kotlin 

Are you stuck in the Java world? I’ll share my story about convincing my team and the client of the benefits of Kotlin. Furthermore I’ll delve into how we migrated an existing Java Android app, with 300k active users, to Kotlin.

Even if you have never seen Kotlin before, come and see how you will create better apps with this modern and elegant language. At the end of this talk you’ll be able to convince your team / client why it’s a great to use Kotlin.

The power of Kotlin can be leveraged everywhere you use Java, since it compiles to JVM bytecode. So even if you’re not an Android developer, check out this session to get acquainted with Kotlin!

No excuses: switch to Kotlin 

Publicité
Publicité

Plus De Contenu Connexe

Diaporamas pour vous (20)

Les utilisateurs ont également aimé (20)

Publicité

Similaire à No excuses, switch to kotlin (20)

Plus récents (20)

Publicité

No excuses, switch to kotlin

  1. 1. NO EXCUSES, SWITCH TO KOTLIN
  2. 2. NO EXCUSES, SWITCH TO KOTLIN t Thijs Suijten Mobile developer @ Q42 @tsuijten
  3. 3. GETTING EVERYBODY ONBOARD TEAM Q42 CLIENT
  4. 4. val s1: String = "Hello" // Immutable variable
 val s2 = "how are you" // Type is inferred Variables var s3 = "John" // Mutable variable
 s3 = "Thijs" // Assign a different value // w00t! String Interpolation
 println("$s1, $s2 $s3?") //> Hello, how are you Thijs?
  5. 5. fun reformat(str: String, normalize: Boolean = true, upperCase: Boolean = true): String {
 ...
 } Functions reformat("Hello world")
 reformat("Hello world", false, false)
 reformat("Hello world", upperCase = false) // Single-Expression functions fun twiceTheFun(x: Int) = x * 2
  6. 6. class Person(val firstName: String, 
 val age: Int, 
 val locale: Locale = ENGLISH) {
 fun sayHello() = println("Hi $firstName!")
 } Classes val thijs = Person("Thijs", 34)
 thijs.sayHello() // Hi Thijs! class Cat(name: String) {
 val name: String = name
 }
  7. 7. val x: String? = getSomeOptionalString()
 x.length // Does not compile. Null safety / optionals val y: String = null // Does not compile. if (x != null) {
 x.length // Compiles!
 } // Safe call
 val optLength = foo?.bar?.length // Returns Int?
 
 // Elvis operator.
 val length = optLength ?: -1
  8. 8. Data classes class User(val name: String, val age: Int) val thijs = User("Thijs", 35) println(thijs) // User(name=Thijs, age=35) val bday = thijs.copy(age = 36) // Named params!
 println(bday) // User(name=Thijs, age=36) data
  9. 9. Data classes public class User {
 private String name;
 private int age;
 
 public User(String name, int age) {
 this.name = name;
 this.age = age;
 }
 
 public String getName() {
 return name;
 }
 
 public void setName(String name) {
 this.name = name;
 }
 
 public int getAge() {
 return age;
 }
 
 public void setAge(int age) {
 this.age = age;
 }
 
 @Override public boolean equals(Object o) {
 if (this == o) return true;
 if (o == null || getClass() != o.getClass()) return false;
 
 User user = (User) o;
 
 if (age != user.age) return false;
 return name != null ? name.equals(user.name) : user.name == null;
 }
 
 @Override public int hashCode() {
 int result = name != null ? name.hashCode() : 0;
 result = 31 * result + age;
 return result;
 }
 
 @Override public String toString() {
 return "User{" +
 "name='" + name + ''' +
 ", age=" + age +
 '}';
 }
 } // Remember this?
 data class User(val name: String, val age: Int) public class User {
 private String name;
 private int age;
 
 public User(String name, int age) {
 this.name = name;
 this.age = age;
 }
 
 public String getName() {
 return name;
 }
 
 public void setName(String name) {
 this.name = name;
 }
 
 public int getAge() {
 return age;
 }
 
 public void setAge(int age) {
 this.age = age;
 }
 
 @Override public boolean equals(Object o) {
 if (this == o) return true;
 if (o == null || getClass() != o.getClass()) return false;
 
 User user = (User) o;
 
 if (age != user.age) return false;
 return name != null ? name.equals(user.name) : user.name == null;
 }
 
 @Override public int hashCode() {
 int result = name != null ? name.hashCode() : 0;
 result = 31 * result + age;
 return result;
 }
 
 @Override public String toString() {
 return "User{" +
 "name='" + name + ''' +
 ", age=" + age +
 '}';
 }
 }
  10. 10. Functional / lambas val sum = listOf(1, 2, 3, 4, 5) // 1,2,3,4,5
 .filter { number -> number < 5 } // 1,2,3,4
 .map { it * 2 } // 2,4,6,8
 .sum() // 20 listOf("Zack", "Bob", "Thijs", "Julie", "Rob")
 .filter { it.length > 3 } // Zack, Thijs, Julie
 .sorted() // Julie, Thijs, Zack
  11. 11. Collections // Interface List<String>
 val fruits = listOf("Apple", "Pear", "Orange")
 fruits.add("Strawberry") // Compiler error // Interface MutableList<String>
 val veggies = mutableListOf("Kale", "Spinach", "Lettuce")
 veggies.add("Tomato")
 veggies += "Cucumber" // Operator overloading
  12. 12. When statement // Java
 if (firstName.equals("Dan")) {
 person.setTeam(programmers);
 } else if (lastName.equals("Jamie")) {
 person.setTeam(designers);
 } else {
 person.setTeam(others);
 } // Kotlin
 when {
 firstName == “Dan" -> person.team = programmers
 lastName == "Jamie" -> person.team = designers
 else -> person.team = others
 }
  13. 13. When statement // Java
 switch (firstName) {
 case "Dan":
 case "Jay":
 person.setTeam(programmers);
 break;
 case "Jamie":
 person.setTeam(designers);
 break;
 default:
 person.setTeam(others);
 } // Kotlin
 when (firstName) {
 "Dan", "Jay" -> person.team = programmers
 "Jamie" -> person.team = designers
 else -> person.team = others
 }
  14. 14. When statement when (x) {
 1 -> print("x = 1")
 is Int -> print(x + 1)
 is String -> print(x.length + 1)
 is IntArray -> print(x.sum())
 in 10..20 -> print("Between 10 and 20")
 } val outcome = when(x) {
 1 -> "x = 1"
 "Hello" -> "is it me you're looking for?"
 in 10..20 -> "Between 10 and 20"
 }
  15. 15. Extensions // Java
 StringUtils.capitalize(AddressUtils.extractStreet(address)) // Kotlin
 address.street.capitalize() // Extension function
 fun String.capitalize() = StringUtils.capitalize(this)
 
 // Extension property
 val String.street: String get() = AddressUtils.extractStreet(this)
  16. 16. Kotlin Android extensions <TextView android:id="@+id/greeting"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"/>
 
 <Button android:id="@+id/button"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Click me!"/> override fun onCreate(savedInstanceState: Bundle?) {
 //… 
 greeting.text = "Hello"
 button.onClick { toast("Button clicked!") }
 }
  17. 17. Interop Demo
  18. 18. Who is using Kotlin
  19. 19. NO EXCUSES, SWITCH TO KOTLIN
  20. 20. .. thijs@q42.nl / @tsuijten try.kotlinlang.org

×