SKY-L028 · Logic and resilience
Too many returns
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-adviceor when the rule is named in[tool.skylos.gate] rules. - Finding type
- advice
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 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 15What Skylos leaves alone
"""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 15Why 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
- Runs with
--qualityon Python functions and methods. Test files, examples, benchmarks and build scripts are skipped. - Counts the
returnstatements in the function's own body, including those insideif,for,while,try,withandmatchblocks, but not those of nested functions or classes. - Reports when the count is above 5: LOW for 6 to 8, MEDIUM for 9 or more. The finding is on the
defline.
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 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
defline.Python def shipping_rate(country, weight_kg, express): # skylos: ignore[SKY-L028][tool.skylos] ignore = ["SKY-L028"]turns the rule off for the project.