Coming from a java background I always mark instance variables as private. I’m learning scala and almost all of the code I have viewed the val/var instances have default (public) access. Why is this the access ? Does it not break information hiding/encapsulation principle ?
Coming from a java background I always mark instance variables as private. I’m learning
Share
It would help it you specified which code, but keep in mind that some example code is in a simplified form to highlight whatever it is that the example is supposed to show you. Since the default access is public, that means that you often get the modifiers left off for simplicity.
That said, since a
valis immutable, there’s not much harm in leaving it public as long as you recognize that this is now part of the API for your class. That can be perfectly okay:Or it can be an implementation detail that you shouldn’t expose:
Here, we’ve littered our class with all of the intermediate steps for calculating standard deviation. And this is especially foolish given that this is not the most numerically stable way to calculate standard deviation with floating point numbers.
Rather than merely making all of these private, it is better style, if possible, to use local blocks or
private[this]defs to perform the intermediate computations:or
If you need to store a calculation for later use, then
private valorprivate[this] valis the way to go, but if it’s just an intermediate step on the computation, the options above are better.Likewise, there’s no harm in exposing a
varif it is a part of the interface–a vector coordinate on a mutable vector for instance. But you should make themprivate(better yet:private[this], if you can!) when it’s an implementation detail.