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

Rule 6.4.1: Avoid using utility classes/objects, use extensions instead #448

Closed
petertrr opened this issue Oct 23, 2020 · 0 comments · Fixed by #508
Closed

Rule 6.4.1: Avoid using utility classes/objects, use extensions instead #448

petertrr opened this issue Oct 23, 2020 · 0 comments · Fixed by #508
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@petertrr
Copy link
Member

As described in [6.2 Extension functions] - extension functions - is a very powerful mechanism.
So instead of using utility classes/objects, use it instead.
This allows you to remove the unnecessary complexity and wrapping class/object and to use top-level functions instead.

Bad example:

    object StringUtil {
        fun stringInfo(myString: String): Int {
            return myString.count{ "something".contains(it) }
        }
    }
    StringUtil.stringInfo("myStr")

Good example:

    fun String.stringInfo(): Int {
        return this.count{ "something".contains(it) }
    }

    "myStr".stringInfo()
@petertrr petertrr added the enhancement New feature or request label Oct 23, 2020
@petertrr petertrr added this to the Chapter 6 milestone Oct 23, 2020
@kentr0w kentr0w self-assigned this Nov 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants