Learn more about File Length Information & Warnings

In this post we’re looking at the file length information and how to configure warnings for it. The file length information is one of many ways Sw!ftalyzer helps us finding potential areas of improvement in our app.

First, we’ll see how Sw!ftalyzer shows file lengths as a histogram. Next, we’ll explore how to use the warnings feature to find files with too many lines.

Getting an Overview of File Lengths with Sw!ftalyzer


We’ll use a demo project analysis as an example. Download the analysis file and open it in Sw!ftalyzer to explore the analysis: Demo Project

The project consists of three files:

  • Repository.swift
  • Service.swift and
  • ViewController.swift


The first one contains the protocol RepositoryProtocol and the implementing class RepositoryImplementation. Next, the class ServiceImplementation has a dependency to RepositoryProtocol. It’s declared in the file Service.swift together with the protocol ServiceProtocol that it implements. Finally, the class ViewController is part of ViewController.swift and uses ServiceProtocol.

Below is how Sw!ftalyzer visualises this project. We can see the files, protocols and classes with their dependencies as described above. Next to it is a bar chart showing the file lengths of all files in the analysed project as a histogram. A histogram is a chart displaying the distribution of numerical data grouped in bins and represented as bars. In this case each bar represents the files in a bin and a bin is defined by a range of file lengths. The number above a bar shows us exactly how many files are in a bin.

In this example there’s one file with a length of 0 – 40 lines, one with a length of 41 – 81 lines and one with a length between 779 and 819 lines. Our goal is to identify the file that falls into the last bin and is significantly longer than the other files.

A graph showing the files and entities of the demo project. Arrows between entities show dependencies between them.
Dependency graph of the demo project.
A bar chart with three bars. Two of them are on the left, one of them is on the right. There's a huge spacing between them.
A histogram of file lengths. One of the files is a lot longer than the other two.

The length of a single file wouldn’t allow for such insides. Having an overview of the lengths of all files in such a histogram helps us to spot outliers. This gives us more context than looking at each file individually and it’s an information we can’t get with many other analysis tool like SwiftLint.

One way of finding the outlier is to go to the selected node tab, which is the first tab in the right panel. Here we can see information about the selected node. When selecting a file node in the graph, we can see the histogram from above but with one bar highlighted. This highlighted bar shows us the file length of the selected file node.

A screenshot of Swiftalyzer with the 4 tabs of the information panel. The first one is selected. A red border highlights the first tab.
The first tab shows information about selected nodes.
A similar graph as above showing the files and entities of the demo project. Arrows between entities show dependencies between them. In addition, the node representing the file ViewController.swift is highlighted with a red border.
Selecting a node in the graph updates the information shown in the right panel.
A similar bar chart as above with three bars. Two of them are on the left, one of them is on the right. There's a huge spacing between them. In  addition, the right one is highlighted in blue, because the file it represents is selected.
The bar representing the bin the selected file is in is highlighted.

Clicking through each file to find the one we are looking for is possible with only three files. However it’ll get quite tedious when dealing with hundreds of files. This is where the file length warning comes into play. It allows us to easily configure when to show a warning that highlights files with too many lines. Let’s take a look at how to use and configure this warning.

Configuring Warnings about File Lengths

Select the configuration tab. It’s the fourth tab in the right panel, which is bordered in the image.

This configuration tab allows us to configure warnings. Among others, one of them being the file length warning, which we can see in the next image.

A screenshot of Swiftalyzer with the 4 tabs of the information panel. The last one is selected. A red border highlights the fourth tab.
Configure warnings in the fourth tab of the right panel.

There’s a checkbox to enable or disable the warning. It’s selected by default meaning that a warning is shown next to each file node that exceeds a given file length. Unselecting this checkbox removes all warnings about file lengths in the dependency graph.

The file length to trigger a warning is set in the text field below the checkbox. Initially, the text field for the number of lines is empty so that no warning is shown regardless of the file length. To find the long file in the demo project, we enter 800 into it.

A checkbox and a text field allow to configure the file length warning.
The checkbox is enabled and the text field contains the number 800.
There are two options to configure the file length warning: Enabling/Disabling it via the checkbox and changing the length of when to show a warning via the number in the checkbox.

After changing the file length to trigger a warning to 800, we can see a red bubble appearing next to the file ViewController.swift. This highlights the node that’s now affected by the warning, so that it’s easy to find it.

A similar graph as above showing the files and entities of the demo project. Arrows between entities show dependencies between them. In addition, the node representing the file ViewController.swift has a red bubble in the top right corner with the number 1. This means that there is one warning for this file.
The graph shows a warning for files with more lines than configured.
A similar bar chart as above with three bars. Two of them are on the left, one of them is on the right. There's a huge spacing between them. In  addition, the right one is highlighted in red, because the file it represents has a warning.
The file length histogram shows warnings for bars.

We now know that it might be worth taking a look at the file ViewController.swift. It looks like it’s doing a lot of things and we can improve the app by separating its code into multiple code entities in separate files.