Tips & Tricks

Context-sensitive analysis with AppCode 3: Never miss a value again!

One of the most notable features of AppCode is code analysis, which helps keep your code accurate and clean. The flexible mechanism of resolving problems works on the fly and allows you to easily improve code structure, detect unreachable and redundant code, conform to numerous guidelines and standards, detect performance issues, and much more.

AppCode not only highlights code issues in the editor, but also suggests intelligent quick-fixes and adds useful indicators to the right-hand gutter which let you instantly see the status of the file and navigate around the detected issues:
gutter
AppCode also provides shortcuts for navigating between these issues (Go to next highlighted error with F2; go to previous with Shift+F2). This detailed tutorial on code inspections will help you explore and learn more useful shortcuts and preferences.

In AppCode 3 we improved the handling of one of the most intelligent case for code analysis: context-sensitive analysis. The IDE helps you with many smart scenarios, for example if a condition is redundant, or if you are trying to access a reference that can be nil, or if you use an unrecognized selector, or if you cast to a type that’s probably incompatible. AppCode warns you about any potential problems and suggests quick-fixes for all cases. Here we’d like to show you a few examples.

Single-state conditions
Relying on thorough data flow analysis, AppCode is aware of all possible values of a variable and can decide in some cases if a given condition is always true or false at a certain point of program execution. The IDE successfully handles many complicated cases such as several data flow branches with input parameters involved:
branch_sample
…or tracking variable values through loops:
loop_do_while

AppCode highlights the potential problem and suggests an appropriate quick-fix, which can be applied simply by pressing Alt+Enter:
loop_sample_fix

Unreachable code
The most popular case for data flow analysis is unreachable code, which may indicate a flaw in logic. For example:
unreachable_switch

Reference may be nil
Nil references often cause serious errors, but as they say forewarned is forearmed. AppCode will highlight potential problems for you:
reference_nil

Type checks
Type checks can be tricky. It’s easy to miss an incompatible cast or unrecognized selector call, especially when working with a complicated inheritance tree:
cast_warn

And what’s your case for the context-sensitive analysis that you’d like AppCode to cover?

Read more Tips&Tricks.