Skip to content

Latest commit

 

History

History
129 lines (84 loc) · 5.64 KB

Material.md

File metadata and controls

129 lines (84 loc) · 5.64 KB

Writing Effective Kotlin

Data Classes

There are typically two types of classes, divided by how they are used

  1. Value types: These are classes that represent the what. They track information.
  2. Transformer types: These are classes that represent business logic. They act upon value types and transform them according to business rules.

There are situations where we want to have default values for some properties in a value type.

Builders

The "Builder" pattern was designed to handle scenarios where you might want to configure small portions of a class.

Reference: Java Builder

Builders are also used to make copies of existing classes with minor changes.

Kotlin has two features on data classes which make this obsolete.

  1. Default parameters: The ability to declare a default value for a constructor/function parameter
  2. Copy methods: The ability to make a copy of an existing object while changing only desired properties

Exercises

Reference: Data Classes

  • Define a PhoneNumber type, which should track a given phone number and the country ISD code.
  • Define an EmailAddress type, which should track the username, the email provider host.
  • Define a User type, which should have an integer user ID, a name, a PhoneNumber and an EmailAddress
  • Create an instance of the following user Name: Vinay Shenoy Phone Number: +91-1234567890 Email Address: [email protected]
  • Change my phone number to +91-0987654321

Lambdas

Interfaces let you do two things:

  1. Provide configuration behaviour
  2. Support event driven programming

Note: Lambdas can be BOTH class properties as well as parameters to methods.

Exercises

References: Lambdas

  • Add a removeAccount method to the Bank Kotlin class
  • Add a lambda method that will get invoked whenever an account is removed that gets invoked with the removed account
  • Configure firstBank to log the removed accounts using the Android logcat
  • Configure secondBank to log the removed account, but only if the account balance is over 5000 rupees

Null-safety

Reference: Null safety

  • Kotlin's type system variations are mainly to remove null references from code. We'll see elvis operator, not-null assertion operator and safe calls as well. Kotlin also supports smart cast and type inference.
  1. Nullable type variables
  2. Non-nullable types

Exercises

  • Create a data class Dessert that has your favorite pastries with the amountOfCalories as non-null Int
  • Create an instance and add a setCalories function that takes nullable amountOfCalories as a parameter and use a safe call to have an integer value or a -1.

Collection operations

Common operations typically done on collections (List):

  1. Iteration: Do something on every item
  2. Transformation: Convert each item into some other item
  3. Filtering: Select some items out of the list
  4. Grouping: Group information based on some criteria

Exercises

Reference: Collections

  1. Find all mutants with 10 or more years of experience
  2. Find the real names of all the good mutants
  3. Concatenate the code names of all the senior mutants with a ", " (Hint: look at `joinToString``)
  4. Concatenate the code names of all the good and evil mutants separately with a ", " and then concatenate these two strings together with a " VS. "

Sealed classes

Reference: Sealed classes

  • Better state handling with restricted types.

Exercises

  • Create a Failure sealed class and add types of errors. Every error must be a data class that takes a string as a parameter. Eg: ServerError,AuthenticationError and Exceptions where exceptions would be unknown errors.
  • Create a function that takes Failure as a parameter. Implement all the failures in it using a when condition. Print "something went wrong` for unknown exceptions and the error names for the rest of them.

Utility classes

Reference: Extension functions

  • Utility classes are generally made when we don't have access to the original source code.
    • Example : if we want to add an email validation method to a string. In Java, we would create a StringUtil class, and add a method like isEmail(String) , so we can call StringUtil.isEmail(value). This can be replaced with extension methods in Kotlin, so we can call something like value.isEmail
  • Syntax .
  • Ability to extend the class without having to inherit from a class.
  • We wont get private information in the class, we will still have access to public fields and functions. It's just a syntactic sugar for Util classes.

Exercises

  • Add a extension function for String to check whether it's an email or not. We can check if the string contains @ to determine if it's email or not.
  • Add a extension function for Int that takes in parameter to check if it's divisible by it or not.