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

Klean that Code, Boil those Boilerplates

oshai
September 13, 2018

Klean that Code, Boil those Boilerplates

Klean that Code, Boil those Boilerplates -
Ohad was a Java developer for some time, then Scala and now Kotlin and Java again. In this talk, Ohad will show by code examples how to get rid of boilerplate Java code and convert it to concise, clean Kotlin code. We will see some of the cool features of Kotlin and how they can be used to make programming more fun.

Ohad is using Kotlin for a couple of years and the maintainer of https://github.com/MicroUtils/kotlin-logging and https://github.com/jasync-sql/jasync-sql

If you want to read more about his journey check out this blog post: https://medium.com/@OhadShai/scala-pack-your-bags-kotlin-is-coming-5169f737cfe8

oshai

September 13, 2018
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. What’s wrong with Boilerplate • It’s not what we came

    here to do. • We have to write that code. • We have to read that code. https://www.ithinkmedia.co.uk/blog/digital-pr/how-to-write-an-effective-boilerplate/
  3. Hello Kotlin! Java public class Hello { public static void

    main(String[] args) { System.out.println("Hello!"); } } Kotlin fun main(args: Array<String>) { println("Hello!") }
  4. Testing Java @Test public void testShouldProcessCampaign_WhenFlyToInRange() throws Exception { …

    } Kotlin @Test fun `when "fly to" is in range - should process the campaign`() { TODO() }
  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; } }
  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; } } 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()…
  8. 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
  9. 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 )
  10. Fun Java public ResultSet executeQuery(final EDatabase database, final String sqlQuery)

    throws SQLException { … } public ResultSet executeQuery(final EDatabase database, ) throws SQLException { executeQuery(database, “select 1”) } Kotlin fun executeQuery(databaseL EDatabase, sqlQuery: String = “select 1”) : ResultSet { … }
  11. Extended Fun Java public ResultSet executeQuery(final EDatabase database, final String

    sqlQuery) throws SQLException { … } DatabaseUtils.executeQuery(database, sqlQuery) Kotlin fun EDatabase.executeQuery(sqlQuery: String): ResultSet { … } database.executeQuery(sqlQuery)
  12. Switch to When WritableCell cell; switch (header.getSummaryType()) { case AVERAGE:

    cell = makeNumberCell(col, 1, sum / count, NumberFormats.FLOAT); break; case SUM: cell = makeNumberCell(col, 1, sum, header.getCellFormat()); break; case TITLE: final String summaryTitle = makeSummaryTitle(header.getSummaryFormat(), count, sum); cell = makeLabelCell(col, 1, summaryTitle); break; default: case COUNT: cell = makeNumberCell(col, 1, count, NumberFormats.DEFAULT); break; }
  13. Switch to When val cell = when (header.summaryType) { AVERAGE

    -> makeNumberCell(col, 1, sum / count, NumberFormats.FLOAT) SUM -> makeNumberCell(col, 1, sum, header.cellFormat) TITLE -> { val summaryTitle = makeSummaryTitle(header.summaryFormat, count, sum) makeLabelCell(col, 1, summaryTitle) } else -> makeNumberCell(col, 1, count, NumberFormats.DEFAULT) }
  14. When Expression when (object) { 1 -> "One" "Hello" ->

    "Greeting" is Long -> "Long" in 2..10 -> "obj is in the range" else -> "Unknown" }
  15. Variable declaration Java private final Map<String, String> myMap = new

    HashMap<>(); myMap.put("Hi", "There"); Kotlin val myMap = mapOf("Hi" to "There")