Intellij Idea–in My opinion the most productive IDE for java–comes with bunch of features helping getting rid of Writi Ng repetitive Code–which is a everyday business no matter what language you use. One of them is live templates.
Live templates contain predefined code fragments. You can use the them to insert frequently-used or custom code constructs into your source code file quickly, efficiently, and accurately.
Idea comes with a set of live templates for Java, Maven, HTML, CSS and more and also enables to create custom templates.
Inserting Live Template
There is 2 ways to insert Live Template:
- Type Live template abbreviation (for example DEP) and next press TAB key
- Use shortcut Command + j (MAC)/Ctrl + j (PC) to the popup list of available live templates in current context
At the beginning it's quite difficult to remember all you wish to use and going back and forth from code to settings or BR Owsing list is not very efficient–especially if you don ' t know what's looking for. That's why I created Gallery of (in my opinion) most useful built-in live templates for Java and Maven so can be used as A cheat sheet:
Maven
- Dep–inserts <dependency/>
Iterations
- Fori–creates Iteration Loop
- Itar–iterates Elements of Array
- Itco–iterates Elements of Java.util.Collection
- Itit–iterates Java.util.Iterator
- Itli–iterates Elements of Java.util.List
Other
- Ifn–inserts "if NULL" statement
- Inn–inserts "If not NULL" statement
- Inst–checks object type with instanceof and down-casts it
- Lazy–performs Lazy Initialization
Plain
- Psfi–public static Final int
- Psfs–public Static Final String
Summary
Using predefined Java templates is just a beginning. You can find the use templates for particular frameworks on Github and I really encourage you to create your custom, Project specific ones.
If you created interesting common use live templates for Java or related frameworks feel invited to post them in comments.
Code faster with Intellij idea live templates