Guía de estilo de Kotlin para desarrolladores de Android (Parte II)

En principio, estoy de acuerdo con los comentarios de que este tema es superfluo, ya que existen herramientas de formateo de código automático





Y además, todos tienen su propia opinión sobre la belleza y la estética, por lo que el estilo de codificación es subjetivo.





Pero decidí terminar esta serie de artículos sobre el estilo Kotlin, como prometí.





Quizás alguien sea útil.





Bueno, por favor, ¡debajo del corte!





Nombrar

Los identificadores de variables, funciones, propiedades, clases usan solo letras y números ASCII (letras inglesas ordinarias + 10 dígitos)





(: _digit, power_



) (: Backing ).





.





: (: fetchDogs



) (makeRepost



)





, , :





@Test fun get_emptyList() {
  // ...
}
      
      



, @Composable



(Jetpack Compose ) , , Pascal :





@Composable
fun MyDog(name: String) {
    // …
}
      
      



, .





Kotlin - val



, get



, . :





(listOf(1, 2, 3)



), , const:





//          const
const val FIVE = 5	
val MY_DOGS = listOf("Dina", "Red")
val EMPTY_ARRAY = arrayOf()
      
      



object



( ).





, , .





, , . .





val viewModel by viewModels<DogViewModel> { viewModelFactory }
val firstName = "Anna"
val dogs = listOf(Dog("Dina"), Dog("Red"))
lateinit var binding: ListItemBinding

fun fetchDogs(page: Int): List<Dog> {
	// ...
}
                                   
                               
      
      



backing :





    private val _dogs = MutableLiveData<List<Dog>>()
    val dogs: LiveData<List<Dog>>
        get() = _dogs
      
      



generic :





  • ( , : T1, T2, R1



    )





  • generic , T (: RequestT, ResponseT



    )





, generic .





Pascal (: SleepingDog



- ).





(: MyCar



- )





. (: Cloneable, Readable, Writable



):





class MainActivity(): AppCompatActivity() {
  // ...
}

interface OnItemListener {
	fun onItemClick(id: Long)
}

interface Readable {}
      
      



: :





// 
package com.example.android.marsRealeState.overview

// 
package com.example.android.mars_reale_state.overview

// OK
package com.example.android.marsrealestate.overview

      
      



enum :





enum class NetworkStatus { 
  SUCCESS, FAILED, LOADING 
} 
      
      



: , :





enum class NetworkStatus {
  SUCCESS,
  FAILED,
  
  LOADING {
  	override fun toString() = "loading..." 
  }
}
      
      



, .





, :





//      
@Singleton
@Component(modules = [DatabaseModule::class])
interface AppComponent {
  // ...
}

//     ,       
@JvmField @Volatile
var disposable: Disposable? = null

//          :
@Inject lateinit var viewModelFactory: DogViewModelFactory
      
      



:





// 
override fun toString(): String = "My name is $name"
// 
override fun toString() = "My name is $name"

// 
private val redDog: Dog = Dog("Red")
// 
private val redDog = Dog("Red")

      
      



KDoc :





/**
 *   
 * 
 */
fun fetchDogs(page: Int) {
    // …
}
      
      



:





/**      */

      
      



:





  • , (*



    ),





  • : @constructor



    @receiver



    @param



    @property



    @return



    @throws,@see







  • KDoc , (: "This function returns sum of digits").





, KDoc , , , public API.





, , :





// ,   .
fun sum(a: Int, b: Int) = a + b
      
      



, , , .





: KDoc , .





.





, , : , , Android Studio, !





: " Android ?"





:





  1. Kotlin ( )





  2. Backing





  3. Generic





  4. Jetpack Compose





  5. Kotlin ( )
















All Articles