To standardize code style among different project and teams, snippet is one of the tools to implement coding standard.
However, snippet is the two-side blade for developer. It will introduce duplicate code and result difficult to maintain if abuse. The recommend place to use snippets are:
- Unit test for high level format;
- DTO / DAO with specific handling in getter / setter method;
- Predefined markup (e.g. XML / HTML, JSON, YAML);
In this demo, it will use JUnit parameter test as sample to create snippet.Steps:
- Create Live Template
Highlight code would like to add into snippet, press Control + Shift + A, and input save as live template, then click Enter. - Add snippet abbreviation and variables.
In Save as Live Template dialog, in Abbreviation, input name ptest, and variables are format like $variableName$.
- Configure snippet variables.
Click Edit Variables, in expression, input default value. You can use method provided to customize value (e.g. upper case, camelCase, etc) to fit your need.
- Test snippet.
In project, type snippet abbreviation, expected it will show in intelli-sense, after press enter, snippet will be render.
Leave a Reply