rspec/rules/S2199/java/rule.adoc

41 lines
974 B
Plaintext
Raw Normal View History

Once a value is known to be ``++null++`` or non-``++null++``, there's no reason to re-check it unless it has been changed (or potentially changed) in the interim. Doing so anyway may may just be an over-abundance of caution, but it could indicate a bug.
== Noncompliant Code Example
----
public void assignCoach(Team team, Person person) {
if (team.hasCoach()) { // team is dereferenced
return;
}
if (team != null) { // Noncompliant; if we got this far, team is not null
//...
}
if (person != null) {
// ...
if (disqualified) {
person = getAlternate();
}
}
if (person != null) { // Compliant; person may have changed since last check
team.setCoach(person);
}
if (person != null) { // Noncompliant; no changes to person since last check
// ...
----
ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]