SlideShare una empresa de Scribd logo
1 de 25
Betabeers Murcia
30/6/2016
@jdmuriel
Cómo ser más feliz, ligar más
y tener más pelo programando en
KOTLIN
Jesús L: Domínguez Muriel
@jdmuriel
Betabeers Murcia
30/6/2016
@jdmuriel
¿Cómo ser más feliz?
• Tomar cañas con los amigos
– (Hay estudios: Close relationships, more than
personal satisfaction or one’s view of the
world as a whole, are the most meaningful
factors in happiness. – Magen, Birenbaum,
and Pery 1996)
• Aprender cosas nuevas
Betabeers Murcia
30/6/2016
@jdmuriel
Kotlin y yo
• Un poco oxidado como
programador
• Todo empezó con la
Google Code Jam
• Python, Groovy, Java…
• ¿Qué es eso de Kotlin de
lo que hablan tanto?
Betabeers Murcia
30/6/2016
@jdmuriel
Kotlin y yo
• Sin querer acabé en la Tuenti Challenge 6
• Ahora no puedo dejarlo 
Betabeers Murcia
30/6/2016
@jdmuriel
Aprender mola, pero…
Betabeers Murcia
30/6/2016
@jdmuriel
Aprendiendo Kotlin
• Kotlin es fácil (al menos para los
programadores Java):
fun main (args: Array<String>) {
println("Hello, world")
}
• Java “Pythonizado”, esta vez con tipado
fijo, no como Groovy
Betabeers Murcia
30/6/2016
@jdmuriel
Fácil de aprender
• Sintaxis muy similar a Java. Diferencias:
– var, val para definir variables
– Los tipos se marcan detrás
• Interoperabilidad completa con Java
– Tipos equivalentes
– Fácil usar cualquier* librería
– Puede utilizarse desde Java (o Groovy)
• Genera bytecode para Java 6
Betabeers Murcia
30/6/2016
@jdmuriel
Extension functions
fun File.miMetodo() {…}
f = File()
f.miMetodo()
• Geniales para acceder a funciones de
librería
• Los principales métodos de java.util,
java.lang.text, etc. están añadidos a las
clases String, Int, File
• Gran parte de las novedades de Kotlin se
implementan así.
Betabeers Murcia
30/6/2016
@jdmuriel
Cómo ligar más
• Para ligar hay que cuidarse
• Para cuidarse hace falta tiempo
• Kotlin ahorra un montón de tiempo
Betabeers Murcia
30/6/2016
@jdmuriel
Cómo ligar más
Usa Java Usa Kotlin
Betabeers Murcia
30/6/2016
@jdmuriel
Ahorro de tiempo
• El tipo se infiere
• No hay New: var s = Objeto()
• No hay ;
• Getters y setters automáticos
(pueden redefinirse o no)
• Parámetros por defecto y con nombre
objeto.member += objeto.método(param="ddd")
Betabeers Murcia
30/6/2016
@jdmuriel
Menos verboso
class EmailMessage2 implements Serializable {
private final String toAddress;
private final String subject;
private final String body;
public EmailMessage2(String toAddress, String
subject, String body) {
this.toAddress = toAddress;
this.subject = subject;
this.body = body;
}
public String getToAddress() {
return toAddress;
}
public String getSubject() {
return subject;
}
public String getBody() {
return body;
}
public boolean equals(Object o) {
return false;
}
public int hashCode() {
return false;
}
}
Data class EmailMessage2 (
val toAddress: String,
val subject: String,
val body: String ): Serializable
Betabeers Murcia
30/6/2016
@jdmuriel
Menos verboso (estilo Python)
• Cadenas interpoladas
println (“El resultado es $resultado”)
• Se pueden incluir expresiones
println (“La suma de 3 + 4 es ${3+4}”)
• Funciones definidas con =
Fun hello (name: String) = “Hello $name”
• Asignación por componentes
(a, b) = getCoordinates()
Betabeers Murcia
30/6/2016
@jdmuriel
Permite usar lambdas
(que están de moda)
¿Cómo dices que llamas a .foldRightIndexed?
Betabeers Murcia
30/6/2016
@jdmuriel
Funciones lambda
• Las funciones son objetos de primer clase
fun suma (a: Int, b: Int) = a + b
var f : (Int, Int) -> Int
f = ::suma
println (f(5,5))
• Si se pasan como último parámetro, se pasan
tras los paréntesis, entre llaves
fun <T> Iterable<T>.forEach(action: (T) -> Unit): Unit {
for (element in this) action(element)
}
l.forEach { println (it) }
Betabeers Murcia
30/6/2016
@jdmuriel
Funciones para colecciones similar
al de Java 8 (pero en Java 6)
• Map
• Filter
• Fold
• forEach
• Te encuentras utilizándolas porque la sintaxis es
muy sencilla.
Betabeers Murcia
30/6/2016
@jdmuriel
Ahorra mucho código en Android
No más clases anónimas
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Click",
Toast.LENGTH_SHORT).show();
}
});
Se convierte en
view.setOnClickListener({ view -> toast("Click")})
view.setOnClickListener { toast("Click") }
Betabeers Murcia
30/6/2016
@jdmuriel
Una literal
function con
receiver y le
derrito el DSL
Una lazy
property más
y caerá en mi
data class
Betabeers Murcia
30/6/2016
@jdmuriel
Cómo tener más pelo
• Kotlin está pensado para evitar algunos
de los errores más comunes.
Betabeers Murcia
30/6/2016
@jdmuriel
Gestión de nulos
• Tony Hoare introduced Null references in
ALGOL W back in 1965 “simply because it was
so easy to implement”. He talks about that
decision considering it “my billion-dollar
mistake”.
• Las variables que admiten nulos deben tener un
tipo específico (String? en vez de String)
• var a : String = null //Error
• var b : String? = null //OK
• “abc”.contains(b) //Error, requiere String
Betabeers Murcia
30/6/2016
@jdmuriel
Gestión de nulos
• Te motiva a utilizar funciones que no devuelven
nulos, excepto en casos especiales (ayuda el
poder devolver dos resultados en un Pair)
• Operador ?. (llama si no nulo)
• Operador ?: (valor por defecto si nulo)
val a = getCell("x")?.substringAfter("-")?:""
Betabeers Murcia
30/6/2016
@jdmuriel
Buenas prácticas
• Sintaxis val / var,
inmutableListOf() =>
Favorecen el uso de
variables inmutables,
programación más
funcional
• Interfaces con código,
clases no heredables por
defecto =>
Reducen las jerarquías de
clases heredadas que
complican los diseños
Betabeers Murcia
30/6/2016
@jdmuriel
Resumen
• Fácil de aprender
• Interoperable con
Java
• Tipado estático,
inferencia
• Data class
• Cadenas
interpoladas
• Funciones de
extensión de clases
• Gestión de nulos
• Lambdas
• Métodos Java 8 para
colecciones
• Var/val, métodos
para listas, arrays,
sets inmutables
• Interfaces con código
• Clases cerradas
Betabeers Murcia
30/6/2016
@jdmuriel
¿Cómo ser más feliz?
…o aprende Kotlin ;-)
Betabeers Murcia
30/6/2016
@jdmuriel
Gracias
¿Preguntas?
Jesús L: Domínguez Muriel
@jdmuriel

Más contenido relacionado

Destacado

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 

Destacado (20)

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 

Cómo ser más feliz, ligar más y tener más pelo programando en Kotlin