avatarChristopher Saez 📱

Summary

The website content outlines four essential tools for enhancing code quality and productivity in iOS projects through Git hooks: SwiftFormat, SwiftLint, Periphery, and Terminal-notifier.

Abstract

The article discusses the implementation of Git hooks in iOS development projects to improve code quality and developer workflow efficiency. It recommends four specific tools that serve different purposes within the Git hook framework. SwiftFormat is suggested as a pre-commit tool for code formatting, emphasizing its flexibility and the importance of not using it on the CI side. SwiftLint is presented as a mandatory linter to prevent code smells, with advice on running it in strict mode pre-commit and avoiding certain configurations in Xcode. Periphery is introduced as a dead code detector that can also identify regressions, with a recommendation to use it in pre-push hooks for larger projects. Lastly, Terminal-notifier is proposed to alert developers after hook processes complete, allowing them to attend to other tasks and return to review the results of the checks. The article encourages contributions from readers for additional tools and invites them to engage with the content through clapping and following the author.

Opinions

  • SwiftFormat is considered the top tool for code formatting and should be integrated into the pre-commit hook to avoid slowing down the build process or losing file history in Xcode.
  • SwiftLint is deemed mandatory for code linting and should be run in strict mode pre-commit to ensure code quality without hindering the development workflow.
  • Periphery is valued for its ability to detect dead code and potential regressions, with a preference for its use in pre-push hooks, especially for larger projects.
  • Terminal-notifier is appreciated for its utility in notifying developers of the status of pre-commit or pre-push checks, enhancing multitasking and productivity.
  • The author expresses a strong preference against using SwiftLint in Xcode with warnings as errors and suggests a custom approach to run it on modified files only to avoid slowing down the build process.
  • The article emphasizes that formatting source code should not be a responsibility of the Continuous Integration (CI) system.

4 best tools to implement for your githooks in an iOS project.

Git Hooks are programs that you can place in a your .git/hooks/ directory to trigger actions at certain points in Git’s execution. These scripts are the best enablers for increasing the quality of your codebase or your productivity. Here is my selection of the best tools to use in your hooks that we use at Shopmium. I will present hooks code samples about :

  • SwiftFormat: Autoreformatter
  • SwiftLint: Swift linter
  • Periphery: Dead code detector
  • Terminal-notifier: Get notified after checks failed or succeeded

#1 SwiftFormat — the mandatory one

SwiftFormat is the #1 tool you should put in your git-hook pre-commit. SwiftFormat allows you to format iOS source code and the available options and configuration make this tool very flexible and powerful, on demand. This tool should NOT be used on the CI side, it’s not the job of the CI at all to format your source code. Incorporating this into Xcode has several drawbacks

  • Slowing down the build just for formatting
  • Seeing your code moving at each build 👀
  • Losing your xcode file history at each build

#2 Swiftlint — The second mandatory one

Dealing with a linter is mandatory and no developer should push code smells. Adding Swiftlint at the pre-commit level acts as a gate, in addition to using it in Xcode.

  • Do not use SwiftLint in Xcode with warnings as errors (it will slow down your workflow).
  • SwiftLint has no baseline system: You have to implement a custom way to launch it in Xcode to allow it to run on modified files only, or it will slow down your build. Pre-commit is mandatory for fine-tuned linting.

I strongly advise running SwiftLint in strict mode at the pre-commit level like this:

#3 Periphery — Dead code detector (and regression)

Periphery is a tool that detected and report dead code in your code base. So it can detect regression such as broken features: Imagine you remove source code but also something you should not and the callee was called just once by the caller. Now it is isolated, and you could have a dead code warning for that !.

  • Put this in pre-push instead for bigger project
  • Can be useful on CI side

#4 Terminal-notifier — Take ☕️ and get notified

With all these tools above, pre-commit of pre-push could take time to finish, in addition to your own tools added. Terminal-notifier is a command line that allow to trigger a notification from the Macos Notification center. You will be able to commit, take your ☕️ , read teammates PR and get notified when pre-commit checks finished with success or fail !

  • Execute script on success or failure when tap on notification or open an app
  • Change the sound
  • Many more capabilities on the official documentation

Wanna contribute? If you know more tools that could be awesome, productive to add in a githook (commit, push or whatever) share it in comment. If you enjoyed the quality of this article, you can 👏 me, and if you want to see more content, you can follow me 🚀.

Git
iOS
Productivity
Tools
Git Hooks
Recommended from ReadMedium