Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin- naming convention for boolean returning methods

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 validation. Should I call it isValidCard or cardValidation or another name? (I didn't find it here: https://kotlinlang.org/docs/reference/coding-conventions.html)

like image 644
Joel Avatar asked Apr 10 '17 07:04

Joel


People also ask

How do you name a boolean returning function?

The usual convention to name methods that return boolean is to prefix verbs such as 'is' or 'has' to the predicate as a question, or use the predicate as an assertion. For example, to check if a user is active, you would say user. isActive() or to check if the user exists, you would say user. exists().

How do I return a boolean value in Kotlin?

If the key is present, then the […] operator will return the corresponding Boolean value; but if the key is not present (which could happen if the map doesn't contain a "default" key), it returns null. However, the variable you're trying to assign it to is of type Boolean , which doesn't allow nulls.

How do you write a Boolean expression in Kotlin?

Kotlin Boolean Expression A Boolean expression returns either true or false value and majorly used in checking the condition with if...else expressions. A boolean expression makes use of relational operators, for example >, <, >= etc.


1 Answers

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().

like image 198
Lukas M. Avatar answered Sep 18 '22 07:09

Lukas M.