Skip to content

Files

Latest commit

 

History

History
37 lines (26 loc) · 712 Bytes

StringLiteralDuplication.md

File metadata and controls

37 lines (26 loc) · 712 Bytes

Pattern: Duplicated String literal

Issue: -

Description

Repeatedly typing out the same String literal across the codebase makes it harder to change and maintain.

Prefer extracting the String literal into a property or constant instead.

Example of incorrect code:

class Foo {

    val s1 = "lorem"
    fun bar(s: String = "lorem") {
        s1.equals("lorem")
    }
}

Example of correct code:

class Foo {
    val lorem = "lorem"
    val s1 = lorem
    fun bar(s: String = lorem) {
        s1.equals(lorem)
    }
}

Further Reading