Skip to content

Instantly share code, notes, and snippets.

View magdamiu's full-sized avatar

Magda Miu magdamiu

View GitHub Profile
@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")
@magdamiu
magdamiu / UncleanMeaningfulNames1.kt
Last active August 21, 2021 12:37
Clean Code with Kotlin by Magda Miu - Meaningful names 1 - Unclean Code
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 August 21, 2021 12:37
Clean Code with Kotlin by Magda Miu - Meaningful names 1 - Clean Code
data class PathParts(val directory: String, val fileName: String)
fun splitPath(path: String) =
PathParts(
path.substringBeforeLast('/', ""),
path.substringAfterLast('/'))
@magdamiu
magdamiu / UncleanMeaningfulNames2.kt
Last active August 21, 2021 12:36
Clean Code with Kotlin by Magda Miu - Meaningful names 2 - Unclean Code
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")
@magdamiu
magdamiu / CleanMeaningfulNames2.kt
Last active August 21, 2021 12:36
Clean Code with Kotlin by Magda Miu - Meaningful names 2 - Clean Code
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")
}
@magdamiu
magdamiu / CleanMeaningfulNames3.kt
Created August 21, 2021 12:35
Clean Code with Kotlin by Magda Miu - Meaningful names 3 - Clean Code
users.filter{ user -> user.job == Job.Developer }
.map{ developer -> developer.birthDate.dayOfMonth }
.filter { birthDay -> birthDay <= 10 }
.min()