https://kotlinlang.org logo
Title
r

romster

03/22/2018, 8:17 AM
Hello guys! Could you please recommend sources where to read/listen about Kotlin code structure? As I can see Kotlin doesn't have "package" as a first-class citizen (no package-private access to members) It suggests to use "modules" instead, but they are slightly different thing, and I haven't seen any "best practices" of using Kotlin modules.
r

romster

03/22/2018, 10:17 AM
Thank you, but there is nothing about modules and "internal" visibility best-practices there.
c

Czar

03/22/2018, 10:18 AM
Sorry, I don't understand what you mean then.
internal is the same as package, but with additional restriction to the same jar
r

romster

03/22/2018, 10:19 AM
So it is useful only for libraries developers, isn't it?
c

Czar

03/22/2018, 10:34 AM
why? within project it acts as package-private
f

Fleshgrinder

03/22/2018, 10:35 AM
But requires special build setup with separation into different directories and what not. Package private is a little easier to apply ad-hoc.
r

romster

03/22/2018, 10:36 AM
Except the code organisation, that package-stuff in java made some methods more testable without necessity to make them public. Maybe it wasn't idiomatic, but it was a very common approach. Kotlin closes this 'backdoor', but doesn't offer any way to test not-public methods, as I can see.
c

Czar

03/22/2018, 10:41 AM
generally, I believe, private methods should not be tested, if they have to be tested - something is wrong in the design or you're "overtesting" things.
f

Fleshgrinder

03/22/2018, 10:42 AM
You can easily access your internal stuff in your tests if you set it up with a dedicated module and I agree private stuff should not be tested.
r

romster

03/22/2018, 11:07 AM
Well, thank you, guys. But I can't agree with this position, because I prefer to limit public API and to decompose one big method into small 'private' (not only class-private) pieces (which could be easily tested, but it's not allowed). This approach with "do not test private method, or make them public" seems restriction-driven to me. Anyway, do you know any open-source project with Kotlin modules which you can call "reference-implementation" of good structure ?
c

Czar

03/22/2018, 2:53 PM
There are no "official" ones, but you could take a look at cbeust's #kobalt build system, or #arrow library or any other popular opensource app/lib written in kotlin, plenty of them on GitHub
And it's not really
do not test private method, or make them public
It's more like "Test all public methods, so that private ones don't need to be tested. If by testing the public method you cannot ensure correctness, and absolutely need to test private methods, then most probably there's something dangerously wrong in the design." I understand that
internal
stuff is not truly private and should be tested, that just means: keep your tests in the same module as your code.
e.g. in my project (I use gradle) if I have internal class (or a class with internal methods) and I'm testing it, this setup works perfectly fine:
/src/main/kotlin/com/example/InternalClass.kt
/src/test/kotlin/com/example/InternalClassTest.kt
f

Fleshgrinder

03/22/2018, 5:29 PM
@romster the last example from @Czar show exactly what I was talking about. With private I really meant private and tiny helpers that are only there to make the code more readable, hence, easy to test automatically via the public stuff that uses them. However,
internal
is different and you can easily test them. As I said, it's more cumbersome to set up compared to a proper package visibility and I would love to see it in Kotlin but right now that's what we have:
/lib/src/main/kotlin/com/example/PublicClass.kt
/lib/src/main/kotlin/com/example/InternalClass.kt
/lib/src/test/kotlin/com/example/PublicClassTest.kt
/lib/src/test/kotlin/com/example/InternalClassTest.kt

/app/src/main/kotlin/com/example/NoAccessToInternalClass.kt