How to fix pmd violation “NullAssignment”?

前端 未结 3 1837
囚心锁ツ
囚心锁ツ 2021-01-23 19:42

PMD report NullAssignment of the following code, what is the best practice to fix it?

Assigning an Object to null is a code smell. Consider refactoring.

3条回答
  •  后悔当初
    2021-01-23 20:20

    Reached this post while searching for other info, and noticed none of previous answers are right (IMO).

    The reason for the null assignment in supplied code is obviously not about garbage collecting, but about being able to stop and recreate new timers, only one timer being able at a time.

    I don't see any reason for using the temporary variable on the stopTimer method, but looks unrelated to original issue.

    I would say this code is fine, and a SuppressWarnings("PMD.NullAssignment") annotation could be added to avoid the violation; PMD is just highlighting null assignments are usually a smell, not a fact of any issue.

提交回复
热议问题