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)
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().
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.
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.
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().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With