Improving code readability with Kotlin extensions

In anticipation of the start of the "Kotlin Backend Developer" course , we invite future students and everyone who wants to watch an open lesson on the topic "Revision of the" 12 factors ": creating a modern microservice on Kotlin".



We also share with you the traditional translation of useful material.






Kotlin Terminology: Extension-Functions and Extension-Properties

When you used an API, did you want to add new functions or properties to it?





( ) , . Java Utils



, , . , .





, Kotlin - -. , . Android Studio , Java. , Android SDK .





, , !





-

, Dog



,  , , .





<!-- Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->

data class Dog(val name: String, val breed: String, val age: Int)
      
      



,  Dog



, , , - . -, , : . this



-, - .





 printDogInformation()



  ,  Dog



.





<!-- Copyright 2019 Google LLC. 
   SPDX-License-Identifier: Apache-2.0 -->

fun main() {
  val dog = Dog("Jen", "Pomeranian", 13)
  dog.printDogInformation()
}
      
      



- Java

- , Java . , , , . - printDogInformation()



Java:





<!-- Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->

DogExtensionKt.printDogInformation(dog);
      
      



- ,

, (nullable). null



-, - nullable- null



. printInformation()



, , .





<!-- Copyright 2019 Google LLC. 
   SPDX-License-Identifier: Apache-2.0 -->

fun Dog?.printInformation() {
  if (this == null){
    println("No dog found")
    return
  }
  println("Meet ${this.name} a ${this.age} year old ${this.breed}")
}
      
      



, null  printInformation()



.





<!-- Copyright 2019 Google LLC. 
   SPDX-License-Identifier: Apache-2.0 -->

fun main() {
  val dog : Dog? = null
  dog.printInformation() // prints "No dog found"
}
      
      



-

, , . - isReadyToAdopt, , 1 .





<!-- Copyright 2019 Google LLC. 
   SPDX-License-Identifier: Apache-2.0 -->

val Dog.isReadyToAdopt: Boolean
get() = this.age > 1
      
      



- ,  Dog



.





<!-- Copyright 2019 Google LLC. 
   SPDX-License-Identifier: Apache-2.0 -->

fun main() {
  val dog1 = Dog("Jen", "Pomeranian", 13)
  if(dog1.isReadyToAdopt){
    print("${dog1.name} is ready to be adopted")
  }
}
      
      



-

- . - , - , -, , , , . ,  toUppercase()



, (String),  convertToUppercase()



.





, , , , . , , - .





 printDogInformation()



Android Studio. Tools/Kotlin/Show Kotlin Bytecode (/Kotlin/ - Kotlin) Decompile ().  printDogInformation()



  :





<!-- Copyright 2019 Google LLC. 
   SPDX-License-Identifier: Apache-2.0 -->

public static final void printDogInformation(@NotNull Dog $this$printDogInformation) {
  Intrinsics.checkParameterIsNotNull($this$printDogInformation, "$this$printDogInformation");
  String var1 = "Meet " + $this$printDogInformation.getName() + ", a " + $this$printDogInformation.getAge() + " year old " + $this$printDogInformation.getBreed();
  boolean var2 = false;
  System.out.println(var1);
}
      
      



- , -. -. โ€” .





, . , , .





:





  • .





  • - .





  • !





!






"Kotlin Backend Developer".



" ยซ12 ยป: Kotlin".












All Articles