Understanding your Project with metrics: Framework Dependencies

When creating an app we often use frameworks. This enables us to be faster, because we don’t need to re-invent the wheel and instead can rely on tested and documented code from other developers.

However, depending on frameworks can also have disadvantages. The more of our code uses a framework, the higher is the dependency to that framework. A high dependency is not always bad, but it can increase the maintenance costs and vulnerability in case of breaking changes in a framework.

Thus it’s useful to know and monitor how much we depend on frameworks. Unfortunately, it’s not easy to get this information. We could count all imports for a given framework, but this is a tedious manual process. This may also be error-prone, our entities may not depend on an entity of an imported framework at all.

Sw!ftalyzer calculates this information automatically. This helps us to get a better understanding of our project.

Let’s take a look at this example project:

A graph showing the files and entities of the demo project. Arrows between entities show dependencies between them.
Dependency graph of the demo project.

This is a small app containing 4 code entities. The first one is DemoProjectApp, it’s the main entry point of the app and shows ContentView. This view struct has a preview called ContentView_Previews. All three of these entities depending on the framework SwiftUI. The fourth entity is ViewController, which uses a framework entity from UIKit.

This graph helps us get an overview of which frameworks are used and how many of our code entities depend on them. With projects often larger than this small example, it’d be too much work understanding all dependencies to frameworks.

Another way of finding this information is in the information panel to the right of the graph. This tab shows general project information like a list of unused entities, an overview of files that are part of multiple targets and much more.

A screenshot of Swiftalyzer with the 4 tabs of the information panel. The third one is selected. A red border highlights the third tab.
The third tab of the right panel shows general information about the project.

One information we find here is the framework dependency metric, wich lists all frameworks used in the analysed project. It shows how many of our own entities use framework entities provided by the frameworks.

In this example we see that 3 of our own code entities depend on entities that are part of the framework SwiftUI. This equals 75% of all our code entities, which is a pretty high dependency to SwiftUI. One of our entities, or 25% of all our entities, depend on the framework UIKit.

A screenshot from Swiftalyzer showing the Framework Dependency metric for the demo project from above. It lists two frameworks: SwiftUI and UIKit. 3 entities or 75% of all entities use framework entities provided by SwiftUI. 1 entity or 25% of all entities use framework entities provided by UIKit.
The third tab shows general information about the project.

Of course not all dependencies are equally. Having a high dependency on 1st party frameworks from Apple is probably very common. At the same time, a high dependency value is not necessarily something to change. However, this information gives us a better understanding of our code and helps us take informed decisions.

Configuring Frameworks

Sw!ftalyzer shows information about frameworks in the third tab of the left panel. Here we can search for, add, remove or rename frameworks and framework entities.

For the example project from above we see two frameworks here, SwiftUI and UIKit, which are the same as in the graph.

When Sw!ftalyzer analyses the project, it automatically checks for frameworks. This means that frameworks from Apple that the project uses are shown here. The list also includes 3rd party frameworks added via CocoaPods or as Swift Packages.

We can create own frameworks to structure the analysis. Adding a new framework and moving entities to it removes the respective entity nodes from their parent nodes in the dependency graph. Instead, Sw!ftalyzer adds them to a new framework node. All incoming and outgoing dependencies are updated.

A screenshot of Swiftalyzer with the 4 tabs of the navigation panel. The third one is selected. A list of frameworks and their entities can be seen.
Configure Frameworks in the third tab of the left panel.