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

351
Visualizações
Where is the Sonar "duplicated code" here?

I just run sonar scanner on the sample Sonar project. It gives me the message that there is "duplicated code on lines 7-20". Can anyone explain this?

enter image description here

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

0

SonarQube is telling you that this portion of the code contains duplicated logic. This doesn't necessarily mean that the code itself is copy-pasted, but that, conceptually, the exact same thing is happening at multiple places. In this case, the logic of returning a String value with regard to the int value is clearly repeated.

A simple solution here:

String[] array = { "One", "Two", "Three", "Four", "Five", "Six" };
if (i >= 1 && i <= array.length) {
    return array[i - 1];
}
over 4 years ago · Santiago Trujillo Relatório

0

SonarRules for Java projects:

A piece of code is considered as duplicated as soon as there is the same sequence of 10 successive statements whatever the number of tokens and lines. This threshold cannot be overridden.

You need to modify that multiple if return sections

over 4 years ago · Santiago Trujillo Relatório

0

public class Test
{
   private static final String[] WORDS;

   static {
      WORDS = new String[] {
            "One", "Two", "Three", "Four", "Five", "Six"
      };
   }

   public String intToEnglishValue(final int number) {
      return number > 0 && number <= WORDS.length ? WORDS[number - 1] : "";
   }
}
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