Comment % quality gate condition not available for new code?

  • Community Build
  • v25.5.0.107428
  • MQR Mode

We would like to include a comment density quality gate condition on new code. The condition exists for overall code but not for new code. Is there anyway to do this?

Thanks!
Todd

Hey Todd,

Currently, SonarQube doesn’t calculate Comment Density for new code, which is why it’s not available as a quality gate condition.

I think the main challenge is determining which comments belong to “new code.” Consider this example where “fees” are added to a method to calculate the cost of an order.

// Calculate the total price for an order
// This method handles tax, discounts, and additional fees // updated comment
// Returns the final amount to charge the customer
public double calculatePrice(double basePrice, double taxRate, double discount) {
    // Apply any discount first
    double discountedPrice = basePrice - discount;
    
    // Calculate tax on the discounted amount
    double tax = discountedPrice * taxRate;
    
    double shipping = calculateShipping(discountedPrice); 
    double handling = 2.50; // new line
    double insurance = discountedPrice > 100 ? 5.00 : 0; // new line
    double processingFee = 1.99; // new line
    double fees = shipping + handling + insurance + processingFee; // new line
    
    // Add everything together for final price
    return discountedPrice + tax + fees;  // updated line
}

The developer added 5 lines of code but only updated 1 comment. The method now has 7 comment lines total, but only 1 was actually modified.

How should SonarQube calculate comment density for this “new code”?

  • Only the 1 updated comment vs 5 new lines? (20% density).
  • Include all 7 existing comments because they’re in the same method? (140% density!)
  • ?

Curious to hear your thoughts.