Questions tagged [kotlin]
22 questions
35
votes
3 answers
Why is there no static keyword in Kotlin?
Kotlin is known primarily as a drop-in replacement for Java, but it gets rid of a well-known Java construct: the static keyword. Instead, that class-level functionality is offered mainly by companion objects.
What is wrong with static methods and…

user1446
- 472
- 5
- 7
18
votes
7 answers
Should one prefer a generic version of a function, even if it's not re-used (yet)?
YAGNI might tell us, that in the below implementation the generic version is not needed, as long as the function is only used once.
But to me personally, it seems, the generic version is more readable because I'm not distracted by all the…

Tobias Hermann
- 608
- 5
- 14
14
votes
4 answers
When to write extension methods for your own classes?
I recently saw a code base which had a data class Address defined somewhere and then in a different place:
fun Address.toAnschrift() = let { address ->
Anschrift().apply {
// mapping code here...
}
}
I found it confusing to not have…

Robert Jack Will
- 511
- 3
- 8
4
votes
1 answer
Do kotlin libraries with inline APIs encourage high coupling and discourage unit testing?
As an example, let's assume our application needs some way to communicate with other systems using HTTP.
interface HttpClient {
fun get(url: String, returnType: Class): T
fun post(url: String, body: Any)
}
Now it seems like good practice…

enp4yne
- 149
- 1
4
votes
2 answers
Why Kotlin doesn't allow assignments as expressions?
Coming from Java, I was surprised to find out that Kotlin doesn't allow assignments as expressions.
Is there a reason for that?
Java (Works)
@Test
public void test_x() {
List elements = null;
for (final String x :…

sero
- 149
- 1
- 3
1
vote
0 answers
Android Jetpack DataStore item separation
I am trying to migrate to Jetpack DataStore from good ol' SharedPreferences and there is one thing I am struggling to come to terms with and that is the amount/size of the data pulling out of DataStore at once.
SharedPreferences were asynchronous…

TheLibrarian
- 111
- 2
1
vote
1 answer
How to test functionality that requires a certain internal state?
I'm struggling to test functionality in a class where the class has to be in a certain state for the functionality to work, but the class cannot be put directly into a given state by design, to maintain state integrity.
I'm developing the back end…

gotube
- 127
- 4
1
vote
2 answers
Is using KDoc/Javadoc comments inside of a function considered bad practice?
I often find it helpful to use KDoc/Javadoc comments inside of a class or function instead of normal comments. IntelliJ colorizes them more obviously by default and, more importantly, allows hyperlinking to definitions. I'm wondering if it is…

Matt Robertson
- 113
- 4
1
vote
1 answer
What is the reasoning behind Kotlin using non-nullable types for Java interop methods?
Considering Kotlin Java Interop: Null Safety and Platform Types
Why is code like this legal in Kotlin?
fun envString(key: EnvVars): String {
return System.getenv(key.toString())
}
getenv() can return null, as indicated by its JavaDoc:
Returns:…

F.P
- 599
- 3
- 12
1
vote
0 answers
Common patterns for Observable data layer on iOS
I am working on learning native iOS development in Swift, and I am trying to find something that is similar to what I've learned in Android development with Kotlin.
In particular I am referring to the concept of having the db as the "single source…

papafe
- 171
- 4
1
vote
1 answer
Kotlin delegation, what should I test?
In Kotlin the powerful construct of delegation can be used to extend functionality of existing interfaces by reusing existing implementations.
class Demo : Map by HashMap {}
Questions:
What should I be testing? Testing hashmap from the example is…

Michiel Leegwater
- 115
- 7
0
votes
0 answers
Android, are seperate state update functions a DRY violation?
In my ViewModel I’ve implemented couple of functions to manipulate the state of my UI. It quickly becomes bigger and bigger and I wonder if these functions violate the DRY principle? I could wrap it all in a single function, but on the other hand…

deja
- 1
0
votes
1 answer
Android + Kotlin + Hilt + multi-module app: Should I "migrate" all classes with static methods to "injection"?
With the purpose of learning Hilt I started "migrating" my multi-module Kotlin app from using classes with static methods as helpers to Hilt injection.
After a lot of headaches, now everything's going fine, and I could say I'm happy and in the 80%…

Diego Perez
- 147
- 1
- 6
0
votes
1 answer
Android + Kotlin + Hilt: Dependency Injection vs Static Methods
I've already read this carefully, but still need more clarification.
I'm not new to dependency injection, but new to Hilt, and trying to implement Hilt in my multi-module app.
The reason?
I currently have a static application class wich not only…

Diego Perez
- 147
- 1
- 6
0
votes
0 answers
Is splitting Android activities/classes into many Kotlin extension functions a good or bad practise?
I have been working on an Android application for some time, and ever since the beginning I've developed a practice of splitting my class's (mostly activity/fragment) code up into multiple files.
This is because the Udemy course from which I learned…

thebluepandabear
- 101
- 3