Search code examples
c#sonarqubesonarqube-scan

Refactor the code not to use Absolute Paths or URIs


My C# WIndows Forms Application has some menu links which point out to web URIs. But now my SonarQube Scan is giving me this error/warning(S1075) to refactor my code. So what would be the safe/best way to use web URI inside C# backend code.

private void HelpDocMenu_Click(object sender, EventArgs e)
{           
   System.Diagnostics.Process.Start("https://docs.google.com/document/d/142SFA/edit?usp=sharing");
}

SonarQube Error

S1075 Refactor your code not to use hardcoded absolute paths or URIs

Solution

  • You have three options:

    1. To ignore it:
    private void HelpDocMenu_Click(object sender, EventArgs e)
    {       
       #pragma warning disable S1075 // URIs should not be hardcoded    
       System.Diagnostics.Process.Start("https://docs.google.com/document/d/142SFA/edit?usp=sharing");
       #pragma warning restore S1075 // URIs should not be hardcoded
    }
    
    1. To have a temporary variable like this:
    private void HelpDocMenu_Click(object sender, EventArgs e)
    {           
        var url = "https://docs.google.com/document/d/142SFA/edit?usp=sharing";
        System.Diagnostics.Process.Start(url);
    }
    
    1. To put it in your configuration file, some settings.json.

    Third option is the recommended one.