Eclipse Indigo: static code analysis about static and why you should care

Eclipse Indigo comes with new optional static analysis ‘warnings’ which automatically detect methods that could be static.

Eclipse Indigo 'method could be static'

Eclipse Indigo 'method could be static'

To activate the warnings associated with this feature go to Windows->Preferences->Java->Compiler->Errors/Warnings->Code Style (or just search for ‘warn’, see screenshot) and click on the Ignore drop down box and select Warning. I have not tested the ‘potentially be static’, maybe I will turn it on when there are no warnings left to fix from the first one.

Reasons for the addition of this feature:

Static methods:

  • limit scope to static variables and passed parameters, thus avoid potential side effects
  • do not require an instance to be called
  • may not be part of a specific class and moved to a more appropriate place (tool/utility)
  • increased performance

Static methods are one step towards ‘pure functions’, here is the definition of a pure function: “The only result of invoking a pure function is the return value”. To learn more about pure functions, read this post about pure functions.

Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.

Antoine de Saint-Exupéry (French writer)

Adding the static keyword means removing (reducing) scope and potential mistakes (present and future). Bear in mind that a bunch of static methods are enough in some cases. This is why I am happy about this feature and you should too, so be sure to turn it on if you happen to be using Indigo. Have a look at the other warnings, some interesting ones are ignored by default.

Related:

Advertisements