Search code examples
javasonarqubereadline

Skipping first line in csv using linereader.readline() but facing code quality issue


Need help..!!

I was trying to skip first line of CSV file using "linereader.readline()" before actual while loop starts to fetch next line values.

But as per code quality like SONAR , saying "read" and "readLine" return values should be used.

Most of the posts anwers were opting the same solution to skip first line.

Any opinion to skip first line but at the same time not to fall under SONAR issue : https://rules.sonarsource.com/java/RSPEC-2677

Thanks.


Solution

  • Rather than doing an inline .readLine() to skip over an item, sonarqube is saying that you should still assign it to a variable.
    "When a method is called that returns data read from some data source, that data should be stored rather than thrown away. Any other course of action is surely a bug." - Sonarqube docs

    This is being done as arbitrarily throwing away data can be very dangerous on large scales where no one may be able to easily see the input/output of a specific file.

    Basically rather than:
    bufferedReader.readline()
    String line
    //do whatever file read you're going to do thing

    You should have:
    String line = bufferedReader.readline()
    //Do whatever logic you're going to do with the file

    This should satisfy Sonarqube.