Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

164
Vistas
Kotlin approach to perform an operation after any of condition blocks are executed

Getting to know Kotlin!

var flag = false;
if(condition) {
    //do one thing
    flag = true;
}
if(condition2) {
    //do another thing
    flag = true;
}
if(flag)
    save();

What is the right way to do this in Kotlin avoiding the flag approach?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I don't see any clever way to do it concisely in Kotlin, but in most cases I would probably prefer assigning conditions to variables over the flag approach:

val isBlue = ... some condition ...
val hasWings = ... another condition ...

if (isBlue) {
   ...
}
if (hasWings) {
   ...
}
if (isBlue || hasWings) {
   ...
}

Of course, it is a matter of personal taste.

Also, in some cases flag makes more sense. There is save() in your example, so if this flag is something like shouldSaveData and it depends on several conditions where for example the data is mutated, then flag approach makes perfect sense.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda