Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

195
Visualizações
Simpler way to write x == 1 && y == 1 && z == 1 in Kotlin?

I was wondering if there is a simpler or shorter way to write repetitive conditions like x == 1 && y == 1 && z == 1?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You could consider using predicates using all. For example:

listOf(x, y, z).all {it == 1}

This will return true when x==1, y==1, and z==1.

If the goal is to shorten what you want, there's not much legroom as your boolean expression is already very concise. Some of the other answers have illustrated how to make what you want more readable, however.

over 4 years ago · Santiago Trujillo Relatório

0

When it is (exactly) repeated code, you should consider to extract the statement into a method and give a meaningfull name, which you know from the context where it is used. This makes the requirement (reading the code) easier to understand, at the point where it is used. And it makes it also easier to spot the it is always the same condition.

if (conditionName(x, y, z)) {

}

fun boolean conditionName(int x, int y, int z) {
    return x == 1 && y == 1 && z == 1;
}

I cannot think of a shorter statement for the condition, but a method extraction will improve your code readability, which should be your overall goal.

over 4 years ago · Santiago Trujillo Relatório

0

You can make a convenience function for this kind of repeated code (or if you just need it to be more readable, or safer to edit):

fun <T> T.allEqual(vararg items: T) = items.all { it == this }

// or you could make the receiver the first parameter instead, this is just
// more explicit about which item is the value being compared against
1.allEqual(x, y, z)

But no, there isn't any other shorthand built into the language as far as I'm aware - conditions like x == 1 chained with boolean operators are about as simple as it can get! If you want to check multiple things without repeating yourself, any and all make that easy, while being flexible for all kinds of situations, and allowing the user to put together the functionality they need with those more general functions.

If you specifically want a version of all that does a simple comparison to a single value, and doesn't require creating an iterable like listOf, you have to write your own with those tools (which is basically what I've done). It's up to you if you think it's worth it!

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda