Hi folks, i am developing an SDK for my applicatio...
# library-development
g
Hi folks, i am developing an SDK for my application's APIs and i have come across a dilemma, in requests like
getAccount(): Accounts
do you prefix your entities or not? Thanks in advance for any answer !
m
What do you mean? Something like
ExampleAccount
vs just
Account
?
g
In regular cases like Account the conflict with the consumer app is pretty much guaranteed
m
I guess "it depends"
If you're one of the mainstream libraries like Okio, you can go for brevity:
Buffer
,
Source
,
Sink
But if you're not, I would prefix
g
If i prefix only the most common (ExampleAccount) ones and the other which the conflict is quite hard to happen to leave them as they are, it would be weird?
j
you can avoid the conflict where it is happening using
as
on the import
Copy code
import foo.bar.Baz as Baz2
m
Yea, alias imports are a solution but I don't feel super comfortable about them
If i prefix only the most common (ExampleAccount) ones and the other which the conflict is quite hard to happen to leave them as they are, it would be weird?
I'd vote for consistency first but then again OkHttp is a counter-example. They have
OkHttpClient
and
Response
So I guess "it depends" each one personal taste
j
Same with ktor too
r
This is one of the very few places where I miss scala - the ability to import a package is really nice. I prefer this:
Copy code
import com.mything.web
import com.bank

val webAccount: web.Account
val bankAccount: bank.Account
to having to alias as you would in Kotlin.
j
Maybe @jessewilson or @jw can help us to understand that naming decision
@Rob Elliot you can workaround it by wrapping it with an object, and based on the file in which you are working you use Foo.Bar or you import Foo and use only Bar
g
Not a bad idea for `as`: if i were the consumer i wouldn't mind it, but i guess it depends on the consumer.
r
@Javier I don't follow - I'm assuming I don't control one or both of the types, I'm just a client of them. I can't nest a typealias inside an object.
g
@Rob Elliot Im a bit confused, can we not write something like this in kotlin?:
Copy code
val myServer = org.x.myProject.workhorse.entities.Server
r
I gather Kotlin had the ability to import a package but it was removed. There's this issue asking for it back: https://youtrack.jetbrains.com/issue/KT-9242/Consider-returning-import-package but it's from 7 years ago.
j
I was assuming we control one part
@George but partial import doesn’t work based on what @Rob Elliot said
r
@George yes, but that's really verbose particularly if you use the type a lot. In my example I'm only namespacing the type to the immediate parent package, not to the fully qualified package.
j
Two recommendations: • don’t be afraid of short names:
Request
,
Call
,
Json
,
Path
• don’t be afraid of short package names
okio
,
okhttp3
Very few programming languages have a
com.mycompany
prefix in their naming, and as far as I know the actual collisions in practice don’t come
If you get a collision, like say
okio.Path
and
java.nio.file.Path
, you can just fully-qualify the Okio one