scala-style-guide
scala-style-guide copied to clipboard
Clarify that private variables should not be prefixed with an underscore
I agree with the majority of posters in this thread that private variables should not be prefixed with underscores.
In the Spark codebase, the majority of private variables are not prefixed with an underscore, but there are a lot of exceptions. Run ack private\ val\ \_
.
Please provide some more guidance if I'm missing something (e.g. certain private variables should be prefixed with underscores and others should not). Thanks!
So this has been debated in the past and a lot of people with experience in Java / C++ are used to writing underscores. I personally don't have an issue with it.
@rxin - I did some research on how some popular open source Scala projects name private variables.
project | # of private vals prefixed with underscore | # of private vals | # of total vals |
---|---|---|---|
scalaz | 0 | 12 | 3,696 |
spark | 77 | 1,991 | 71,390 |
scalding | 0 | 57 | 6,447 |
playframework | 2 | 165 | 8,974 |
akka | 28 | 836 | 25,585 |
Here are the commands I used to generate the counts:
-
num private vals prefixed with underscore:
ack private\ val\ \_ --type=scala | wc -l
-
num private vals:
ack private\ val --type=scala | wc -l
-
num total vals:
ack val --type=scala | wc -l
Most private vals are not prefixed with underscores in the selected projects. I think a codebase should be consistent, pick one convention and stick with it.
It seems like the Scala community generally favors not prefixing private variables, so I think my pull requests stands. I am open to your thoughts / comments. Thanks!
The issue is that what is the convention for naming a private variable that has a public method with the same name? Prefix underscore is a very natural way to do this (even in other Scala projects).