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:
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:
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.
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.