Android Studio tricks: Extra Inspections
When using Android Studio for development all of us have already encounter that the IDE shows some warnings on some specific actions we try to perform on the code, that is the Inspect code function and together with Lint helps developers to ship better and less buggy code.
But what happens if the provided Inspections are not enough for us? What if I want to be more strict with the code that is written in my app? Then you might encounter yourself having to read through hundred of Inspections in order to find some good ones. In this article I'd like to recommend some useful inspections.
Let's get started:
Some useful Inspections
First of all, go to Android Studio > Preferences > Search for Inspections. I'll go group by group listing all the inspections I personally find useful, please refer to the description provided in AS for further explanation:
Android > Lint > Correctness
- Newer Library Versions Available
- Magic Number
Java > Assignment Issues
- Assignment used as condition
Java > Class metrics
- Inner class too deeply nested (nesting limit = 2)
- Abstract class may be interface
- Multiple top level classes in single file
- Non-'final' field in enum
- Utility class not 'final'
- Utility class without private constructor
Code maturity issues
- Call to 'printStackTrace()' (disable for tests)
- Use of System.out or System.err
Code style issues
- Call to 'String.concat()' can be replaced with '+'
- 'equals()' called on Enum value
- 'expression.equals("literal")' rather than '“literal”.equals(expression)'
- Field may be 'final'
- 'indexOf()' expression is replaceable with 'contains()'
- Redundant interface declaration
- Redundant no-arg constructor
- 'size() == 0' replaceable with 'isEmpty()'
Control flow issues
- Conditional expression with identical branches
- Unnecessary 'null' check before 'instanceOf' expression
- Missing @Override annotation
- Double brace initialization
- Array.length in loop condition
- Field repeatedly accessed in method
- Private member access between outer and inner classes
Java language level migration aids
- [Uncheck] 'for' loop replaceable with 'foreach'
- [Uncheck] ‘while’ loop replaceable with ‘foreach’
- Suspicius getter/setter
- Inner class may be 'static'
- Zero-length array allocation
- Call to simple getter from within class
- Call to simple setter from within class
- Dynamic regular expression could be replaced by compiled Pattern
- 'equals()' or 'hashCode()' called on java.net.URL object
- Field may be 'static'
- Map replaceable with EnumMap
- Method may be 'static'
- Set replaceable with EnumSet
- Single character string concatenation
- Array comparison using '==' instead of 'Arrays.equals()'
- Cast conflicts with 'instanceOf'
- Comparable implemented but 'equals()' not implemented
- Constant conditions & exceptions (very nice to use with @Nullable and @NonNull annotations)
There are many more that could be useful for you/your team, so I really encourage you to take a look at the available ones and add them to your local repo so everybody gets the same warnings.
[Update] As Saúl Molinero mentioned in the comments “typing ‘cmd + shift + a’ + ‘run inspection by name’ allows you to run a single inspection without need to run a profile filled with a set of inspections.”
Do you have any favourite inspection? Please share in the comments!