Skip to main content

Number of Imports (NOI)

v1.3.0
short name: NOI
effort: 10m
default threshold: 20

Number of imports is a quantitative metric that measures the total number of imports in a file.

High number of imports indicate a file that depends on too many other files and is usually hard to understand and maintain.

note

To get real-time analysis for this metric, enable the max-imports rule.

Why Be Cautious

High number of imports can lead to:

  • Maintainability Challenges: High number of imports can indicated files with too many responsibilities and too many integration point that require attention when any of the external dependency changes.
  • Increased Complexity: The more other files a file depends on, the harder it can be to understand and debug.

How to Address High Number of Imports?

First, understand the responsibility of the file. If that file is supposed to be the file that integrates different parts of your app together, having many imports is normal.

For other cases, consider splitting the file into several smaller files, each encapsulating a specific piece of functionality.

Config Example

analysis_options.yaml
dcm:
metrics:
number-of-imports:
threshold: 10

To set multiple threshold or other common config options, refer to the Configuring Metrics page.

Example

info

To view what contributes to the metric value, generate an HTML report.

❌ Bad: High Number of Imports

// NOI: 12
import 'dart:io';
import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:http/http.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:vector_graphics/vector_graphics.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:your_project/services/api_service.dart';
import 'package:your_project/utils/constants.dart';

// Main code follows here...

✅ Good: Low Number of Imports

main.yaml
// NOI: 8
import 'dart:io';
import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:your_project/network_service.dart'; // using http
import 'package:your_project/auth_service.dart'; // using firebase_auth, google_sign_in, shared_preferences
import 'package:your_project/utils/constants.dart';

// Main code follows here...
auth_service.dart
// NOI: 3
import 'package:shared_preferences/shared_preferences.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:firebase_auth/firebase_auth.dart';

// Implementation
network_service.dart
// NOI: 1
import 'package:http/http.dart';

// Implementation