Swift Witness for Sourcery
Sourcery is generating buzz in the mobile development world. It generates Swift code from your source code, saving you time and potential mistakes.
Join the DZone community and get the full member experience.Join For Free
Yep, think we’ve outdone ourselves with the obscure title references this time. If you knew it was from Malachi 3:5, our congratulations!
What Is Sourcery?
"Sourcery scans your source code, applies your personal templates, and generates Swift code for you, allowing you to use meta-programming techniques to save time and decrease potential mistakes.
Using it offers many benefits:
- Write less repetitive code and make it easy to adhere to DRY principle.
- It allows you to create better code, one that would be hard to maintain without it, e.g. performing automatic property level difference in tests.
- Limits the risk of introducing human error when refactoring.
- Sourcery doesn’t use runtime tricks, in fact, it allows you to leverage compiler, even more, creating more safety.
- Immediate feedback: Sourcery features built-in daemon support, enabling you to write your templates in real-time side-by-side with generated code.
Sourcery is so meta that it is used to code-generate its boilerplate code."
We’re pretty leery of tools like this in general as they end up being yet another cesspool of technical debt, but that this one is based on SourceKitten is interesting enough to at least pay some attention to it. And it’s catching a bit of buzz.
Switching between our Android projects written in Java (or more recently, Kotlin) and iOS projects in Swift makes me sorely miss annotation processing. Annotations in Java can be used to generate common code and things that Swift developers would have to hand write. Hand written code has to be bug-free and consistently updated and maintained. Things like implementing equals and parsing JSON typically require boilerplate code, unique enough that you cannot abstract it away.
Although Swift does not have annotation processing, we can get pretty close with Sourcery. Let’s take a look at how it all works in this quick Sourcery tutorial…
I’m really interested in Sourcery, so I installed it in my one of repositories. In my case, I generated AutoEquatable and AutoHashable at first because I felt these implementations contain really a lot of boilerplate code.
If I add a new property or a new API, it prevents a human error! Though it looks trivial, Sourcery uses a safe Hashable as I mentioned in Safe Hashable in Swift. Isn’t it cool?
The Magic of Sourcery
Sourcery is a code generation tool for Swift. It is particularly well suited for auto-generating code that is very repetitious; code most developers refer to as “boilerplate”. Some examples that come to mind:
- Equatable implementations.
- Hashable implementations.
- struct initializers.
- Lens implementations.
Think of the potential: you can basically generate all the boilerplates of your architecture be it CLEAN or VIPER, you can generate all the protocol implementation or even the Realm data object, or even a view based on the variables that are being declared (Sourcery does support inline code generate). A declarative view! The sky is the limit!
If you’re looking for a good project to try this out with, a decent set of VIPER templates would be eagerly welcomed. Let us know if you come up with something!
Published at DZone with permission of Alex Curylo, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.