Skip to content

Instantly share code, notes, and snippets.

// UserRepository interface
interface UserRepository {
suspend fun isBiometricsOptedIn(): Boolean
}
// SessionController interface
interface SessionController {
suspend fun biometricOptIn(): BiometricResult
suspend fun biometricOptOut(): BiometricResult
}
@kwalker-git
kwalker-git / CodeSmells.kt
Last active May 9, 2025 15:54
Code Smells
package com.example.codesnippetsforinterviews
class User(
val name: String, val age: String, val address: Address, medicalRecordNumber: String,
primaryDiagnosis: List<String>
)
class Address(
var s1: String, var s2: String,
var c: String, var zc: String,
medicalRecordNumber: String, serviceArea: String
private fun processSuccess(response: RefreshResponse) {
// before 1.5
run msgLoop@{
response.messages.forEach { message ->
val user = extractUserData(message)
if (user != null) {
logLastMessage(message.userData)
return@msgLoop
}
}
@JvmInline
value class PreferredFirstNameInline(private val name: String) // No overhead for PreferredFirstNameInline class unless needed for boxing
// as compared to
data class PreferredFirstName(private val name: String)
fun main() {
// The Java code decompiled from the bytecode shows the underlying implementation.
val inlineFirstName = PreferredFirstNameInline("fred") // Implemented by static method
/**
sealed interface NewSession {
fun showMessage()
}
sealed interface ExistingSession
sealed class Navigation
class PreviousPage : Navigation(), ExistingSession
class NextPage : Navigation(), ExistingSession
class Home : Navigation(), NewSession {
override fun showMessage() {}
@kwalker-git
kwalker-git / functionalInterfaceExample.kt
Created December 23, 2020 20:39
Interface using keyword "fun"
fun interface DataChangeNotifier {
fun publishDataChange()
}
@kwalker-git
kwalker-git / SAMconversion.kt
Created December 23, 2020 14:49
SAM conversion of Kotlin code
fun notifyAppointmentDataChanges() {
val notifier = DataChangeNotifier { apptDataPubSub.onNext() }
notifier.publishDataChange()
}
@kwalker-git
kwalker-git / objectExpressionForSAM.kt
Created December 23, 2020 14:47
Using Object Expression to implement a SAM
fun notifyAppointmentDataChangesNonFunctional() {
val notifier = object : DataChangeNotifier {
override fun publishDataChange() {
apptDataPubSub.onNext()
}
}
notifier.publishDataChange()
}
@kwalker-git
kwalker-git / noLabelToBreakOrContinue.kt
Created December 23, 2020 14:44
Using break and continue for a when statement nested inside a for loop
for (proxy in proxyList) {
when (proxy.relation) {
(Proxy.RELATION_SELF) -> break
(Proxy.RELATION_CHILD) -> continue
(Proxy.RELATION_TEEN) -> {
// Logic for teen relation
}
}
// Additional processing
}
@kwalker-git
kwalker-git / loopLabelNeeded.kt
Created December 23, 2020 14:40
Before 1.4, if a developer wanted to code a break or continue statement in a loop nested inside a when clause, it was necessary to define a label for the loop statement, and then reference the label in the inner break or continue statement.
PROXYLOOP@for (proxy in proxyList) {
when (proxy.relation) {
(Proxy.RELATION_SELF) -> break@PROXYLOOP
(Proxy.RELATION_CHILD) -> continue@PROXYLOOP
(Proxy.RELATION_TEEN) -> {
// Logic for teen relation
}
}
// Additional processing
}