Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

355
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!