Home > Net >  Doing this on kotlin is a bad practice?
Doing this on kotlin is a bad practice?

Time:08-05

The second way is a little smaller but i dont know if this is okay, can i use also just for be able to use expression body and put the first line of the cod on the side of method name?

override fun findOrdensColeta() {
    view.setProgressBarVisibility(View.VISIBLE)

    model.findOrdensColeta {
        handleFindOrdensColeta()
    }
}

override fun findOrdensColeta() = view.setProgressBarVisibility(View.VISIBLE).also {
    model.findOrdensColeta {
        handleFindOrdensColeta()
    }
}

CodePudding user response:

Yes, I think the second version is bad style. I see no good reasons to use also() like that, and several reasons not to:

also() is intended for use within expressions, where you don't have the option of adding a separate statement. (The classic case is logging a value before doing something with it.) That doesn't apply here, where two simple statements work just as well. So there's no benefit other than conciseness; using also() here is unnecessary complexity.

The second version has an expression body, which looks like it returns a useful value — but it actually returns the result of calling setProgressBarVisibility(), which is presumably Unit just like the first version. So the expression body is highly misleading.

Also, the only reason that the second version is shorter is that the first statement has been squeezed onto the same line. I don't think that's justified here* — it joins two things (the function signature and the call to setProgressBarVisibility()) that aren't directly related, and it makes the line too long for most people to read easily. (I'm surprised you find the second version easier to read. I tend to prefer conciseness, but even I find the first version a good deal easier to read — probably because it falls into a very familiar pattern that doesn't need any extra thought.)

If you cared only about reducing the number of lines, then the first version could be written like this (not recommended!):

override fun findOrdensColeta() { view.setProgressBarVisibility(View.VISIBLE)
    model.findOrdensColeta {
        handleFindOrdensColeta()
    }
}

(You could join even more lines, perhaps squeezing it all onto a single line if you wanted to make it completely unreadable!)

Conversely, if there were other good reasons for using the second version, it would be better if wrapped like this:

override fun findOrdensColeta()
    = view.setProgressBarVisibility(View.VISIBLE).also {
        model.findOrdensColeta {
            handleFindOrdensColeta()
        }
    }

So as you can see, the difference in length is mainly due to the (unjustified and confusing) line-joining, not the use of also().

So using also() here has no real benefit, as well as some significant drawbacks.


* I'm not saying you should never put the function body on the same line as its signature. That can work well if the body is an expression that's short enough to fit neatly all one line. For example:

override fun toString() = "MyClass(val1 = $val1)"

However, if that makes the line very long, or wraps onto further lines, or is a function body, then it's almost always more readable to start the body on the next line in the traditional fashion.

CodePudding user response:

I believe the second one is a bad approach. also is designed to provide the ability to modify or use the receiver and return it afterwards.

In your case, also is not containing any usages of its receiver (which is the result of view.setProgressBarVisibility(View.VISIBLE) ). Therefore it is not needed here

CodePudding user response:

The second version is a bit confused to me - if you take the first one as the standard way to do things, a simple code block with two statements in it, what benefit does the second one really give you? You're basically using expression syntax to make it a one-liner - but it's not a one-liner, so you have to add a scope function just to give yourself back the curly braces so you can add another line of code!

So this:

override fun findOrdensColeta() {
    view.setProgressBarVisibility(View.VISIBLE)

    model.findOrdensColeta {
        handleFindOrdensColeta()
    }
}

Does exactly the same thing as this:

override fun findOrdensColeta() = view.setProgressBarVisibility(View.VISIBLE).also {
    model.findOrdensColeta {
        handleFindOrdensColeta()
    }
}

But with the latter

  • it appears to return a result from setProgressBarInvisibility because it's a single-expression function (the original clearly returns nothing)
  • the use of also which passes that result value through reinforces the idea that you're trying to return that result
  • the also block implies you're using that value for something (otherwise why's it there?) and it takes a moment to see that you're not
  • when you realise none of the above are true, now you might be wondering if you're missing something, or if the original coder intended something specific but made a mistake

Because the basic function block is so simple and readable and a natural fit for what you're doing, doing something else can throw up some questions, or be confusing to read. Sure the way it's formatted you've saved a single line, but now it's harder to understand, y'know?

This is something to watch out for in Kotlin I think (and I'm guilty of this myself) - being able to chain stuff together sometimes encourages people to go for "one-liners" that are hard to follow, but hey at least you didn't (explicitly) create a variable! That's not what you're doing here (you're creating an unnecessary variable actually!) but it feels like a similar thing - trying to make a single expression instead of doing things the old-school way.

Coding is about trying to strike that balance between simplicity and readability, and elegant efficiency, and a lot of it's about learning what tools and tricks are available, and knowing when to use them (and how best to do it) and when to avoid them. At the end of the day it's a style choice and this is just my opinion (although all the other commenters so far are saying similar things) but hopefully it's given you something to think about! I've been there too - including using expressions for functions that don't return a value at all - but I think that's all part of learning a language and the things it offers you

  • Related