Skip to content

Instantly share code, notes, and snippets.

Avatar

Magda Miu magdamiu

View GitHub Profile
@magdamiu
magdamiu / CleanErrorHandling3.kt
Created Aug 21, 2021
Clean Code with Kotlin by Magda Miu - Error Handling 2 - Sealed Classes Improved Version - Clean Code
View CleanErrorHandling3.kt
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 Aug 21, 2021
Clean Code with Kotlin by Magda Miu - Error Handling 2 - Sealed Classes - Clean Code
View CleanErrorHandling2.kt
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 Aug 21, 2021
Clean Code with Kotlin by Magda Miu - Error Handling 1
View CleanErrorHandling1.kt
fun computeSqrt(number: Double): Double {
if(number >= 0) {
return Math.sqrt(number)
} else {
throw RuntimeException("No negative please")
}
}
@magdamiu
magdamiu / CleanFunctions2.kt
Created Aug 21, 2021
Clean Code with Kotlin by Magda Miu - Functions 2 - Clean Code
View CleanFunctions2.kt
var countUsersYoungerThan30WithSubscriptions = 0
for (user in users) {
if (user.isYoungerThan30WithSubscriptions) {
countUsersYoungerThan30WithSubscriptions++;
}
}
@magdamiu
magdamiu / UncleanFunctions2.kt
Created Aug 21, 2021
Clean Code with Kotlin by Magda Miu - Functions 2 - Unclean Code
View UncleanFunctions2.kt
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 Aug 21, 2021
Clean Code with Kotlin by Magda Miu - Functions 1 - Clean Code
View CleanFunctions1.kt
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 Aug 21, 2021
Clean Code with Kotlin by Magda Miu - Functions 1 - Unclean Code
View UncleanFunctions1.kt
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")
@magdamiu
magdamiu / CleanMeaningfulNames3.kt
Created Aug 21, 2021
Clean Code with Kotlin by Magda Miu - Meaningful names 3 - Clean Code
View CleanMeaningfulNames3.kt
users.filter{ user -> user.job == Job.Developer }
.map{ developer -> developer.birthDate.dayOfMonth }
.filter { birthDay -> birthDay <= 10 }
.min()
@magdamiu
magdamiu / UncleanMeaningfulNames3.kt
Created Aug 21, 2021
Clean Code with Kotlin by Magda Miu - Meaningful names 3 - Unclean Code
View UncleanMeaningfulNames3.kt
users.filter{ it.job == Job.Developer }
.map{ it.birthDate.dayOfMonth }
.filter{ it <= 10 }
.min()
@magdamiu
magdamiu / CleanMeaningfulNames2.kt
Last active Aug 21, 2021
Clean Code with Kotlin by Magda Miu - Meaningful names 2 - Clean Code
View CleanMeaningfulNames2.kt
class Book(val title: String?, val publishYear: Int?)
fun displayBookDetails(book: Book) {
val title = book.title ?:
throw IllegalArgumentException("Title required")
val publishYear = book.publishYear ?: return
println("$title: $publishYear")
}