Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Klean Code with Kotlin

oshai
November 26, 2019

Klean Code with Kotlin

oshai

November 26, 2019
Tweet

More Decks by oshai

Other Decks in Technology

Transcript

  1. Boilerplate In computer programming, boilerplate code or boilerplate refers to

    sections of code that have to be included in many places with little or no alteration. It is often used when referring to languages that are considered verbose, i.e. the programmer must write a lot of code to do minimal jobs.
  2. IDE

  3. Hello Kotlin! Java public class Hello { public static void

    main(String[] args) { System.out.println("Hello!"); } } Kotlin fun main() { println("Hello!") }
  4. POJO Class Java public class AdCpcData { private int adId;

    private double cpc; public AdCpcData(final int adId, final double cpc) { this.adId = adId; this.cpc = cpc; } public double getCpc() { return cpc; } public int getAdId() { return adId; } public void setAdId(final int adId) { this.adId = adId; } public void setCpc(final double cpc) { this.cpc = cpc; } }
  5. POJO Class Java public class AdCpcData { private int adId;

    private double cpc; public AdCpcData(final int adId, final double cpc) { this.adId = adId; this.cpc = cpc; } public double getCpc() { return cpc; } public int getAdId() { return adId; } public void setAdId(final int adId) { this.adId = adId; } public void setCpc(final double cpc) { this.cpc = cpc; } } equals(), hashcode(), toString()…
  6. POJO Class Java public class AdCpcData { private int adId;

    private double cpc; public AdCpcData(final int adId, final double cpc) { this.adId = adId; this.cpc = cpc; } public double getCpc() { return cpc; } public int getAdId() { return adId; } public void setAdId(final int adId) { this.adId = adId; } public void setCpc(final double cpc) { this.cpc = cpc; } @Override public String toString() { return com.google.common.base.Objects.toStringHelper(this) .add("adId", adId) .add("cpc", cpc) .toString(); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AdCpcData adCpcData = (AdCpcData) o; return adId == adCpcData.adId && Double.compare(adCpcData.cpc, cpc) == 0; } @Override public int hashCode() { return Objects.hash(adId, cpc); } } equals(), hashcode(), toString()…
  7. POJO Class Java public class AdCpcData { private int adId;

    private double cpc; public AdCpcData(final int adId, final double cpc) { this.adId = adId; this.cpc = cpc; } public double getCpc() { return cpc; } public int getAdId() { return adId; } public void setAdId(final int adId) { this.adId = adId; } public void setCpc(final double cpc) { this.cpc = cpc; } @Override public String toString() { return com.google.common.base.Objects.toStringHelper(this) .add("adId", adId) .add("cpc", cpc) .toString(); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AdCpcData adCpcData = (AdCpcData) o; return adId == adCpcData.adId && Double.compare(adCpcData.cpc, cpc) == 0; } @Override public int hashCode() { return Objects.hash(adId, cpc); } } equals(), hashcode(), toString()… 57 LOC
  8. Data Class Java public class AdCpcData { private int adId;

    private double cpc; public AdCpcData(final int adId, final double cpc) { this.adId = adId; this.cpc = cpc; } public double getCpc() { return cpc; } public int getAdId() { return adId; } public void setAdId(final int adId) { this.adId = adId; } public void setCpc(final double cpc) { this.cpc = cpc; } @Override public String toString() { return com.google.common.base.Objects.toStringHelper(this) .add("adId", adId) .add("cpc", cpc) .toString(); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AdCpcData adCpcData = (AdCpcData) o; return adId == adCpcData.adId && Double.compare(adCpcData.cpc, cpc) == 0; } @Override public int hashCode() { return Objects.hash(adId, cpc); } } Kotlin data class AdCpcData( var adId: Int, var cpc: Double )
  9. Variable declaration Java private final Map<String, String> myMap = new

    HashMap<>(); myMap.put("Hi", "There"); Kotlin val myMap = mapOf("Hi" to "There")
  10. Testing Java @Test public void testShouldProcessCampaign_WhenFlyToInRange() throws Exception { …

    } Kotlin @Test fun `when "fly to" is in range - should process the campaign`() { TODO() }
  11. Collections val fruits = listOf("banana", "avocado", "apple", "kiwi") fruits .filter

    { it.startsWith("a") } .sortedBy { it } .map { it.toUpperCase() } .forEach { println(it) }
  12. When Expression when (object) { 1 -> "One" "Hello" ->

    "Greeting" is Long -> "Long" in 2..10 -> "obj is in the range" else -> "Unknown" }
  13. Null Safety val world: String? = null println(world.toUpperCase()) // In

    Java you will get NPE // In Kotlin you will get compilation error
  14. Null Safety val world: String? = null if (world !=

    null) { println(world.toUpperCase()) } // In Java you will get NPE // In Kotlin you will get compilation error
  15. Many More Features • String Interpolation • Named Parameters •

    Smart Casting • Contracts • Multiline Strings