Skip to main content

SKY-L028 · Logic and resilience

Too many returns

Varies

The catalog gives no severity. Six to eight return statements are LOW; nine or more are MEDIUM.

A function with more than five return statements. Each return is another exit the reader has to keep in mind, and the function's result depends on which branch ran.

Languages
Python
Quality gate
counted toward the gate limits (full and diff scans). Hidden when it is the only metric reported for the function. It is listed when two complexity measures agree on the same function (SKY-Q301 with SKY-Q306, or SKY-Q306 with SKY-Q302); the metrics then share one entry, led by the most severe, which the gate counts once. Otherwise it is counted only with --all-advice or when the rule is named in [tool.skylos.gate] rules.
Finding type
advice
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

Long chains of early returns often encode a table of values as control flow. Adding a case means finding the right place in the chain, and a case placed one line too low silently never runs.

What Skylos flags

shipping/rates.py · PythonReported on line 4
"""Shipping rates by country."""  def shipping_rate(country, weight_kg, express):    if express:        return 25    if weight_kg > 20:        return 40    if country == "US":        return 5    if country == "CA":        return 8    if country in ("GB", "IE"):        return 9    return 15

What Skylos leaves alone

shipping/rates.py · Python
"""Shipping rates by country."""  def shipping_rate(country, weight_kg, express):    if express:        return 25    if weight_kg > 20:        return 40    if country in ("US", "CA"):        return 5    if country in ("GB", "IE"):        return 9    return 15

Why Skylos leaves it alone

Five return statements is the limit, not over it. The rule counts exits only; it does not judge whether a sixth would make the function harder to read.

How Skylos decides

  1. Runs with --quality on Python functions and methods. Test files, examples, benchmarks and build scripts are skipped.
  2. Counts the return statements in the function's own body, including those inside if, for, while, try, with and match blocks, but not those of nested functions or classes.
  3. Reports when the count is above 5: LOW for 6 to 8, MEDIUM for 9 or more. The finding is on the def line.

How to fix it

Keep the special cases as early returns and move the per-country values into a table. Adding a country is now one line of data.

shipping/rates.py · Python · the fix, against the flagged file
"""Shipping rates by country.""" Added: COUNTRY_RATES = {"US": 5, "CA": 8, "GB": 9, "IE": 9}Added: DEFAULT_RATE = 15 Added:  def shipping_rate(country, weight_kg, express):    if express:        return 25    if weight_kg > 20:        return 40Removed:     if country == "US":Removed:         return 5Removed:     if country == "CA":Removed:         return 8Removed:     if country in ("GB", "IE"):Removed:         return 9Removed:     return 15Added:     return COUNTRY_RATES.get(country, DEFAULT_RATE)

When it can be wrong

  • A flat dispatcher or validation function where every branch is a guard clause is reported, even when it reads clearly.
  • The limit of 5 is fixed; it cannot be changed in configuration.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • Put the marker on the def line.

    Python
    def shipping_rate(country, weight_kg, express):  # skylos: ignore[SKY-L028]
  • [tool.skylos] ignore = ["SKY-L028"] turns the rule off for the project.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.