Skip to content

Instantly share code, notes, and snippets.

View magdamiu's full-sized avatar

Magda Miu magdamiu

View GitHub Profile
@magdamiu
magdamiu / Lambdas.kt
Created December 12, 2021 20:18
Lambdas expressions & Closure (capturing lambda) | High performance with idiomatic Kotlin
var priceBooks = 0.0
val prefixPriceDetails = "The current sum is "
books.forEach {
priceBooks += it.price
println("$prefixPriceDetails $priceBooks")
}
@magdamiu
magdamiu / HighOrderFunctions.kt
Created December 12, 2021 12:15
High-order functions | High performance with idiomatic Kotlin
fun titleStartsWithS(book: Book) = book.title.startsWith("S")
fun lengthOfTitleGraterThan5(book: Book) = book.title.length > 5
fun authorStartsWithB(book: Book) = book.author.startsWith("B")
val book1 = Book("Start with why", "Simon Sinek")
val book2 = Book("Dare to lead", "Brene Brown")
val books = listOf(book1, book2)
// this code should be improved
val filteredBooks = books
@magdamiu
magdamiu / PureFunctions.kt
Created December 10, 2021 18:55
Pure functions | High performance with idiomatic Kotlin
class Book(val title: String, val author: String) {
// impure function
fun getBookDetails() = "$title - $author"
}
// pure function
fun getBookDetails(title: String, author: String) = "$title - $author"
@magdamiu
magdamiu / CleanErrorHandling3.kt
Created August 21, 2021 13:11
Clean Code with Kotlin by Magda Miu - Error Handling 2 - Sealed Classes Improved Version - Clean Code
val <T> T.exhaustive: T
get() = this
fun displayMovieResult(movieResult: MovieSearchResult) {
when(movieResult) {
is MovieFound -> println("yey, we found the movie")
is MovieNotFound -> TODO()
}.exhaustive
}
@magdamiu
magdamiu / CleanErrorHandling2.kt
Last active August 21, 2021 13:10
Clean Code with Kotlin by Magda Miu - Error Handling 2 - Sealed Classes - Clean Code
sealed class MovieSearchResult
data class MovieFound(val movie: Movie) : MovieSearchResult()
object MovieNotFound : MovieSearchResult()
object DatabaseOffline : MovieSearchResult()
fun displayMovieResult(movieResult: MovieSearchResult) {
when(movieResult) {
is MovieFound -> println("yey, we found the movie")
is MovieNotFound -> TODO()
}
@magdamiu
magdamiu / CleanErrorHandling1.kt
Created August 21, 2021 13:07
Clean Code with Kotlin by Magda Miu - Error Handling 1
fun computeSqrt(number: Double): Double {
if(number >= 0) {
return Math.sqrt(number)
} else {
throw RuntimeException("No negative please")
}
}
@magdamiu
magdamiu / CleanFunctions2.kt
Created August 21, 2021 12:48
Clean Code with Kotlin by Magda Miu - Functions 2 - Clean Code
var countUsersYoungerThan30WithSubscriptions = 0
for (user in users) {
if (user.isYoungerThan30WithSubscriptions) {
countUsersYoungerThan30WithSubscriptions++;
}
}
@magdamiu
magdamiu / UncleanFunctions2.kt
Created August 21, 2021 12:47
Clean Code with Kotlin by Magda Miu - Functions 2 - Unclean Code
for (user in users) {
if(user.subscriptions != null) {
if (user.subscriptions.size > 0) {
var isYoungerThan30 = user.isYoungerThan30()
if (isYoungerThan30) {
countUsers++
}
}
}
}
@magdamiu
magdamiu / CleanFunctions1.kt
Created August 21, 2021 12:46
Clean Code with Kotlin by Magda Miu - Functions 1 - Clean Code
fun parseProduct(response: Response?) = when (response?.code()){
null -> throw ClientException("Response is null")
200, 201 -> mapToDTO(response.body())
in 400..499 -> throw ClientException("Invalid request")
in 500..599 -> throw ClientException("Server error")
else -> throw ClientException("Error ${response.code()}")
}
@magdamiu
magdamiu / UncleanFunctions1.kt
Created August 21, 2021 12:46
Clean Code with Kotlin by Magda Miu - Functions 1 - Unclean Code
fun parseProduct(response: Response?): Product? {
if (response == null) {
throw ClientException("Response is null")
}
val code: Int = response.code()
if (code == 200 || code == 201) {
return mapToDTO(response.body())
}
if (code >= 400 && code <= 499) {
throw ClientException("Invalid request")