I find the `CanBeParameter` inspection annoying. T...
# intellij
m
I find the
CanBeParameter
inspection annoying. That’s the one that warns when you can remove
val
from a constructor param. As I move code around in a class I’m having to arbitrarily change wether I need
val
or not. But as far as I can tell there is no benefit for private vals. Before I disable the inspection i’m wondering what am I missing?
e
The
val
must always be stored, whereas the non-
val
is just a parameter when constructing the instance and then discarded?
m
Ok so maybe a slight memory advantage if the reference can be garbage collected. Not worth it in most cases then.
e
Also, you’re not bogging down your class with unnecessary stuff.. Keep stuff around (as `val`s) and someone will eventually start depending on that data being there 🙃
m
I’m not talking about the public api. Just private vals.
e
Someone in this case might be someone else working in the same codebase, or you yourself a few months/years down the line 🙂
j
Yup! the more vals and vars in scope, the more complex your code is. If a value is not needed after construction it shouldn't be available for your other class functions to access.
Its a form of the law of demeter
m
I’m really sceptical of the value of trying to limit a class’s access to code that is inside the class. Law of demeter talks about objects talking to other objects, not talking to itself.
And its not like the object can’t access it, it’s that you can only access it in limited ways
Copy code
class Foo (a: Int) {
  private val b = a // can access here
  private fun c() = a // cannot access here
}
Seems arbitrary. And no developer is going to be stopped from using
a
in
fun c()
they’ll just change
a
to
val
as soon as they need it.