https://kotlinlang.org logo
#general-advice
Title
# general-advice
c

Christopher Hübner

03/12/2024, 8:20 AM
When to use inline functions? I thought, this case would be a nice example for inlining functions, but intellj ide recommendds to remove.
Copy code
private inline fun String.isValidFile() = File(this).exists() && File(this).isFile
e

ephemient

03/12/2024, 8:27 AM
there's generally no need to inline functions like that
the jit/optimizer can easily choose to inline it at callsites if it decides it's hot and the space tradeoff is worthwhile
1
if you explicitly
inline
, then you're always paying the space tradeoff, no choice
1
higher order functions that take a lambda are much better candidates since inlining reduces allocation and virtual calls, but neither is the case above
s

Sebastian Schuberth

03/12/2024, 8:46 AM
Apart from that,
isFile == true
implies
exists() == true
, so just use the former.
k

Klitos Kyriacou

03/12/2024, 9:32 AM
I would use the newer
java.nio
features instead of the old
<http://java.io|java.io>
ones:
Copy code
private fun String.isValidFile() = Path(this).isRegularFile()
1