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

139
Visualizações
How to efficiently shorten these if statements?

I am working on a project and in the code I added a few if statements. I was then told that I can do it on line and more efficiently. The way I did works perfectly but I need to refactor to get it accepted. Could you please help me out? I have tried ternary operator as you can see in the examples below but it's still not that short

Assuming that we have two arrays arr1 and arr2 and the following code is implemented to check if their lengths.

const hasValArr1 = ():boolean => return arr1.length > 0
const hasValArr2 = ():boolean => return arr2.length > 0

Now the interesting part if statements

const isEmpty():boolean => {

if (!hasValArr1() && !hasValArr2()) return false

else if (hasValArr1() && hasValArr2()) return true 

else if (!hasValArr1() && hasValArr2()) return true 

else if (hasValArr1() && !hasValArr2()) return true

}

using ternary operator

 (!hasValArr1() && !hasValArr2()) ? false

:(hasValArr1() && hasValArr2()) ? true 

:(!hasValArr1() && hasValArr2()) ? true 

:(hasValArr1() && !hasValArr2()) && true

How would you go to write this in a more readable and efficient way? Thanks in advance!

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

0

The implementation does not match the name of the method. The name of the method is isEmpty but it returns false if both arrays don't have a value: if (!hasValArr1() && !hasValArr2()) return false

So the name should be: hasAnyValue or doArraysHaveAnyValue or something of that sorts.

As for simplification, you can simply use ||:

const doArraysHaveAnyValue(): boolean => {
    return hasValArr1() || hasValArr2();
}

The reason this is better is that it is easier to read, and gives preference to using "positive" instead of negation with !

about 4 years ago · Santiago Trujillo Relatório

0

I think you can

сonst isEmpty():boolean => {

    if (!hasValArr1() && !hasValArr2()) 
       return false
    
       return true;
}

or:

const isEmpty():boolean => {
   return (!hasValArr1() && !hasValArr2())
}

or if you want to check whether the both arrays have values:

const HasArraysData():boolean => {
   return (hasValArr1() && hasValArr2())
}

and it becomes simpler to read code:

if (HasArraysData)

or:

if (!HasArraysData)
about 4 years ago · Santiago Trujillo Relatório

0

Without questioning the premise of the question, you can write :

const isEmpty():boolean => {
   return hasValArr1() || hasValArr2()
}
about 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