Improve Build Efficiency

i.vikash
5 min readApr 24, 2022

Using good coding practices | Swift

During development, build time is very important factor for developers. So reducing time by even a few seconds can have a significant impact over the development time.

Even most of the optimizations are automatically done by Xcode compiler, but using good coding practices we can also reduce unnecessary work for the compiler. So in this post we will discuss few good coding practices to improve build efficiency.

Let’s start :-

Below are some good practices through which we can reduce the compiler work -

  1. Provide Explicit Type Information

2. Define Delegate Methods in Explicit Protocols

3. Simplify Complex Swift Expressions

4. Include Framework Names in Import Statements

5. Minimize Number of Symbols Shared, Between Swift and Objective-C

1. Provide Explicit Type Information

When we declare a variable, sometime we miss to define the type. The swift compiler is capable of inferring the type of a variable from the value we assigned to it. But the compiler have to perform extra work to compute type information.

Let’s understand with example :

In above case compiler quickly determine the type of var unknownType.

But in case of complex value compiler must perform extra work to compute type information.

No explicit type information.

In above case, to determine the type of var unknownType, Swift compiler must evaluate the results, which takes a some time.

With explicit type information.

In above case, by providing explicit type information we can reduce the work of compiler.

So good coding practice is to always define the type.

2. Define Delegate Methods in Explicit Protocols

Delegates are extremely common in iOS development and widely used for communicate between objects. So, same like explicit type variable it is always good to provide explicit type information to our delegate objects also.

Without type information

In the above code, delegate is declared as an optional object of any type. In this case compiler must assume that any object in your project or referenced frameworks contains the function, so it searches your entire project to make sure that function exists somewhere and it takes time.

With type information

In the above code, an explicit protocol helps the compiler find the method more quickly.

So good coding practice is supply specific type information.

3. Simplify Complex Swift Expressions

The Swift language allows us to write code in very expressive ways. But some time we write some complex statements which affect compile time a lot.

Complex Expressions

In the above code, to get sum of non optional values we have written one line expression which will work fine but, this one-line closure makes the code hard to read and harder for the compiler to evaluate.

Simple And Readable Expressions

The above code offers the same behavior as the single-line closure version, but is easier to read and compiles quickly.

So good coding practice is to create something simpler and more readable, rather than complex expression.

4. Include Framework Names in Import Statements

When we import headers into our source files, we always include the name of the parent framework or library in our import statement.

In C-based code, importing headers usually copies the contents of the header file into your source.

When we include the framework name, the compiler has the option to use module maps to import the headers, which significantly reduces importation time.

With module maps, the compiler loads and processes the framework’s header files once, and caches the resulting symbol information on disk. When you import the same framework from another source file, the compiler reuses the cached data, eliminating the need to again read and preprocess the header files.

Above example shows import statements for both a system and custom framework, both of which have module maps.

So good coding practice is to include the framework name in import

5. Minimize Number of Symbols Shared, Between Swift and Objective-C

This is an important one when we mix Swift and Objective-C code in our project, Swift code might need to know about portions of our Objective-C code, and vice versa.

As we know, for this compiler use two special header files :-

  1. The Objective-C bridging header : To determines which Objective-C symbols is available to Swift code.
  2. The compiler-generated Swift header : It is a list of all public Swift symbols, which can be use in your Objective-C code.

Reducing the size of both header files reduces the compiler’s workload and improves compilation times. A smaller file size means the compiler can process the headers more quickly. It also means faster symbol lookup times when compiling source files.

So good coding practice is to include only the headers and symbols we actually reference from our Swift source, when configuring the contents of our Objective-C bridging header

Conclusion :

In this post we were trying to cover that how we can reduce compile time by using some good coding practice. Though most of the developers are already using these thing in daily coding. These small small things add a lot of value during development.

Thanks for reading…

Happy coding …

--

--

i.vikash

Tech enthusiast exploring software architecture, development, and innovation. Join me on my journey of tech discovery! 🚀 #TechExploration