Kotlin- naming convention for boolean returning me

2019-06-15 02:29发布

问题:

How would like to know what is the naming convention for boolean returning methods? Using an 'is', 'has', 'should','can' in the front of method sound ok for some cases. but I'm not sure. Is there a better way to name such methods? for example: a function that checks card's validaition . should I call it isValidCard or cardValidation or another name? (I didn't find it here: https://kotlinlang.org/docs/reference/coding-conventions.html)

回答1:

Kotlin naming style assumes you use the Java naming conventions to the possible extend. I suggest you use this answer to the same question about Java.

UPDATE: they have released coding conventions http://kotlinlang.org/docs/reference/coding-conventions.html



回答2:

Something about naming convention for properties in Kotlin, I know it's not for methods. But it's related:

From book Kotlin in Action (by Dmitry Jemerov & Svetlana Isakova) - section 2.2.1 Properties:

In Kotlin, properties are a first-class language feature, which entirely replaces fields and accessor methods.

Listing 2.5. Declaring a mutable property in a class:

class Person {
    val name: String,      // read only property: generates a field and a trivial getter
    var isMarried: Boolean // writable property: a field, getter and a setter
}

Kotlin’s name property is exposed to Java as a getter method called getName. The getter and setter naming rule has an exception: if the property name starts with is, no additional prefix for the getter is added and in the setter name, is is replaced with set. Thus, from Java, you call isMarried().