Code analysis and style checking
In addition to unit and integration testing, it's highly recommended to utilize static code analysis and linting to further ensure the correctness, efficiency, and cleanliness of your code.
Static code analysis
Xcode has a built-in mechanism for statically analyzing your code -- the
Build > Analyze command. It utilizes the Clang compiler to analyze source code and detect bugs and identify code smells.
Having a consistent and clean style across a team has many benefits:
- easier to spot bugs and code smells
- less friction and time wasted during code reviews
- better code comprehension and increase programmer effectiveness
There are third-party tools that can automatically format Objective-C code to adhere to a given set of code style rules.
OCLint is a static code analysis tool for improving quality and reducing defects by inspecting C, C++ and Objective-C code and looking for potential problems like:
- Possible bugs - empty if/else/try/catch/finally statements
- Unused code - unused local variables and parameters
- Complicated code - high cyclomatic complexity, NPath complexity and high NCSS
- Redundant code - redundant if statement and useless parentheses
- Code smells - long method and long parameter list
- Bad practices - inverted logic and parameter reassignment
A tool to format C/C++/Obj-C code
Supports approximately 60 rules for formatting the style of code. Somewhat limited in use.
Source Code Beautifier for C, C++, C#, ObjectiveC, D, Java, Pawn and VALA
- Indent code, aligning on parens, assignments, etc
- Align on '=' and variable definitions
- Align structure initializers
- Align backslash-newline stuff
- Reformat comments (a little bit)
- Fix inter-character spacing
- Add or remove parens on return statements
- Add or remove braces on single-statement
- Supports embedded SQL
- Highly configurable - 454 configurable options as of version 0.60
"There are currently 489 options and minimal documentation."
Recommended project setup
# install oclint $ brew cask install oclint # install clang-format $ brew install clang-format
Create folder at project root called
Automatically run Xcode's Analyze tool during every compile
- Analyze During 'Build' (Yes)
- Mode of Analysis for 'Analyze' (Deep)
- Mode of Analysis for 'Build' (Shallow / Deep if build times are still reasonable)
- Treat Warnings as Errors (Yes)
Further analysis with OCLint
The oclint script provided here is based off this gist.
This script will:
- Ensure that
oclintexists (assumed to be installed via homebrew-cask)
oclint-xcodebuildto generate the
oclintrequires for understanding the project hierarchy
By default, some of the oclint rules have been disabled and some directories excluded from analysis. These may need to be modified on a per-project basis.
As of this writing, the way oclint is being used here by Xcode (via oclint-json-compilation-database) doesn't provide a mechanism for importing a .oclint configuration file, which is why the config flags must be added to the invocation of oclint within this script.
Format code style target
clang-format has a limited set of code style options, so
uncrustify runs after it as part of the Format code style process.