SKY-C304 · Quality, structure and performance
Function too long
SKY-C304 reports a function or method with more than 50 lines of code. In Python and TS/JS, blank lines and comment-only lines do not count, nor do Python docstrings. The examples below lower the Python limit to 15 with max_lines = 15 in pyproject.toml, so they stay short.
- Languages
- Python, TypeScript, JavaScript, Java, Go
- Quality gate
- counted toward the gate limits (full and diff scans). Hidden when it is the only metric reported for the function. In Python and JS/TS it is listed when SKY-Q306 and SKY-Q301 or SKY-Q302 also report the function, in Go and Java when another function metric does; 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
A long function usually does several jobs at once: it fetches, filters, computes and formats. Each job is harder to test on its own, and a change to one of them means reading, and risking, all the others.
What Skylos flags
"""Monthly sales report for the finance team.""" from collections import defaultdict def monthly_report(orders, month): totals = defaultdict(float) refunds = defaultdict(float) customers = set() for order in orders: if order.created.month != month: continue customers.add(order.customer_id) if order.status == "refunded": refunds[order.region] += order.total continue totals[order.region] += order.total lines = [f"Sales report for month {month}", ""] for region in sorted(totals): net = totals[region] - refunds.get(region, 0.0) lines.append(f"{region:<12} {totals[region]:>10.2f} {net:>10.2f}") lines.append("") lines.append(f"Customers: {len(customers)}") lines.append(f"Refunded: {sum(refunds.values()):.2f}") return "\n".join(lines)[tool.skylos]max_lines = 15What Skylos leaves alone
"""Monthly sales report for the finance team.""" from collections import defaultdict def monthly_totals(orders, month): """Sum sales and refunds per region for one month. Refunded orders are kept apart so the report can show gross and net figures side by side. Orders from other months are skipped. Returns the totals and refunds keyed by region, and the customers. """ totals = defaultdict(float) refunds = defaultdict(float) customers = set() for order in orders: if order.created.month != month: continue customers.add(order.customer_id) if order.status == "refunded": refunds[order.region] += order.total continue totals[order.region] += order.total return totals, refunds, customersWhy Skylos leaves it alone
The function spans 23 lines, but its docstring and blank lines are not code. It has 13 code lines, under the limit of 15 set in pyproject.toml.
How Skylos decides
- Runs with
--quality. Each function and method is measured from its first line to its last, so a nested function's lines also count toward the function that contains it. - Python counts code lines only: blank lines, comment-only lines and the docstrings of the function and of functions and classes inside it are left out. It reports a count above
max_linesunder[tool.skylos](default 50): MEDIUM below 100 lines, HIGH from 100. - Python skips test code (test folders,
test_*.pyand*_test.pymodules that define tests, files pytest is configured to collect),examplesandbenchmarksfolders, a top-leveldocsfolder and the rootsetup.py. - TypeScript and JavaScript leave out blank and comment-only lines and skip test callbacks such as
describe(...)andit(...)bodies in test files. Java and Go count every line, blanks and comments included. All three report LOW above 50; that limit is fixed and does not readmax_lines. - Reports on the
def(or function) line.
How to fix it
Split the function along its jobs: one function gathers the numbers, another formats them. Each part is then short enough to read in one go and can be tested on its own.
"""Monthly sales report for the finance team.""" from collections import defaultdict Removed: def monthly_report(orders, month):Added: def monthly_totals(orders, month): totals = defaultdict(float) refunds = defaultdict(float) customers = set() for order in orders: if order.created.month != month: continue customers.add(order.customer_id) if order.status == "refunded": refunds[order.region] += order.total continue totals[order.region] += order.totalAdded: return totals, refunds, customersAdded: Added: Added: def monthly_report(orders, month):Added: totals, refunds, customers = monthly_totals(orders, month) lines = [f"Sales report for month {month}", ""] for region in sorted(totals): net = totals[region] - refunds.get(region, 0.0) lines.append(f"{region:<12} {totals[region]:>10.2f} {net:>10.2f}") lines.append("") lines.append(f"Customers: {len(customers)}") lines.append(f"Refunded: {sum(refunds.values()):.2f}") return "\n".join(lines)When it can be wrong
- A long but flat function, such as one that builds a large mapping or registers many routes in a row, is reported although it is easy to read.
- In Java and Go, comments and blank lines count, so a well-commented method reaches the limit sooner than the same code in Python or TS/JS.
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
On the
defline (the reported line):Python def monthly_report(orders, month): # skylos: ignore[SKY-C304] one report layoutignore = ["SKY-C304"]under[tool.skylos]turns the rule off for the project; a highermax_linesraises the Python limit instead. A baseline accepts existing findings, but it records the absolute file path and the line, so an entry stops matching when the checkout moves or the line shifts.