Skip to main content

SKY-C304 · Quality, structure and performance

Function too long

Varies

The catalog gives no severity. Python reports MEDIUM below 100 code lines and HIGH from 100. TypeScript, JavaScript, Java and Go always report LOW.

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-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

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

app/reports.py · PythonReported on line 6
"""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)
pyproject.toml · TOML
[tool.skylos]max_lines = 15

What Skylos leaves alone

app/reports.py · Python
"""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, customers

pyproject.toml: same as in the flagged example.

Why 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

  1. 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.
  2. 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_lines under [tool.skylos] (default 50): MEDIUM below 100 lines, HIGH from 100.
  3. Python skips test code (test folders, test_*.py and *_test.py modules that define tests, files pytest is configured to collect), examples and benchmarks folders, a top-level docs folder and the root setup.py.
  4. TypeScript and JavaScript leave out blank and comment-only lines and skip test callbacks such as describe(...) and it(...) 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 read max_lines.
  5. 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.

app/reports.py · Python · the fix, against the flagged file
"""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 def line (the reported line):

    Python
    def monthly_report(orders, month):  # skylos: ignore[SKY-C304] one report layout
  • ignore = ["SKY-C304"] under [tool.skylos] turns the rule off for the project; a higher max_lines raises 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.

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