Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private property name doesn't match regex

The following code returns this warning in Kotlin project using Android studio 3.0 on Mac.

private val REQUEST_CODE = 11 
private val TAG = "RecentCallsActivity"

Private property name 'REQUEST_CODE' doesn't match regex '_?[a-z][A-Za-z\d]*' less... (⌘F1) Reports private property names that do not follow the recommended naming conventions.

What is the recommended naming convention ?

I found , a similar question, that answers how to disable the same.

enter image description here

Update: In some examples , I have seen this usage which removes the warning.

class KotlinExampleActivity : Activity() {

companion object {
    val TAG: String = KotlinExampleActivity::class.java.simpleName
}
like image 763
Ashildr Avatar asked Nov 06 '17 10:11

Ashildr


3 Answers

it is a warning and you could ignore it. It bothers you, declare them as private const

private const val REQUEST_CODE = 11 
private const val TAG = "RecentCallsActivity"

class RecentCallsActivity : AppCompatActivity() {

Or you could declare the const val in a companion object. The main difference between the two approaches Is the in the latter you will create a pointless additional object (the companion).

like image 168
Blackbelt Avatar answered Nov 12 '22 17:11

Blackbelt


Kotlin default to the Java Coding Conventions. So use of lowerCamelCase is suggested for Variables. Full caps naming is suggested for Constant variables. More info here https://kotlinlang.org/docs/reference/coding-conventions.html http://www.oracle.com/technetwork/java/codeconventions-135099.html

like image 43
Febi M Felix Avatar answered Nov 12 '22 18:11

Febi M Felix


I believe it requires you to use camel case style of naming for example, instead of REQUEST_CODE use requestCode or another option is to put it inside a companion object for example

 companion object {
        private const val TAG: String = "RecentCallsActivity"
    }
like image 4
EmmaJoe Avatar answered Nov 12 '22 17:11

EmmaJoe