Skip to main content
26 votes
Accepted

Should one prefer a generic version of a function, even if it's not re-used (yet)?

There are definitely cases where solving a more general problem than required makes code easier to read, to reason about and to maintain. The most simple example I can think of is when code deals with ...
Doc Brown's user avatar
  • 221k
21 votes

When to write extension methods for your own classes?

Extension methods do not provide something that can't be done in other ways. They are syntactical sugar to make development nicer, without providing an actual technical benefit. Extension methods are ...
Flater's user avatar
  • 59.5k
14 votes

Should one prefer a generic version of a function, even if it's not re-used (yet)?

There's has been some back and forth in the comments, and my feedback generally boils down to the same argument every time: Your problem, as you describe it, always starts from the really unusual ...
Flater's user avatar
  • 59.5k
9 votes

Why Kotlin doesn't allow assignments as expressions?

The only people who can answer this question are the designers of Kotlin. However, we can make some educated guesses: Assignment is a fundamentally imperative operation. In pure functional programming,...
Jörg W Mittag's user avatar
8 votes

Why Kotlin doesn't allow assignments as expressions?

Kotlin allows named arguments, which also uses the = as separator. A function fun foo(bar: Int) might be called as foo(42) or foo(bar = 42). Thus, assignment cannot occur within expressions without ...
amon's user avatar
  • 136k
7 votes

When to write extension methods for your own classes?

I agree with Flater that there hasn't been enough time for convention to crystallize, but we do have the example of the .NET Framework Class Libraries themselves. Consider that when LINQ methods were ...
James Jensen's user avatar
5 votes

Is there ever a reason in Kotlin to use threads over coroutines?

Independent of the language, there may be operating system or external library requirements which mandate that some code runs in separate threads. For example, a library may use thread local storage ...
Hans-Martin Mosner's user avatar
5 votes

How to test functionality that requires a certain internal state?

Tests should run fast. Now sure, you could set up each test by using processCommand to process every commend needed to get the game into the state you want to test but that's pitifully slow. The ...
candied_orange's user avatar
5 votes
Accepted

Is there ever a reason in Kotlin to use threads over coroutines?

Coroutines try to hide the existence of real threads from your programming model. If required you can pin a coroutine to a thread (by not using suspend functions) so the functionality is largely ...
DavidT's user avatar
  • 4,647
4 votes
Accepted

Is using KDoc/Javadoc comments inside of a function considered bad practice?

You ask "is it considered bad practice", and the answer is Yes. But not so much because of using KDoc/Javadoc syntax in an inappropriate place, but because of the level of commenting. If you ...
Ralf Kleberhoff's user avatar
4 votes

When to write extension methods for your own classes?

The driving force behind extension methods is the ability to add functionality you need to all classes or interfaces of a certain type regardless of whether they are sealed or in another assembly. ...
Berin Loritsch's user avatar
4 votes

Using a shared enum across 15 nanoservices

If the single big enum is used as a key value in a single message property to allow readers to determine the expected message format and content, then there are some consequences to breaking it into ...
joshp's user avatar
  • 3,511
4 votes

What to name a method which reads and sets value without return value?

You want a name like this to be meaningful within the context it is called. You've showed us the code defining the method, but you haven't showed us how this method is being called. Context matters, ...
Greg Burghardt's user avatar
3 votes
Accepted

Separation of concerns: persisting complex types

If you need some serialization code which is specific for certain Handler implementations, my first choice would be to make it part of the type itself (i.e. derive the handler directly from interface ...
Doc Brown's user avatar
  • 221k
3 votes

Should one prefer a generic version of a function, even if it's not re-used (yet)?

I do think it depends. If you are the only developer who will touch the code, I would write a generic only if you can actually foresee to, say, greater than 50% probability, usage for multiple types. ...
wchlm's user avatar
  • 149
3 votes
Accepted

When is the application of Kotlin infix functions appropriate?

infix should be treated like the syntactic sugar that it is: If it aids in the readability and flow of your code? Use it. Otherwise? Don't. That's really the only hard/fast rule. Mind that just b/c ...
incongruentDoughnut's user avatar
3 votes

What is the reasoning behind Kotlin using non-nullable types for Java interop methods?

Is there any document providing the reasoning behind this design? It is explained in the documentation under Calling Java code from Kotlin – Null Safety and Platform Types [bold emphasis mine]: Any ...
Jörg W Mittag's user avatar
2 votes

Is using KDoc/Javadoc comments inside of a function considered bad practice?

It's just not useful to do that. The purpose of KDoc or JavaDoc is to run a tool to generate HTML documentation for how to use your code. They way the tool works is it scans for the specially ...
Berin Loritsch's user avatar
2 votes

Approach for using multiple implementation of one interface for a single class, optional to use all or most of them

The idea of a class AuthHost implemented in terms of an interface Auth is that the class stays generic, with no compile time knowledge which of the available Auth implementations will be actually used ...
Doc Brown's user avatar
  • 221k
2 votes
Accepted

Android + Kotlin + Hilt + multi-module app: Should I "migrate" all classes with static methods to "injection"?

There is no best practice for this. Instead, consider the reasons why an object instance is desirable: The object needs to maintain state in between method invocations. You need to support ...
Greg Burghardt's user avatar
2 votes

What is the relationship between Java Library and Kotlin?

Basically kotlin can be thought of mostly Java with much much better syntax and a handful of very useful libraries. But any real world kotlin project is going to be using lots of base java libraries. ...
whatsisname's user avatar
  • 27.7k
2 votes

Is there ever a reason in Kotlin to use threads over coroutines?

I have no real experience with using Kotlin but I'm thinking back to the last time I worked directly with threads in Java. I'm not even sure I actually went with that approach, but I can remember why ...
JimmyJames's user avatar
  • 31.1k
2 votes
Accepted

Which is the best approach I should follow use private fields in data class primary constructor or use Interfaces Inheritance?

In my point of view private variables with getter and setter and interfaces are two different levels of encapsulation. With private variables and the use of getter and setter methods, you can change ...
JanRecker's user avatar
  • 1,584
2 votes
Accepted

Is returning Result types the standard way of dealing with errors in Kotlin?

I think this is almost the ideal way of dealing with exceptions in Kotlin. Dealing with the Result objects is used very often in Kotlin because it makes it easier to make business logic decisions. On ...
João Esperancinha's user avatar
1 vote

Is there ever a reason in Kotlin to use threads over coroutines?

Similar to virtual threads, you likely gain nothing from coroutines if all you do is local computation. Instead you have to pay the price to manage them. When using a thread pool you only pay the ...
Frank Hopkins's user avatar
1 vote

Should one prefer a generic version of a function, even if it's not re-used (yet)?

What it actually does is not that important for this example Where you define it is. Sweeping generic utility functions often have no benefit of being part of the class which often implements it. ...
tresf's user avatar
  • 119
1 vote

Which is the best approach I should follow use private fields in data class primary constructor or use Interfaces Inheritance?

My general preference is "getters and setters." For a few practical and maybe philosophical reasons. • "Philosophically," I don't think that "how it works" ever belongs ...
Mike Robinson's user avatar
1 vote

Interface or Object with Type?

I think I have a solution your problem using Generics. Shouldn't impact performance at runtime: interface Vertex<T : Vertex<T>> { fun distance(other: T): Double } class VertexGeo : ...
timj11dude's user avatar
1 vote

Do kotlin libraries with inline APIs encourage high coupling and discourage unit testing?

The main problem is that reification and inlining are currently tied together, which has some nice implementation properties, at the expense of some limitations in the interface. Kotlin hints in their ...
Karl Bielefeldt's user avatar
1 vote

Should one prefer a generic version of a function, even if it's not re-used (yet)?

In the meantime, I've learned more about the underlying principle, this decision can be based on, and also found a better minimal example. It can be found in this article ("Why a generic ...
Tobias Hermann's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible