Skip to content

kevin-sakemaer/avoid_long_functions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

avoid_long_functions

A Dart analyzer plugin that reports functions, methods, and constructors exceeding a configurable line count. Long functions are harder to read, test, and maintain — this rule helps keep them short.

What it checks

The rule counts non-blank, non-comment lines inside function bodies (between { and }). It applies to:

  • Top-level functions
  • Instance and static methods
  • Constructors
  • Getters and setters
  • Anonymous functions (lambdas/closures)

Expression-bodied functions (=> expr) are always skipped since they are inherently concise.

Line counting details

Only lines between the opening { and closing } are counted. The following are excluded:

  • Blank lines
  • Lines containing only comments (//, /* ... */, * ...)

This means well-commented code is not penalized.

Setup

Add the plugin to your analysis_options.yaml:

plugins:
  avoid_long_functions:
    path: <path_to_this_package>
    diagnostics:
      avoid_long_functions: warning

The severity can be set to warning, error, or info.

Configuration

The maximum line count defaults to 20. Override it with the max_lines option:

plugins:
  avoid_long_functions:
    path: <path_to_this_package>
    max_lines: 30
    diagnostics:
      avoid_long_functions: warning

Example

Given max_lines: 20, this function would trigger the rule:

void processData(List<int> data) {
  // ... 21+ non-blank, non-comment lines ...
}

The diagnostic message looks like:

The function 'processData' is 25 lines long (maximum is 20).
Try refactoring into smaller functions.

Suppressing the rule

Use standard Dart ignore comments:

// ignore: avoid_long_functions
void legacyFunction() {
  // ...
}

Or for an entire file:

// ignore_for_file: avoid_long_functions

Requirements

  • Dart SDK ^3.11.0

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages