SonarQube Common Code Smells

A code smell is not a bug. The program still runs, yet the code hints at deeper trouble, much like a strange odor in a kitchen hints at spoiled food. Smells slow teams down, confuse new readers, and invite future bugs. SonarQube detects hundreds of them across many languages.

The Smell Map

 Code Smell Families
 ┌────────────────┬────────────────┬────────────────┐
 │  Clutter       │  Confusion     │  Risk          │
 ├────────────────┼────────────────┼────────────────┤
 │ Unused code    │ Magic numbers  │ Empty catch    │
 │ Dead comments  │ Long methods   │ Swallowed      │
 │ Duplicate text │ Vague names    │   errors       │
 └────────────────┴────────────────┴────────────────┘

Clutter Smells

Unused Variables and Parameters

A variable that nobody reads wastes attention. Readers wonder whether the code forgot something important. Delete it.

int total = 0;
int unused = calculateBonus(); // never read

Commented-Out Code

Old code wrapped in comments adds noise. Version control already remembers the past, so remove the lines and rely on history.

Duplicated String Literals

The same text typed in five places invites typos. Store it once in a constant and reuse the name.

String role = "ADMIN";  // appears 6 times across the file
// Better
private static final String ADMIN_ROLE = "ADMIN";

Confusion Smells

Magic Numbers

A bare number such as 86400 leaves readers guessing. A named constant explains itself.

if (age > 18) { ... }                // what does 18 mean?
if (age > LEGAL_ADULT_AGE) { ... }   // clear intent

Long Methods

A method with hundreds of lines does many jobs at once. Split it into small methods that each do one task and carry a clear name.

Too Many Parameters

A function that accepts eight inputs causes mixed-up arguments. Group related inputs into one object.

Vague Names

Names such as data, temp, or x tell nothing. Pick names that describe purpose, like invoiceTotal.

Risk Smells

Empty Catch Blocks

An empty catch swallows an error silently. The program fails later with no clue about the origin. Log the problem or handle it on purpose.

try {
  save(order);
} catch (Exception e) {
}   // smell: error vanishes

Overly Broad Exceptions

Catching the most general exception type hides unexpected problems. Catch the specific type you can actually handle.

Methods That Return Null Collections

Returning null instead of an empty list forces every caller to check. An empty list is safer and clearer.

Reading a Smell in SonarQube

  1. Open the project and select Issues.
  2. Filter the Type to code smell, or use the Maintainability quality filter in newer versions.
  3. Click an issue to see the highlighted code.
  4. Select Why is this an issue? to read the rule explanation with a fix example.

Priority Guide

SmellTypical EffortPriority
Unused variableMinutesLow, quick win
Empty catch blockMinutesHigh
Magic numberMinutesMedium
Long methodHoursMedium to high
Duplicated blockHoursMedium to high

Habits That Prevent Smells

  • Run the scanner in the editor with SonarQube for IDE for instant hints.
  • Review small pull requests instead of huge ones.
  • Name things for readers, not for the compiler.
  • Delete code the moment it loses its purpose.

A Note on False Positives

Occasionally a rule fires on code that is correct in context. Mark the issue as Accepted or False Positive and add a comment that explains the reason. Reviewers learn from those comments.

Learning From Rule Descriptions

Every smell comes with a rule page that explains the problem in plain language. The page shows a noncompliant example, a compliant example, and links for deeper reading. Developers who read these pages grow faster than those who only click the fix button. Make a habit of reading one rule description per day during the first month, and the common smells will become easy to spot even before the scan runs.

Team Agreement

Agree on a short list of smells that block a merge, such as empty catch blocks. Treat lower-priority smells as cleanup tasks. A shared agreement removes arguments and keeps reviews friendly.

Quick Recap

Smells fall into clutter, confusion, and risk. Remove clutter quickly, clarify confusing spots, and fix risky patterns first.