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 / UncleanMeaningfulNames1.kt
Last active Aug 21, 2021
Clean Code with Kotlin by Magda Miu - Meaningful names 1 - Unclean Code
View UncleanMeaningfulNames1.kt
data class GetFile(val d: String, val n: String)
val pattern = Regex("(.+)/([^/]*)")
fun files(ph: String): PathParts {
val match = pattern.matchEntire(ph)
?: return PathParts("", ph)
return PathParts(match.groupValues[1],
match.groupValues[2])
@magdamiu
magdamiu / CleanMeaningfulNames1.kt
Last active Aug 21, 2021
Clean Code with Kotlin by Magda Miu - Meaningful names 1 - Clean Code
View CleanMeaningfulNames1.kt
data class PathParts(val directory: String, val fileName: String)
fun splitPath(path: String) =
PathParts(
path.substringBeforeLast('/', ""),
path.substringAfterLast('/'))
@magdamiu
magdamiu / UncleanMeaningfulNames2.kt
Last active Aug 21, 2021
Clean Code with Kotlin by Magda Miu - Meaningful names 2 - Unclean Code
View UncleanMeaningfulNames2.kt
class Book(val title: String?, val publishYear: Int?)
fun displayBookDetails(book: Book) {
val title = book.title
if (title == null)
throw IllegalArgumentException("Title required")
val publishYear = book.publishYear
if (publishYear == null) return
println("$title: $publishYear")