Stop Typing Your Source Code

An introduction to creating source code faster with less typing using NetBeans 4.0.


Did you know that the keys on a standard keyboard are deliberately laid out in a way which slows down your typing? They are. This layout, called the QWERTY layout, was originally chosen for the first typewriter in 1872 because it kept people from typing so fast the mechanical type bars would collide.

This is just one of many reasons why you should stop typing your source code, or rather, why you should let NetBeans create the source code for you. This article contains a series of tips on how you can write source code faster with NetBeans 4.0 using as little keyboard input as possible.


Tip 1: Use Word Matching

Once you have typed a word in a source file you can retype this word anywhere by entering the first few letters of it followed by pressing Ctrl+K. This feature is called word matching and it works by looking for words which start with the letters you have just typed. Ctrl+K looks upwards in your source file and Ctrl+L looks downwards.

Once you have written the for loop shown below

for(Person person : personSet) {


which iterates through a set of persons, you can place your cursor between the curly braces and type "pe" followed by pressing Ctrl+K. NetBeans will immediately suggest the word "personSet" as it is the nearest word which starts with "pe", and pressing Ctrl+K a second time will make NetBeans suggest "person" which is the second nearest word.

In fact, in this special case you do not even have to type "pe" � just pressing Ctrl+K inside the loop will also automatically type the identifiers "personSet" and "person"; this works because these two identifiers are the first words encountered when NetBeans searches upwards in the source file. However, had you written ten words between the top of the loop and the cursor, just pressing Ctrl+K would make NetBeans cycle through all those ten words before finally suggesting "personSet" and then "person". In this case, typing "pe" before pressing Ctrl+K helps by making NetBeans skip all words which do not start with "pe".

Should you accidentally press Ctrl+K too many times and reach a word which is placed before the word you want, just press Ctrl+L a few times and NetBeans will move onwards to the word you missed. Finally take note that this feature works everywhere in all kinds of files � in fact it is quite handy in XML files where you can use it to type the names of tags already present in the file.

Tip 2: Create an Identifier Macro

Writing long identifier names makes code more readable. Remembering that an identifier called "i" actually identifies an instance of type "Invoice" and not an "Iterator" or an "ItemListener" can be hard, especially in lengthy methods; if it had not been for all the typing involved, a name like "invoice" would have been a better choice. Now, if only we could make NetBeans write "invoice" for us, there would be no reason to choose "i". This section presents a macro which does just that.

Imagine the cursor is placed right after "Invoice" (denoted with a "|") in the code below:

public class Order {
	public void setInvoice(Invoice |) {


When triggering the macro NetBeans will automatically type "invoice" by using the type name "Invoice" already present in your code. The macro simply retypes the previous word and changes the first letter of the retyped word to lower case. Now, once you have made NetBeans type the first occurrence of the word "invoice" remember to use the word matching feature (Alt+K) explained in "Tip 1" to retype the identifier.

Creating the macro is done by following these five steps:

  1. Select the menu "Tools > Options"
  2. Expand "Editing" and "Editor Settings"
  3. Select "Java Editor"
  4. Look at the right half of the window for the property called "Macros" and click the "..." button to the far right.
  5. Add a new macro named "Type identifier name using previous word" containing the following macro code (simply copy it from here):
    selection-previous-word copy-to-clipboard
    caret-next-word " " paste-from-clipboard caret-previous-word
    selection-forward to-lower-case caret-next-word

Finally click the OK button of the "Macros" window to close it but keep the "Options" window open - you need it in order to create a shortcut key combination for your new macro:

  1. Open the "Key Bindings" window by clicking the "..." button of the property named "Key Bindings"
  2. Click "Sort by Name" and select your new macro which will listed under the name "macro-Type identifier name using previous word"
  3. Add the "Alt+N" shortcut by clicking the "Add" button.

When creating macros you would normally use the macro recording feature which let you record mouse and keyboard gestures and save them as a macro. However, the macro shown here contains the "to-lower-case" action and this action is not yet represented by a mouse or keyboard gesture, so you need to add this action manually.

Tip 3: Create an Assignment Macro

This is one of my favourite macros! Usually when you define a constructor you need to assign the value of the constructor parameters to class variables as in this example:

public Person(String name, String emailAddress) { = name;
	this.emailAddress = emailAddress;

If you define the following macro and bind it to Alt+=, instead of typing "this.emailAddress = emailAddress;" you� can simply type "this.e" followed by Ctrl+K to get "this.emailAddress" followed by Alt+= which will type the rest.

See "Tip 2" for instructions on how to create the macro. Here is the macro code:

selection-previous-word copy-to-clipboard caret-next-word " = " paste-from-clipboard ";"

Once you get into the habit of using the Alt+= macro you will find it to be way faster than anything else.

Tip 4: Use Abbreviations

NetBeans has a long range of built in abbreviations for typing well-known words or even multi-line code constructs. You can try this out by typing "sout" followed by pressing the Space bar � this will make NetBeans type a complete standard output statement for you, i.e.


NetBeans does not type a "|" character as shown, rather the IDE places your cursor at that location after the abbreviation has been replaced.

The built in abbreviations are a real time saver, but you can benefit even more from the abbreviations feature by defining your own custom abbreviations.

A good way to start is by looking through your existing code files and locating code constructs which you use frequently. A typical example of a frequently used construct could be the if/throw construct found in this method:

public void remove(int lower, int upper) {
	if(lower < 0)
		throw new IllegalArgumentException(
			"Parameter \"lower\" has to be a " +
			"non-negative number."

This method starts with a guardian statement which asserts that the first parameter is a legal argument � if not, an IllegalArgumentException is thrown.

Since placing such guardian statement is good, common practice, you can benefit from adding an abbreviation which NetBeans will expand to the if/throw construct. This can be done as follows:

  1. Select the menu "Tools > Options"
  2. Expand the nodes "Editing", "Editor Settings", and "Java Editor"
  3. Click the "..." button of the row containing the property "Abbreviations"
  4. Add an abbreviation called "ill" which expands to
    	throw new IllegalArgumentException("");

Note that NetBeans understands the use of the "|" character to indicate where the cursor should be placed after the expansion.

Now try typing "ill" and pressing Space - voila!

Of course you could create a macro which types exactly the same thing, but an important benefit of abbreviations is the fact that they are easy to remember because you can give them mnemonic names like "ill" or even "illarg" instead of cryptic keyboard combinations like "Ctrl+Alt+I". The downside to abbreviations is they cannot trigger a macro � at least not yet...

Tip 5: Use Source Templates

The really easy-to-use template feature of NetBeans is completely overlooked by many developers. If you need to create many source files which have many aspects in common you should define a template containing these common aspects.

For instance, in many systems you have rules for how an exception class should be defined or rules which say that all classes extending class X should contain certain methods, variables, documentation or the like.

When writing a new class, what many developers do is to start out from the empty "Java Class" template and write everything by hand; often this involves typing code which you have written before. Instead you should find a class which is a good starting point, right-click its tree node and select "Save As Template...". Now, whenever you need to create a similar class you just right-click the package in which the new class should be placed, select the menu item "New > File/Folder..." and choose the template.

Defining a template from an existing source file might actually give you more content than needed, but once the template has been defined you can customize it to remove any unnecessary code by selecting the menu item "Tools > Options" and locating the template under "Source Creation and Managent". You simply right-click its tree node and select "Edit", edit the file, and save it.

If you provide an API to your co-workers in which they need to implement the same interface quite often, you could help them by providing a source template along with your API. Sharing a template is as simple as copying it from one machine to the next. The templates are stored in the folder

"... [user home dir]\.netbeans\4.0\config\Templates\[your template folder]"

If you use versioning, I warmly recommend creating a set of project templates and sharing them through your versioning system.

Tip 6: Edit Getters and Setters Efficiently

If you write Java beans by hand a huge amount of time might be wasted on writing and maintaining the trivial getter and setter methods. Fortunately, NetBeans can both generate these methods for you, and the IDE even has a built in mechanism for maintaining them efficiently. Since NetBeans 4.0 the refactoring mechanism has supported generating default getter and setter methods from a class' variables. To create the getName and setName methods shown here

public class Customer {
  private String name;

    public String getName() {
      return name;

    public void setName(String name) { = name;

all you have to do is to declare the name property, right-click it, and select the popup menu item "Refactor... > Encapsulate Fields...".

Once the methods have been generated, you might want to change the name of the property "name". This could potentially involve quite a lot of typing, since the word "name" or "Name" is now scattered all over your source file. Luckily, NetBeans has a really nice feature called "Bean Patterns" which solves this problem. If you locate your class in the Project tree and expand its tree node, NetBeans displays a node called "Bean Patterns" which contains a subnode for each bean property of the class. This node can be renamed by hitting either F2 or by right-clicking and selecting "Rename...", and if you rename your property this way, NetBeans will make all the necessary changes for you.

The Bean Patterns feature also allows you to change the property's type efficiently. If you right-click the property's tree node and select the popup menu item "Properties", this opens a window containing information about the property. Here you can change the type and NetBeans will alter your source code accordingly.

Tip 7: Use the "Fix Imports" Tool

In brief: You should never write import statements. NetBeans 4.0 has introduced a really brilliant feature called "Fix Imports" accessible by the keyboard combination Alt+Shift+F. This feature analyses your source code and looks for class names which are not yet imported. If a class X has not been imported yet and only one class in your whole project is called X, NetBeans will import this class automatically. If two classes in two different packages are named X, NetBeans will bring up a dialog asking which class you prefer to import. "Fix Imports" works extremely well, and it automatically removes imports which are no longer used.


By using these tips you will soon find yourself producing better code with less syntactical errors in less time. Also, if you are among the many developers who have experienced aching fingers in coding intensive projects, making NetBeans write the code for you is definitely the way to go.

The more you customize the IDE to fit your project's specific needs the faster you can implement your system. NetBeans 4.0 is like the perfect racing car - fast and fun to ride. But just like the racing car, tuning it is essential. Tune the IDE by customizing the abbreviations, macros, and templates to fit your project's specific needs and unleash the true power of NetBeans 4.0!

About the Author

is the founder of the Danish software company ROCK IT, and the mastermind behind the groundbreaking, all-Java, component oriented content management system Puls, which is developed entirely with NetBeans (to be released in the spring 2005).