I am preparing my android application for production so I have enabled most lint warnings. One particular warning is wide spread, but I don't understand it.
Here is the warning:
Type of field 'loginF' is concrete class 'LoginFragment'
And here is the description:
Reports any instance fields whose type is declared to be a concrete class, rather than an interface. Such declarations may represent a failure of abstraction, and may make testing more difficult. Declarations whose classes come from system or third-party libraries will not be reported by this inspection.
Any elaboration would be appreciated.
Note: The IDE I am using is 'Android Studio'.
It's complaining because you're coupling this class (which contains the loginF
field) with LoginFragment; if you change LoginFragment, you may have to change this class.
Alternatively, look at the methods you're using on LoginFragment in this class. Do they collectively form some behaviour that you can specify in an interface?
e.g. if the only method you call on loginF
is initiateLogin()
, then perhaps you can have:
interface Login {
void initiateLogin();
}
class LoginFragment extends Fragment implements Login {
@Override
public void initiateLogin() {
// do login
}
}
such that instead of declaring LoginFragment loginF = new LoginFragment();
, you use Login login = new LoginFragment()
.
Then you can change LoginFragment
so long as it keeps initiateLogin()
method, or even swap out new LoginFragment()
for any other implementation of Login
, which might be useful for mocking in tests.
A very contrived example. I would suggest using the default lint config, not this super strict one.