SwiftLint introduction tutorial

Keeping a codebase consistent and maintainable in a project with a team of devs sometimes may be very hard, different conventions and styles, plus different levels of experience with the language across devs may result in most of the times in an app very difficult to debug and mostly very hard to understand for new developer joining the team.

While investigating for a CI for one project that will help my team and I avoid bugs and keep our code clean I found Swiftlint, A tool to enforce Swift style and conventions, its great it follows Swift guidelines, its maintained by the Realm team and its open source which means its get updated constantly by the Swift community.

First, I will show you step by step how to integrate Swiftlint in your project so it will run every time you build it and then how to use Hound CI if you don’t want to download Swiftlint on your computer,

I will use one of my repositories to show you but you will have to use your own if you have any small app or small project lets use it, the steps are the same so that should work.

I am going to install Swiftlint using Brew if you don’t have it installed you can follow the steps on their website.

Once installed, open terminal and run

brew install swiftlint

once installed navigate to your project folder, you can follow the instructions for adding this to your project here, but let me show it to you, copy this…

if which swiftlint >/dev/null; then
swiftlint
elseecho "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi

We are going to add a new “Run Script Phase” to your Xcode project so open your project, choose the target and go to Build Phases if you don’t know how to add a script check this link.

Paste the snippet like so…

Build your app and you should have a lot of errors and warnings, mostly if your project has pods installed, so don’t worry, the cool thing is that we can configure what rules to check or not when using Swiftlint, I recommend to go to the Swiftlint rules file in their repo and see all of them.

To configure it’s very simple all you have to do is create a new document inside your project folder named …

.swiftlint.yml

In my case, I did it using the terminal like so…

touch .swiftlint.yml

Open the file…

open .swiftlint.yml

add what you want or not to include, there is an example of how to do it in the “configuration” section here.

Not sure how do your warnings look like but on my project I can see a lot of trailing_whitespace warnings, this one means that there should not be any space between lines, the warning looks like this “Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)”

I am not fun of leaving empty spaces in my code but I do like to leave one space between the signature and the body of a function, so I am going to disable that rule that its enable by default, how I know? well, the rules file in their repo has all the info you need, copy and paste this into the new file to disable that rule and also to avoid check for code in pods.

disabled_rules: # rule identifiers to exclude from running
- trailing_whitespace
excluded: # paths to ignore during linting. Takes precedence over `included`.
- Carthage
- Pods

Save and build, you will see that the number of warnings and errors are now so much lower.

A cool thing about Swiftlint is that it also can automatically correct certain violations by running on the terminal…

swiftlint autocorrect

Just a comment before using the autocorrection feature, Files on disk are overwritten with a corrected version.

Just to be aware Please make sure to have backups of these files before running, swiftlint autocorrect otherwise important data may be lost.

Ok now if you want you can run autocorrect, I did it and on my terminal, it says that is “Done correcting 36 files!” and now the amount of warning got down from 250 to 46, now its easier for me go one by one and decide what kind of rules I want to keep and refactor what I need; in the future, my code will follow those rules.

That’s pretty much it for the first part, now let me show you how to automate your code reviews using Hound CI. With no need to download Swiftlint locally

I am going to use another project for this and I invite you to use another of your projects as well.

So first create an account in https://houndci.com its free for public repositories.

Quick disclaimer, I am still deciding if Hound is the best tool for the job but because it makes working with Swiftlint so easy I am kind of liking it.

Now that you have an account you will be able to see all your repositories activate the one that you want to review.

Now that you have an “Active” project navigate to its folder locally and create a new branch, we are going to make a modification commit it and create a PR.

Push the PR to your remote repository, you will see this on your PR.

Hound CI now is reviewing your PR and see if it is following standard conventions, it looks like I need to fix something before merging it…

This is great now I can see what I did wrong without asking another teammate to review it, and this will let us all be on the same page, by the way, you can also configure the rules that you want to apply.

I found Swiftlint awesome for write clean and consistent code between devs, and Hound CI great for automated code reviews, what do you think? what other tools do you recommend?

I strongly recommend watching this great talk about Swiftlint here.

Big shout out to JagCesar for making it very easy to understand.