在上一篇中看過初步的Coding Style介紹,我們在來看看
https://developer.android.com/kotlin/style-guide#package_names
Package names are all lowercase, with consecutive words simply concatenated together (no underscores).
可以看到package name建議全小寫,如果有兩個單字也是一樣,直接黏再一起命名,但這邊似乎跟kotin官方的命名建議是不同的,之前看到是允許使用底線
// Okay
package com.example.deepspace
// WRONG!
package com.example.deepSpace
// WRONG!
package com.example.deep_space
Class names are written in PascalCase and are typically nouns or noun phrases. For example,
Character
orImmutableList
. Interface names may also be nouns or noun phrases (for example,List
), but may sometimes be adjectives or adjective phrases instead (for example Readable).
而測試用的檔案,建議是在尾巴加上Test
而非在開頭處
Test classes are named starting with the name of the class they are testing, and ending with
Test
. For example,HashTest
orHashIntegrationTest
.
https://developer.android.com/kotlin/style-guide#function_names
函式名稱在Test class中是允許使用底線的,通常都是透過動詞或者動名詞開頭,並且是小寫
Function names are written in camelCase and are typically verbs or verb phrases. For example,
sendMessage
orstop
.
Underscores are permitted to appear in test function names to separate logical components of the name.
@Test fun pop_emptyStack() {
// …
}
然後Android來看如果是擁有@Composable
標注且回傳Unit
的,通常會用名詞且大小開頭
Functions annotated with @Composable that return Unit are PascalCased and named as nouns, as if they were types.
而名稱是不能包含空白,因為並非在所以平台都支援,大部分只會在testing中這樣使用
Function names should not contain spaces because this is not supported on every platform (notably, this is not fully supported in Android).
// WRONG!
fun `test every possible case`() {}
// OK
fun testEveryPossibleCase() {}
在java中會透過static final的方式來對常數進行命名,並且透過全大寫的方式,而kotlin中和何謂常數?
Constant names use UPPER_SNAKE_CASE: all uppercase letters, with words separated by underscores. But what is a constant, exactly?
這邊有解釋說到,當變數是val
宣告,並且沒有custom get function
的時候,內容為不可變,沒有其他副作用的變動性時候
Constants are val properties with no custom get function, whose contents are deeply immutable, and whose functions have no detectable side-effects. This includes immutable types and immutable collections of immutable types as well as scalars and string if marked as const. If any of an instance’s observable state can change, it is not a constant. Merely intending to never mutate the object is not enough.
來看看這些例子
const val NUMBER = 5
val NAMES = listOf("Alice", "Bob")
val AGES = mapOf("Alice" to 35, "Bob" to 32)
val COMMA_JOINER = Joiner.on(',') // Joiner is immutable
val EMPTY_ARRAY = arrayOf()
當中的 COMMA_JOINER
其實是一個不可變性的結果,細節大家可以再去查看一下原文
These names are typically nouns or noun phrases.
Constant values can only be defined inside of an object or as a top-level declaration. Values otherwise meeting the requirement of a constant but defined inside of a class must use a non-constant name.
Constants which are scalar values must use the const modifier.
非常數名稱通常雖然為val
,然後他是透過小寫命名,通常都是一些物件的實體或者實例
Non-constant names are written in camelCase. These apply to instance properties, local properties, and parameter names.
These names are typically nouns or noun phrases.
https://developer.android.com/kotlin/style-guide#camel_case
這段我覺得比較有趣,駝峰式命名,當你遇到一個全大寫的專有名詞,到底你該不該用全大寫去進行命名呢?像是IPv6
or iOS
,你可以根據以下規則
Sometimes there is more than one reasonable way to convert an English phrase into camel case, such as when acronyms or unusual constructs like “IPv6” or “iOS” are present. To improve predictability, use the following scheme.
Convert the phrase to plain ASCII and remove any apostrophes. For example, “Müller’s algorithm” might become “Muellers algorithm”.
移除一些多餘的符號,譬如 Mueller's
變成 Muellers
Divide this result into words, splitting on spaces and any remaining punctuation (typically hyphens). Recommended: if any word already has a conventional camel-case appearance in common usage, split this into its constituent parts (e.g., “AdWords” becomes “ad words”). Note that a word such as “iOS” is not really in camel case per se; it defies any convention, so this recommendation does not apply.
下面是他建議的,大致上就是可以把有單字拆開,然後一樣依照駝峰命名來拼湊,然後像是YouTube
本身可能就是有大小寫就可以持續用這樣方式命名,不過他後面的Youtube
也是被接受的,但不推薦,iOS
會變成 Ios
,XML
變成Xml
,
這邊官方有一些範例
又或者你不知道該怎麼做的時候,可以透過chatGPT或bard 來幫忙,
https://g.co/bard/share/68c926e09fe4
上面只是一個範例,不一定正確,你可以用你自己認為的方式去調整,但大方向就是
ADWord
to adWord
is NIKE
to isNike
類似這樣的規則,還有很多細節可以在官網上看到,多看看遵循官方的Coding Style,或是可以善用工具在commit前進行code format也都是一個很好的方法,有機會再介紹kotlin官方的規範