DEV Community

Yonatan Karp-Rudin
Yonatan Karp-Rudin

Posted on • Originally published at yonatankarp.com on

2 2

Kotlin Code Smell 3 - String Abusers

z8ol4jpeszi5wydtv1nj.png

TL;DR: Use real abstractions and real objects instead of string accidental manipulation.

Problems

  • Complexity

  • Readability

  • Maintainability

  • Lack of Abstractions

Solutions

  • Work with objects instead of strings.

  • Replace strings with data structures dealing with object relations.

  • Find Bijection problems between real objects and the strings.

Examples

  • Serializers

  • Parsers

Sample Code

Wrong

val schoolDescription = "College of Springfield"

// location = "Springfield"
val location = """[^]*\$""".toRegex()
    .find(schoolDescription)?.value

// school = "College"
val school = """^[\w]+""".toRegex()
    .find(schoolDescription)?.value

Enter fullscreen mode Exit fullscreen mode

Right

class School(
    private val name: String,
    private val location: Location
) {
    fun description() = "$name of ${location.name}"
}

class Location(
    val name: String
)

Enter fullscreen mode Exit fullscreen mode

Conclusion

Don't abuse strings. Favor real objects. Find absent protocol to distinguish them from strings.

Credits

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

AWS Security LIVE!

Hosted by security experts, AWS Security LIVE! showcases AWS Partners tackling real-world security challenges. Join live and get your security questions answered.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️