A tool to keep yourself honest as an iOS developer
All iOS developers love writing code. After all, there is a good reason they are doing it. A smaller set of iOS developers take great pride in creating code that can be reused easily, which can be maintained easily. In short write great code.
A few years back I had joined a team in which I kickstarted a code review process to help raise the code quality in the. Some of the comments were around coding style and a few about code complexity.
It was a great process, I learned quite a lot by doing this as my co-worker gave me feedback on my code. Also, just reading other people’s code, I’d sometimes be: “I had no idea you could do this”
Now that I’m working independently, code reviews are less frequent and I was looking for a way to keep me honest. That’s when I started looking at linting tools. Specifically, I settled on SwiftLint.
First, because I’m not always consistent about my coding style, yet I like well formatted code. I was looking for a tool that could automatically format my code and make it look good. Which SwiftLint does (though not as much as I’d like).
Second, because I want to have a tool to keep me honest around code quality. We’ve all been there. Late in the day, I just wanted to get this last feature in. To make it quick, I’ll just add it to this class. Sure it does not belong there, this class is already 500 lines, this methods is already 75 lines, it won’t easily be maintained and no one will know. Except with SwiftLint, as soon as you build, it tells you.
To get Swiftlint going in your project, I recommend using cocoapod to install it.
Then add create a shell script (ie: runSwiftLint.sh) in a file in your project with the following code
echo "========== SwiftLint: start =========="
if which swiftlint >/dev/null; then
echo "warning: SwiftLint does not exist, download it from https://github.com/realm/SwiftLint"
echo "========== SwiftLint: done =========="
This script will run both the auto formatting feature of swiftlint and the linting feature. If SwiftLint is not installed, you will get a warning.
Don’t forget to make this file executable:
chown +X runSwiftLint.sh
Last step is to create a build step in Xcode to run your script:
I like to keep my script file outside of the xcode project file to make it easier to track changes to those script over time.
There are plenty of things SwiftLint won’t tell you, but after this last project, I’m adding it to all my projects. It forces me to consider what is the goal of this class/method/struct. How can I refactor it so it stay readable, maintainable.
How about you, any other great tools or process you discovered to keep your code quality up? Let me know in the comments or send me a message Olivier Destrebecq.