What are code snippets in Xcode? ๐Ÿค“

A code snippet is a small amount of reusable code. Often overlooked, it is a powerful tool to improve the consistency of a codebase and to speed up your development process.

Consistency

When working on a codebase, we tend to write the same small portions of code again and again. A good example would be:

// MARK: - Privates
private extension MyClass {
    // ...
}

or something like:

func testMyCode() {

    // Given
    // ...

    // When
    // ...

    // Then
    // ...
}

Itโ€™s very easy to forget that MARK: -, make a typo or break a convention. While it doesnโ€™t impact the project that much, itโ€™s always nicer to work on a codebase that is consistent from file to file.

Using code snippets for those repetitve portions of code eliminates those inconsistencies, improving the readability and consistency of a codebase.

Speed

Code snippets allows us to generate a significant amount of code in a small amount of time. Especially if you configure them with a completion shortcut, things get real fast! ๐Ÿš€

Example with vs without

How can we use them? ๐Ÿ› 

There are different ways to use code snippets in Xcode. You can either decide to you use the pre-defined snippets available out of the box in Xcode or create your own.

Pre-defined code snippets

You can find your collection of code snippets using Cmd + Shift + l. From there you can easily:

  • Browse your code snippets

  • Edit by control-clicking the code snippets of your choice

  • Delete by pressing Delete om the code snippets of your choice

Create your custom code snippets

The best way to use code snippets is to create your own. Xcode makes it super easy! There are multiple ways to create code snippets.

  • Go to Editor
  • Select Create code snippet

You will be prompted by this window:

from which you can create your code snippet. Iโ€™d strongly recommend adding a Completion Shortcut to your code snippets as it makes them even faster to use!

You can also:

  • Select a portion of code
  • Control-click
  • Select Create code snippet

This will create a new code snippet from the code you selected and open the code snippet library so you can customise it.

Itโ€™s also good to mention that you can insert placeholder tokens with <# and #> in your code snippet to make it a bit more customisable. Also, it makes super easy to fill in your code snippet because you can use Tab to switch from one placeholder to the other! ๐Ÿง 

Share and import code snippets

All your code snippets are in ~/Library/Developer/Xcode/UserData/CodeSnippets/.

From there you can easily copy them and share then with your team! You can also import a code snippet in that same directory and it will be added to Xcode!

Examples ๐Ÿ“š

I personally use code snippets very often. Here are some of my favourites:

  • Test body
func test<#testcase#>() {

    // Given
    <#code#>

    // When
    <#code#>

    // Then
    <#code#>
    
}
  • Private extension
// MARK: - Privates
private extension <# class name#> {
    <#code#>
}
  • Todo
#warning("TODO: yourname, <#date#> - <#description#>")
  • Testable
@testable import <#target#>