maintain coding standards using SwiftLint

Maintain Your Coding Standards with SwiftLint


We all have struggled maintaining our Coding Standard and discussed it more often. SwiftLint helps us maintain the Coding Standard. This article would go around how you can maintain your coding standards with SwiftLint. Learn how SwiftLint can be integrated in your project and how to get the best out of it.

Goal

Understand SwiftLint and how to use it to maintain your Coding Standards.

Why do we need to follow the Coding Standards?

It is important to have a Coding Standard and actually follow it as it makes the code consistent and therefore easy to maintain. Because anyone can understand it and modify it easily at any time. This gives us the ability to work on any module and switch between them easily, making the Team dynamic and Agile.

What is SwiftLint?

SwiftLint is a tool to enforce Swift styles and conventions, keeping the codebase consistent and maintainable.

How to use SwiftLint?

SwiftLint can be installed globally with brew or you can integrate it for a single project as a pod. We will be adding it as part of the project using the pod.

Add the following to the Podfile:

pod 'SwiftLint'

Run pod install command in the terminal and it will install SwiftLint.

Now to add this in the Xcode, open Build Phases and add a Run Script with following:

"${PODS_ROOT}/SwiftLint/swiftlint"

NOTE: Keep this as the top most phase to be executed so that you get all the errors before the project files start compiling. This reduces the time majorly.

Hit Run and the SwiftLint will be executed automatically. 

An example of how it looks like when the standards does not match:

Maintain your coding standards with SwiftLint
Source: Official SwiftLint GitHub Repo

Advanced Configurations

Following all the Coding standards which the Swift Community is following is sometimes very difficult and preferencial. Hence SwiftLint has given the choice of which rules to follow and which ones to ignore.

You need to create a configuration file in the project root directory with name .swiftlint.yml. In this file, you can add the desired configurations like this:

excluded: # paths to ignore during linting
  - Pods
disabled_rules: # rule identifiers to exclude from running
  - trailing_whitespace

As you can see, the excluded allows us to exclude files and folders which we do not want to modify such as Pods.

The rules which we do not wish to follow goes in the disabled_rules section.

The saviour : Autocorrect

Yes, we can autocorrect the code which does not follow the rules of SwiftLint!

SwiftLint has offered a command autocorrect which modifies the code with necessary changes.

Go to root of the project and Run the following command in the Terminal:

./Pods/SwiftLint/swiftlint autocorrect

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

This command has limitations, it does not correct all the errors! It only corrects the rules listed as correctable. You can check the list of rules with the following command:

./Pods/SwiftLint/swiftlint rules

Conclusion

The SwiftLint is easy to use during the development itself and helps us follow the coding standards. We need to create a process which forces everyone to check the code before merging to the main development branch.

If you want to learn how to use SwiftUI in your existing app, then you can read it here!

or If you want to learn how to use and convert your UIViewControllers in SwiftUI, then you can read it here!

References

https://github.com/realm/SwiftLint

https://medium.com/developerinsider/how-to-use-swiftlint-with-xcode-to-enforce-swift-style-and-conventions-368e49e910

https://www.youtube.com/watch?v=YpRs3Jb-UjY


Like it? Share with your friends!

0 Comments

Your email address will not be published. Required fields are marked *