Code Inspection & Refactoring Tools

Xcode: The IDE Itself

Chapter 7 ยท Code Inspection & Refactoring Tools

Debugging and profiling find problems while the app runs. This chapter covers tools that look at your code without running it: Xcode's static analyzer, its refactoring commands, and SwiftLint, a separate tool that fills a gap Xcode does not cover by itself. The facts about SwiftLint come from its own repository page. The claims about the analyzer and refactoring come from tutorial summaries and a project blog, because Apple's documentation pages did not give me usable text, and they are marked.

The Static Analyzer

Xcode's static analyzer is the Clang static analyzer. It finds hard-to-produce, edge-case bugs without running your code, and shows the sequence of steps along which a bug occurs. You run it manually with Product → Analyze, or with Shift-Command-B.

The Swift Question
A summary I read says the Clang analyzer finds bugs in C, C++ and Objective-C code, including in mixed Swift and Objective-C projects, but does not directly analyze pure Swift code in current Xcode versions. I could not confirm that against Apple's documentation, and a WWDC21 session on the static analyzer exists that I was unable to read. Because this course is about Swift, treat the analyzer as something whose Swift coverage you should check in your own Xcode version before relying on it.

Refactoring Commands

Xcode's Refactor menu covers several operations. Two are well described in the sources.

OperationWhat the sources say
RenameThe refactoring engine can transform code locally within a single Swift source file, or globally, such as renaming a method or property that occurs in multiple files and even different languages
Extract to MethodSelect the code and choose Refactor → Extract to Method to move it into its own method
Why Use the Tool, Not Find-and-Replace
A rename done by the refactoring engine understands your code's structure, unlike a text search that can change unrelated words or miss a use in another file. Clean Code, SOLID & Refactoring 8 covers the refactoring techniques themselves, such as Extract Method and Rename, and why they are done in small steps. Combine that chapter's discipline with this chapter's tools, and run your tests after each change.

A Swift.org blog post titled "Swift Local Refactoring" describes how these transformations work inside the Swift compiler project. I did not read enough of it to summarize, so I mention it only as a place to look.

SwiftLint: The Gap

SwiftLint is a tool to enforce Swift style and conventions, loosely based on the now archived GitHub Swift Style Guide. It contains over 200 rules covering naming, code structure and style practices. Another summary adds that it hooks into Clang and SourceKit to use the AST representation of your source files for more accurate results. It is maintained by volunteers and is not an Apple product.

NeedCovered by
Find bugs in C-family code without running itThe static analyzer
Rename and restructure code safelyXcode's refactoring commands
Enforce a consistent Swift style across a teamSwiftLint, a separate tool

Installing and Wiring It In

The repository lists these install options: Homebrew, Swift Package Manager (as a command or build tool plugin), CocoaPods, a pre-built package and Docker. For Xcode projects it integrates through build tool plugins, which it describes as recommended for modern projects, or through a Run Script build phase. On Apple silicon Macs with Homebrew you may need to add /opt/homebrew/bin to your PATH.

# Install with Homebrew brew install swiftlint # Check the current folder swiftlint # Apply automatic fixes where a rule supports them swiftlint --fix

Configuration lives in a .swiftlint.yml file. It supports including or excluding rules through disabled_rules, opt_in_rules or only_rules, custom regex-based rules, and path-specific includes and excludes. The example below shows the shape. The rule name is one I believe exists, but I did not verify it against the current rules list, so check the list before using it.

disabled_rules: - trailing_whitespace excluded: - Pods
Linting Is Not Testing
A linter checks style and structure against rules. It cannot tell you that the program does the right thing. Use it alongside the debugger and Instruments from Chapters 4 and 5, not instead of them. Also, swiftlint --fix changes your files, so commit or stash your work first and read the diff afterwards.

Hands-On Exercises

Exercise 1

Explain why renaming a method with Xcode's Refactor command is safer than find-and-replace, using the description of the refactoring engine in this chapter.

๐Ÿ“„ View solution
Exercise 2

Your team argues about brace placement and naming in code reviews. Say which tool from this chapter addresses that, how you would install and wire it into an Xcode project, and what you would do before running the automatic fix.

๐Ÿ“„ View solution
Exercise 3

A colleague says "Xcode's Analyze will catch the bugs in my Swift code." State what the sources support, what they do not, and how you would find out for your own project.

๐Ÿ“„ View solution

Chapter 7 Quick Reference

  • Static analyzer: Product → Analyze or Shift-Command-B; finds bugs without running code; reported to cover C, C++ and Objective-C, not pure Swift
  • Refactor: Rename works locally or globally, across files and languages; Extract to Method moves selected code into a method
  • SwiftLint: style and conventions linter with over 200 rules; not an Apple product
  • Install: Homebrew, Swift Package Manager plugin, CocoaPods, pre-built package, Docker
  • Config: .swiftlint.yml; swiftlint --fix applies automatic fixes
  • Unverified: the analyzer's Swift coverage in current Xcode, and the example rule name