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

Este artículo cubre no solo problemas de formato estético, sino también otros tipos de convenciones y estándares que los desarrolladores de Android deben conocer.





El enfoque principal es, en primer lugar, las rígidas reglas que siguen los desarrolladores de Google en todas partes.





Al principio pensé que el artículo sería pequeño, pero debido a la gran cantidad de ejemplos de código, ha crecido lo suficiente.





Entonces decidí dividirlo en dos partes.





Ambas partes contienen una descripción de los estándares de código en el lenguaje de programación Kotlin.





:





  • , , , ..









  • - , , , , .









().





!





, .





UTF-8 .





, , : + .kt





( enum



) , :





// PhotoAdapter.kt

class PhotoAdapter(): RecyclerView.Adapter<PhotoViewHolder>() {
	// ...
}


// Utils.kt

class Utils {}

fun Utils.generateNumbers(start: Int, end: Int, step: Int) {
	// ...
}

// Map.kt

fun <T, O> Set<T>.map(func: (T) -> O): List<O> = // ...
fun <T, O> List<T>.map(func: (T) -> O): List<O> = // ...
      
      



Kotlin .kt :





  • , ()





  • ,





  • package





  • import





  • (, , )





:





/*
 * Copyright 2021 MyCompany, Inc.
 *
 *
 */
      
      



KDoc :





/** 
 * Copyright 2021 MyCompany, Inc.
 *
 */

// Copyright 2021 MyCompany, Inc.
//
      
      



@file



, use-site target package



:





/*
 * Copyright 2021 MyCompany, Inc.
 *
 */

@file:JvmName("Foo")

package com.example.android
      
      



package



import



:





package com.example.android.fragments  //  

import android.view.LayoutInflater //    
import android.view.View
      
      







import



, .





:





 import androidx.room.*  //    
      
      



Kotlin , , typealias



.





. extension , .









, .





, .





: , ,





, .





ASCII (0x20).





, :









  • Tab





, (\b, \r, \t, \\



) , Unicode (: \u000a



).





, ASCII, Unicode (∞), Unicode (\u221e



).





, :





//  :   
val symbol0 = "∞"	

// :       Unicode 
val symbol1 = "\u221e" // ∞	

// :    ,     
val symbol2 = "\u221e"

// :  Unicode    
return "\ufeff" + content	//    

      
      



!





when



if



( if



else



):





if (str.isEmpty()) return

when (option) {
    0 -> return
    // …
}
      
      



if, for, when



do



while



:





if (str.isEmpty())
    return  //   !

if (str.isEmpty()) {
    return  // OK
}
      
      



:









  • c









  • , , , .





class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        // ...
    }
}
      
      



K&R:





try {
    val response = fetchDogs("https://api.dog.com/dogs")
} catch (e: Exception) {} // 

try {
    val response = fetchDogs("https://api.dog.com/dogs")
} catch (e: Exception) {
} // OK
      
      



if/else



, :





val value = if (str.isEmpty()) 0 else 1  // OK

val value = if (str.isEmpty())	// 
	0
else
	1

val value = if (str.isEmpty()) { 	// OK
	0
} else {
	1
}

      
      



4 . ( ).





(;



)





100 .





:





  • , (: URL)





  • package



    import







  • , shell





:





  • infix .





  • , :





    • (.



      , .?



      )





    • (::



      )









  • (,)







  • (->



    ) lambda





, ( 4 ):





fun makeSomething(
  val param1: String,
  val param2: String,
  val param3: Int
) {

}
      
      



:





override fun toString(): String {
	return "Hello, $name"
}

override fun toString() = "Hello, $name"
      
      



, - - :





fun waitMe() = runBlocking {
	delay(1000)
}
      
      



(=



):





 val binding: ListItemBinding = 
 	DataBindingUtil.inflate(inflater, R.layout.list_item, parent, false)
      
      



get



set



(4 ):





 val items: LiveData<List<Item>>
 	get() = _items
      
      



Read-only :





val javaExtension: String get() = "java"

      
      



:





  • : , ,





    • . ( backing )









  • ()





( ) ASCII :





  • , : if



    , for



    catch



    :





// 
for(i in 1..6) {
}

// OK
for (i in 1..6) {
}
      
      



  • , else



    catch



    :





// 
}else {
}

// OK
} else {
}
      
      



  • :





// 
if (items.isEmpty()){
}

// OK
if (items.isEmpty()) {
}
      
      



  • :





// 
val four = 2+2

// OK
val four = 2 + 2

//        (->)

// 
items.map { item->item % 2 == 0 }

// OK
items.map { item -> item % 2 == 0 }
      
      



  • : (::



    ), (.



    ) range (..



    )





// 
val str = Any :: toString

// OK
val str = Any::toString

// 
item . toString()

// OK
item.toString()

// 
for (i in 1 .. 6) {
		println(i)
}

// OK
for (i in 1..6) {
		println(i)
}
      
      



  • Antes de dos puntos ( :



    ) para indicar una extensión de una clase base o interfaz, y en una when



    expresión para tipos genéricos:





// 
class Worker: Runnable

// OK
class Worker : Runnable

// 
fun <T> min(a: T, b: T) where T: Comparable<T>
  
// OK
fun <T> min(a: T, b: T) where T : Comparable<T>
      
      



  • Después de dos puntos ( :



    ) o coma ( ,



    )





// 
val items = listOf(1,2)

// OK
val items = listOf(1, 2)

// 
class Worker :Runnable

// OK
class Worker : Runnable
      
      



  • A ambos lados de la doble barra:





// 
var debugging = false//  

// OK
val debugging = false //   
      
      



Conclusión

Este artículo resultó ser bastante extenso, espero que le sea útil.





En el siguiente artículo: nombres, construcciones especiales y documentación.





Enlaces útiles:





  • Guía de estilo de Kotlin (en inglés)





  • Estilo K&R





  • Libro: Código limpio (Bob Martin)





  • Brevemente sobre el libro de Bob Martin





¡Espera la siguiente parte!












All Articles