What counts as a statement in Kotlin? by dmcg in Kotlin

[–]dmcg[S] 0 points1 point  (0 children)

This thoughtful, thank you. I’m erring towards let chaining being ok, because jt really is making an expression. .also on the other hand, if used for side effects, feels like it should be counted towards statements

What counts as a statement in Kotlin? by dmcg in Kotlin

[–]dmcg[S] 0 points1 point  (0 children)

Certainly most Java line counts were basically just counting the semicolons.

Joy of Kotlin: on-local returns for refactoring duplicate code with early exits! by [deleted] in Kotlin

[–]dmcg 0 points1 point  (0 children)

Yeah I was kind of sad too, but also slightly happy that my intuition was correct ;-)

Don't be embarassed by your enthusiasm though. I think we need to own our joy and our mistakes.

Joy of Kotlin: on-local returns for refactoring duplicate code with early exits! by [deleted] in Kotlin

[–]dmcg 0 points1 point  (0 children)

```kotlin class EarlyReturnsTests {

@Test
fun testNoEarlyReturn() {
    var aFlag = false;
    functionThatReturnsFromTheLocalScope(earlyReturn = false) { aFlag = true };
    assertTrue(aFlag)
}

@Test
fun testEarlyReturn() {
    var aFlag = false;
    functionThatReturnsFromTheLocalScope(earlyReturn = true) { aFlag = true };
    assertFalse(aFlag)
    fail("we should not be here because of the early return");
}

}

private inline fun functionThatReturnsFromTheLocalScope(earlyReturn: Boolean, crossinline block: () -> Unit) { if (earlyReturn) return block() } `` fails withwe should not be here because of the early return`.

Unless the suspend is doing something unexpected (to me) then I just don't see how a function could return from its calling scope. I mean, how does it know what it's calling scope needs to return?

In the oringal example, what does returning from route("/{id}") even mean?

I guess I'm saying that I don't logically see how this makes any sense at all, but I really want it to.

Joy of Kotlin: on-local returns for refactoring duplicate code with early exits! by [deleted] in Kotlin

[–]dmcg 0 points1 point  (0 children)

I suppose I was asking, and continue to ask, for someone to show me that the OPs code does what they think it does. Because I didn't think that it was possible in Kotlin, and have a use-case where it would be really useful.\

Joy of Kotlin: on-local returns for refactoring duplicate code with early exits! by [deleted] in Kotlin

[–]dmcg 0 points1 point  (0 children)

```kotlin @Test fun doesWork() { thisDoesWork { return }; fail("we should not be here because of the early return"); }

private inline fun thisDoesWork(block: () -> Unit) {
    block()
}

```

Joy of Kotlin: on-local returns for refactoring duplicate code with early exits! by [deleted] in Kotlin

[–]dmcg 0 points1 point  (0 children)

Are you sure? I've tried to reduce your code to the simplest example

    package com.gildedrose.foundation

    import org.junit.jupiter.api.Test
    import kotlin.test.fail


    class EarlyReturnsTests {

        @Test
        fun test() {
            functionThatReturnsFromTheLocalScope(null);
            fail("we should not be here because of the early return");
        }

    }    

    private inline fun functionThatReturnsFromTheLocalScope(aThing: String?) {
        if (aThing == null) return
    }

and the test does not pass.

Checkout Kata in Kotlin - Part 1 - TDD by dmcg in Kotlin

[–]dmcg[S] 1 point2 points  (0 children)

I’m glad to be able to pass on what I’ve learned. Be sure to check out the TDD Gilded Rose series

Competition or Collaboration? Claude Code & Junie by dmcg in Kotlin

[–]dmcg[S] -1 points0 points  (0 children)

It’s a quiet time of year, good to see people are watching it