Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

As a developer, I can use String extensions in effective way #50

Open
hoangnguyen92dn opened this issue Jun 4, 2024 · 0 comments
Open

Comments

@hoangnguyen92dn
Copy link
Collaborator

Why

There are 2 extension to validate a valid string to reduce the ! in the if-condition block

fun String?.isNotNullOrEmpty(): Boolean = !this.isNullOrEmpty()

fun String?.isNotNullOrBlank(): Boolean = !this.isNullOrBlank()

Even the validation result is true, the develop still has to add the fallback value

val name: String = if (name.isNotNullOrEmpty()) {
    name // 🚫 IDE error due Type mismatch. Required: String Found: String?
}

The idea is building the extension with contract to tackle this issue

fun String?.isNotNullOrEmpty(): Boolean {
    contract {
        returns(false) implies (this@isNotNullOrEmpty != null)
    }
    return !this.isNullOrEmpty()
}

Reference:

Who Benefits?

Describe who will be the beneficiaries e.g. everyone, specific chapters, clients...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant