Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Berlindroid: Naming is Hard

Ash Davies
February 28, 2024

Berlindroid: Naming is Hard

Naming is hard, it's no unfamiliar thing to software engineers that describing our software can sometimes be just as hard as writing it in the first place. But did you know that the names we give our well-intentioned ideas can come back to haunt us?

It would turn out that naming is not just hard, but it has implications! Gradle will often not actually report when names collide! Which means you might end up with unexpected class resolutions! Which leads us down the rabbit hole of, how does Java resolve classes in the first place?!

This talk is a work in progress, I'll show you my problem-solving approach, and what discoveries I made along the way to find out that, well, naming is indeed, hard.

Ash Davies

February 28, 2024
Tweet

More Decks by Ash Davies

Other Decks in Programming

Transcript

  1. Naming is Hard How Naming Can Bite You in the

    Ass Berlindroid - Feb ‘24 󰎲 Ash Davies Android / Kotlin GDE - Berlin ashdavies.dev Kotti
  2. commonMain Expected and actual declarations Kotlin Multiplatform public data class

    File(public val path: String) public fun File.print(): String = "File: $path" public expect fun createNewFile(): File
  3. androidMain / jvmMain Expected and actual declarations Kotlin Multiplatform public

    actual fun createNewFile(): File { return File(createTempFile().toString()) }
  4. Expected and actual declarations Kotlin Multiplatform > Task :shared:compileDebugKotlinAndroid FAILED

    e: file:///shared/src/androidMain/kotlin/io/ashdavies/shared/File.kt:1:1 Duplicate JVM class name 'io/ashdavies/shared/FileKt' generated from: FileKt, FileKt e: file:///shared/src/commonMain/kotlin/io/ashdavies/shared/File.kt:1:1 Duplicate JVM class name 'io/ashdavies/shared/FileKt' generated from: FileKt, FileKt 🔥🔥🔥
  5. Extensions Resolved Statically Kotlin Multiplatform public data class File(public val

    path: String) { private val segments = path.split("/") } // Does not compile! public fun File.printSegments() { segments.forEach { println(it) } }
  6. Extensions Resolved Statically Kotlin Multiplatform public data class File(public val

    path: String) public val File.extension: String get() = path.substringAfterLast(".")
  7. Extensions Kotlin Multiplatform Exception in thread "main" java.lang.NoSuchMethodError: 'io.ashdavies.playground.File io.ashdavies.playground.File_jvmKt.createNewFile()'

    FATAL EXCEPTION: main Process: io.ashdavies.playground, PID: 28922 java.lang.NoSuchMethodError: No static method createNewFile()Lio/ashdavies/playground/File; in class Lio/ashdavies/playground/File_androidKt; or its super classes (declaration of 'io.ashdavies.playground.File_androidKt' appears in /data/data/io.ashdavies.playground/code_cache/.overlay/ba se.apk/classes17.dex) 🔥🔥🔥 Desktop Android
  8. Archaic Conventions public interface ViewStateFactory { fun create(): ViewState }

    public class ViewStateFactoryImpl : ViewStateFactory { override fun create(): ViewState { TODO() } } Impl Suffix
  9. Unclear Intentions for (i: Int in kS) { try {

    c.connect(i) } catch (e: Throwable) { // Meh 󰤇 } } Single letter names
  10. Meaningful Naming fun updateClaimWithPolicyDetails(c: Claim, pc: PolicyCriteria, identifier: String) fun

    getIfNotSetWithDefault(value: Any): String fun compareAndSet(value: Any): Boolean fun isNullOrEmpty(value: String): Boolean Command / Query
  11. “Always code as if the guy [sic] who ends up

    maintaining your code will be a violent psychopath who knows where you live” - John Woods
  12. Language 🗣 󰎼 ¿Qué tal? 󰧻 Helô 󰏃 Salut! 󰑒

    Pryvit 󰏅 Hello! 󰏢 Ciao 󰎩 Nǐ hǎo 󰎲 Hallo 󰐨 Oi 󰎺 Ahlan 󰏮Anyoung 󰐴 Hej 󰏏 Yassou 󰑍 Selam
  13. # -*- coding: iso-8859-1 -*- schön = Wahr häßlich =

    Falsch für bäh in [schön, häßlich]: drucke bäh def sovielwiemöglich(): "gib" zurück "was mir gehört"
  14. Don’t repeat yourself, avoid naming collisions Consider causes for ambiguous

    error messages Scrutinise naming conventions Understand idioms for the language Takeaways