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:

Eclipse line shortcuts

You can only have one thing in your clipboard, right ?

So you end up having to paste somewhere temporarily to avoid overwriting your clipboard content…you lose time and you mess up your files with temporary pastes that you forget to clean up…

There are other ways. If you want to clone a block of lines and move it somewhere nearby, proceed this way:

  1. Select the block of lines by pressing Shift + Arrow up/down.
  2. Press Ctrl + Alt + Arrow down, this will clone the block of lines.
  3. Move the lines where you want them: Alt + Up/Down.

You may still think copy & paste is faster, but notice how these shortcuts are similar, if you put your fingers on Alt and Ctrl and the up/down arrows you can combine them very quickly. It gets even faster for a single line.

In addition to that I would recommend using Ctrl + D for quickly removing lines and Ctrl + Shift + C to comment/uncomment lines. There are many other shortcuts,  but it does not make much sense to list them without giving a use case, simply press Ctrl + Shift + L.

There are various Multi clipboard tools/plugins, but I do not like depending on those.

Conclusion

This is very similar to copy & pasting, but it allows you to keep your clipboard content where you would normally not. These shortcuts can prove very effective when combined.

Related posts