Home > Back-end >  Why does the official document use uppercase letter for a val variable?
Why does the official document use uppercase letter for a val variable?

Time:09-02

The Code A is from the official document, val Yellow200 use uppercase letter, but in many sample projects I can find Code B which is lowercase.

I think Code B is good way, yellow200 is not a const which is often using uppercase letter.

Why does the official document use uppercase letter for a val variable?

Code A

val Yellow200 = Color(0xffffeb46)

Code B

val yellow200 = Color(0xffffeb46)

CodePudding user response:

This is a standard naming convention in Jetpack Compose, and is described in the API Guidelines. It's used throughout the framework and libraries, and can optionally be followed by app developers too.

Jetpack Compose framework development MUST name deeply immutable constants following the permitted object declaration convention of PascalCase as documented here as a replacement for any usage of CAPITALS_AND_UNDERSCORES. Enum class values MUST also be named using PascalCase as documented in the same section.

Library development SHOULD follow this same convention when targeting or extending Jetpack Compose.

App Development MAY follow this convention.

https://android.googlesource.com/platform/frameworks/support/ /androidx-main/compose/docs/compose-api-guidelines.md#singletons_constants_sealed-class-and-enum-class-values

In the same document they provide some justification for the decision, if you're interested in reading more. The source they're referencing there is the Kotlin coding conventions, which actually give a bit of leeway on whether property names should start with an uppercase letter. It's not as simple as "all variables are lowerCamelCase".

What the Kotlin style guide says is:

Names of top-level or object properties which hold objects with behavior or mutable data should use camel case names:

val mutableCollection: MutableSet<String> = HashSet()

Names of properties holding references to singleton objects can use the same naming style as object declarations:

val PersonComparator: Comparator<Person> = /*...*/

https://kotlinlang.org/docs/coding-conventions.html#property-names

So in general Kotlin code the choice of whether to use a lowercase or uppercase initial letter is somewhat left up to you, depending on how the variable is used.

By "singleton objects" they might mean just objects declared using an object expression, but it's not too much of a leap to extend the same naming convention to things like individual immutable instances of a Color class.

The same UpperCamelCase naming convention can also be used for enum entries. The Color instances in your example are very similar to enum entries in their use and purpose, which I think is another argument in favour or preferring the uppercase initial letter here.

The current version of the Android Kotlin style guide is a bit less permissive here. It suggests that all variable names should be lowerCamelCase, except for constants which should be UPPER_SNAKE_CASE. On that basis, the names of the Color variables should start with a lowercase letter.

I think both your 'Code A' and 'Code B' are stylistically okay, and there are good arguments on both sides. It depends on which style guide you read, and how you interpret it. It also depends, of course, on your own personal preference and on the shared conventions you might have agreed with the co-collaborators on your codebase.

CodePudding user response:

We are following the Kotlin convention here for names of properties referencing singleton objects.

https://kotlinlang.org/docs/coding-conventions.html#property-names

CodePudding user response:

I think the "rules" and many would want a full uppercase YELLOW200.

Local constants however often deviate from this, with some (Kotlin) rule too. That is probably psychological, to blend in with the surrounding code.

Then there are things like standardized web color names, that are published in small letters. For instance Java started with both yellow and then reconsidered YELLOW.

  • Related