When to use inline functions? I thought, this case...
# general-advice
c
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
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
Apart from that,
isFile == true
implies
exists() == true
, so just use the former.
k
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