TinyExperience: When to use inline function

Galvin Li
5 min readJan 27, 2019

--

此文章同时提供中文版本:TinyExperience:Inline function的使用场景

I believe everyone who used Swift all know about the function in Swift. But inline function maybe not that common for most of developer. This article is to introduce my way to use inline function in daily development.

What is inline function

We usually use function at the level of class, struct, enum, etc.:

class Example {
func method1() {}
}

And inline function is a function inside another function, like this:

class Example {
func method1() {
func inlineMethod1() {}
}
}

In fact, from another angle, the inline function is just a normal usage of function. Objective-C not supported nested function and Swift fix that and even support multiple levels of nesting. But one thing we should notice is inline function belongs to the local declaration, so it need to be defined before use it.

Common usage

Since we don’t need to use it in Objective-C, why we will need it in Swift? For example, we often write initialization code of view like this:

The loadView contains a lot of different initialization code. It may be clear when you write it, but it will be very confusing when you look at it later. Of course, writing comments are also a good way to make it clear, but the way we write comments are all not same. And comments are no direct correlation with the content, so there are often cases where the comments can’t keep up with the code. The better way is make the code describe itself, like extracted into a function, such as this:

This should be our most common way to do. The extracted function makes the code easier to understand and handle. But there is a problem: those exposed initxxx() methods can be called directly from anywhere inside the class. Although we know that they are just the initialization code should only called once from the loadView, but no restrictions in the code and you can’t control other developers in your team. At this point we can use the inline function to solve this problem:

Replacing the three initxxx() methods with the inline function preserves the structure of the code while avoiding possible error calls and helping with post-refactoring because the code is more compact and not scattered across local.

Reduce duplicate code

Reducing duplicate code has always been the responsibility of the function. But in the past with Objective-C, function only available for the class level, we usually only extract the general repetitive code into function. Some partially reused code we just directly repeating them because if all local logic extracts to function would make our class level functions way too messy.
Just like the example below, label.text with multiple duplicate set up. The example might be fairly simple, but we still need to maintain consistency between the two sets of strings.

But with the inline function, we can implement the DRY principle more thoroughly, so that the complex parts are fully reused, just like this:

In fact, we can also make our code more structured by taking advantage of other features of the function, such as parameter passing:

Callback optimization

In addition to optimizing some of the original function of the class level, inline function can also be used to optimize some callback processing problems. For example, we often need to make network requests, there will be multiple callbacks, such as the following example:

However, when the number of parameters is requested like this, the whole method call will be very long and unclear. We can use line breaks to deal with it, but this time you have to endure the weird indentation logic of Xcode:

The parameters are clear enough, but the callback content is indented too much and the indentation position of } is not acceptable to me. At this time we can use the inline function to deal with this problem, after adding two inline functions, we can make the code much clearer:

In fact, you can extract the onSuccess and onFailure methods to the class layer like we used to do, but the advantage of using the inline function is that these two methods do not available for other methods, which is equivalent to limiting the scope of these methods in a good way. And the structure of the entire code will be neat and tidy, with low indentation level.

These simple examples may not reflect the need to use the inline function. But imagine if there are multiple requests at the same time and you need to do some common processing for all callback. If you put the function to the class level, it will be a method use only for specific method, but available for all other methods in a bad way. But if you use the inline function, you can still maintain the structure and low interference, such as the following example:

In fact, the inline function is to make our function scattered in a more suitable place instead of all stored on the first level of class and struct, such as the private func under our class to distinguish at a finer level.

  • If you have questions or suggestions, welcome to leave comment for discuss.
  • If you feel this article is valuable, please forward it so more people can see it.
  • If you like this type of content, welcome to follow my Medium and Twitter, I will keep posting useful content for everyone.

--

--

Galvin Li

A Tiny iOS developer who love to solve problems and make things better.