AppCode inspections for your code perfection
AppCode helps you keep your code accurate and clean and does so as you type so you don’t need to interrupt your coding process. This is what the inspection mechanism is about. If you’re not familiar with this mechanism, you can read our tutorial that describes the essential principles. In this post we’d like to focus on some of the issues where AppCode’s code inspections can help you.
The full list of inspections includes over 100 items, but here we’ll point out a few of the most interesting and useful ones. Do not hesitate to go to Preferences | Inspections for the full list.
Hides class scope
When the codebase is big and complicated sometimes you may create a local variable or parameter with the name of an existing instance variable. This can produce problems if not done on purpose. The “Hides class scopes” inspection helps you to pay attention to such places:
AppCode will suggest solutions (
Alt+Enter), like for example rename the parameter.
AppCode helps you find unused code whether it is a class, an expression result, a declaration, a macro, a method or a property. All of these are detected automatically and the IDE suggests the “Safe delete” quick-fix.
AppCode helps you comply with the deployment target API. In case you use some deprecated API or one that is not available for your target OS, the IDE shows you the inspection “Usage of the API unavailable for the deployment target”.
Key value coding
KVC is a powerful idea that allows you to access an object’s properties indirectly, using strings. It’s a convenient and straightforward approach but can be tricky if a mistake takes place in a field, method or property name and thus causes the exception in run-time. AppCode tracks these keypaths and checks they are defined.
And of course it suggests you a quick fix like for example to create the property with the given keypath:
Missing switch case
In case you have a wide-ranged enum you can miss some values while implementing a switch case operator over this enum type. Or you can possibly miss the default branch for the switch case over the integer type. This won’t be a problem with the “Missing switch case” inspection. AppCode tracks such places for you.
The quick fix automatically creates the missing branches (like default here).
To ensure all strings are translated correctly, the inspection “Non-localized string” helps you find
and localize them with NSLocalizedString.
After several refactorings and reviewings of the code some statements become clearly simple though you can miss it. AppCode will help you to make your code perfect in this sense. “Simplifiable statement“ inspection finds all the constant conditions, identical if branches and much more
and suggests you to simplify the code.
Fitting into receiver
‘Value may not fit into receiver’ inspection is very close to the previous one but in fact more general. If the receiver in the assignment narrows the value range it can cause errors. To stay protected from such issues one definitely should pay attention to the AppCode hint:
Continuing with types AppCode checks all of them for you and suggests which type casts are redundant. Still to leave it or to remove is fully up to you but why not to know the scope?
Hiding non-virtual function
In the project with a huge inheritance tree one can simply forget or just miss some methods from the base class and implement a function in descendant with the same signature as in the ancestor. AppCode helps you here too! Inspection ‘Hiding non-virtual function’ tracks this situation
and suggests the quick fix like making the parent class function virtual:
And what’s your favorite inspection? Feel free to check out the full list in Preferences | Inspections.