user-icon Balazs Baltringer
07. August 2017
timer-icon 2 min

Working with PostFix Code Completion

Nowadays we developer are happy about code that we do not have to write ourselves. The common IDEs like Eclipse and IntelliJ support  us with code completion and templates to write code faster.

Both IDEs have a lot of built-in templates to generate boilerplate code. They can generate for-loops, null checks, switch statements, instanceof checks and a lot more code snippets. Every project is different and has other requirements. The best thing is that these templates can be extended directly by the user in the IDE.

To get a feeling for the existing templates take a look at the preferences/settings:

Eclipse: Window/Preferences/Java/Editor/Templates
IntelliJ: Settings/Editor/Live templates

Another way

The so called PostFix Code Completion mechanism is another way to generate code within our IDEs. It is supported in both Eclipse and IntelliJ.
As the name “Postfix” suggests, we can append short suffixes to code blocks to invoke a completion. The completion will use the preceding code to fill out a template. This mechanism really helps me to stay in the flow of writing code. Sounds good? Here are some examples:

Generate local variable

Generate new field

Generate a private/public constant (Eclipse only)

Generate an enhanced for-loop

Generate println statement

… a lot more

Both IDEs support a lot more postfix templates. Eclipse provides 22 built-in templates, IntelliJ even 30 java templates and 17 templates for Kotlin. It’s worth to give it a try.

Setup

Eclipse only supports this feature with an additional plugin. Use Install New Software and paste the following in the update site input field:


Select the checkbox for JDT Postfix code completion. The JDT Patch is only required for Eclipse Luna or even older versions (I hope nobody has to use such a prehistoric eclipse version).
IntelliJ is already delivered with that feature.

Within Eclipse the new completions can be found in the preferences dialog where the default templates are located. The only difference is that the new ones have the Java postfix context. This is also the place to add new templates.

In IntelliJ the postfix completions are located in Settings/Editor/General/Postfix Completion. However, I haven’t found a way to add my own templates to IntelliJ.

Conclusion

I like this solution to generate small code elements. It allows me to stay in flow and avoid remembering weird keyboard shortcuts.

If you know a way to add new templates to IntelliJ, let me know in the comments. Also feel free to drop me a comment with your own productivity hacks.

Comment article