About Byron

Hope you enjoyed the tip. Browse the Tips Archive for more.

Use Fast Views to maximise screen space

As a developer, the main area on my screen is the editor, the place where the code is. To work better and faster, I want this space to be as big as possible.

The default Java perspective in Eclipse comes with a number of views that surround the editors. This gives the editors less space, which means I need to do a lot more work to get my coding done, eg. more scrolling, paging, etc.

Eclipse helps out with a feature called Fast Views, a way to make views smaller so they take up less space. It’s the first thing I do to a new workspace (if I don’t have my workspace preferences handy) and I’d recommend it to anyone struggling with small monitors or stuck with just one monitor.

Continue reading

Jump to the start and end of methods, loops, blocks and XML tags

Common wisdom says that we should keep our methods small and avoid nested if statements and nested loops. But I’ve seen some huge methods (1000’s of lines) and code that included an if in a while in a for loop in an if.

The problem is finding out where the block starts and where it finishes (ie. where it’s opening and closing brackets are). Also, most frameworks require XML configuration, leading to big XML files that become hard to navigate, eg. moving between tags.

But, once again, Eclipse makes it easy to jump between the opening and closing brackets/tags using a keystroke.

Continue reading

Generate validation code for required variables using templates

You may find yourself having to write the following code once too often, especially at the start of a method to validate arguments:

if (account == null) {
    throw new IllegalArgumentException("Account is required");
}

//Or for strings
if (name == null || name.length() == 0) {   //Or even StringUtils.isBlank() of Apache Commons
    throw new IllegalArgumentException("Name is required");
}

Well, here  are templates that makes it easy to validate an object or string and throw an IllegalArgumentException if it isn’t.

Continue reading

Find all classes that override/reimplement a method

Inheritance is a great feature of OO. It can make browsing and navigating code a lot more difficult though, especially in a class hierarchy where some classes override a method along the hierarchy but others don’t.

Here’s an example. Your task is to find all the classes that implement/override the method connect().

public interface DataSource {
 public void connect();
 public void update();
 public void disconnect();
}

public abstract class DBDataSource implements DataSource {
 public void connect() {
 // ... Connect to DB
 }

 public void disconnect() {
 // ... Disconnect from DB
 }
}

public class AccountDataSource extends DBDataSource {
 public void update() {
 // ... Update account
 }
}

public class ClientDatasource extends DBDataSource {
 public void connect() {
 // ... Special code to connect to client DB
 }

 public void update() {
 }
}

In this case DBDataSource implements the method, ClientDataSource overrides it but AccountDataSource doesn’t. Finding this out manually is slow and time consuming because the classes are in different files.

There are two lesser known features of Eclipse that can you help. The first one is an unknown feature of the Type Hierarchy called Lock View and Show Members and the second one is Quick Type Hierarchy which is my favourite and which I use all the time.

Continue reading

Move, copy and delete lines with a single keystroke

When coding new features or making big changes to existing functions, I end up moving around a lot of code by typing something, deleting it, copying another line to modify it slightly, moving another line up or down, etc. Deleting a line is also more common than you think.

Instead of selecting lines and using standard copy, paste and delete to copy, move or delete lines, I’d rather use only single keystrokes. Why? Because selecting a line is slow since you have to move the beginning to fully select it. Then you have to copy, move to the new location and paste. Also, it impresses any colleagues watching me code.

Continue reading

Write a filter loop in 20 seconds using Eclipse templates

We’re still waiting for closures to come to  Java – the saga continues. Until then we’ll need to write the filter boilerplate code  manually. Filter loops often take an input array/collection, match each element with a condition and, if the condition matches, adds the element to another, filtered list. Here’s an example:

List filteredNames = new ArrayList();

for (String name : names) { //names is the original String Collection/array
    if (name.contains("Sam")) {
        filteredNames.add(name);
    }
}

This code is tedious, error-prone and a time-waster. It takes a minute or two or even longer to write without any errors.

You can use Eclipse templates to generate a filter loop in about 20 seconds. If you haven’t worked with templates before, see this post for an overview.

I’ve created a template that produces a List declaration, enhanced for loop, if statement and an add to the filtered list. The list’s type is taken from the nearest array/collection in scope so should be a fairly good match. It even generates import statements for the List and ArrayList.

You can either import the template (recommended; much faster) or create the template manually.

Continue reading

Faster null checking in Java with Eclipse templates

We constantly have to check for nulls in Java. There are ways/ideas to cut down on them (eg. Safe Navigation Operator, type annotations, Null Object, better API design) but they’re either not here yet or not used often enough. So you’ll often need code similar to the following:

Transport transport = SomeAPI.createTransport(...);
//...
if (transport != null) {
    //... Do something with transport...
    if (transport.getSomething() != null) {
        //... Do something with transport.getSomething ...
    }
}

Typing the null-check is fine until you have to retype it 5 times a day, every day for 10 years (well, at least it feels like that).

You can use an Eclipse template to stop the madness. If you haven’t worked with templates before, see this post for an overview.

I’ve created a template that produces an if-statement with a not-null check on a variable that Eclipse will guess at first but give you the option to override it.

You can either import the template (recommended; much faster) or create the template manually.

Continue reading

How to tweak Eclipse templates to suit you

I discussed templates and why to use them in another post. Basically, we use them to eliminate the pesky, repetitive code zombies. I also showed some built-in templates and what they do.

But what if you want to add your own template or modify an existing one? Managing your own templates is important because your business domain and how you work determines to a large extent what is the common code you use all the time.

Don’t be tempted to say “It takes too long to create the template. In that time I could’ve typed the code myself.” Are you going to say that the next 50 times you have to type the same code? I always force myself to sacrifice the 2 minutes because I’ll save me 20 minutes in the long run.

Continue reading

Speed up boilerplate code with Eclipse templates

Take the following piece of code:

for (int i = 0; i < arguments.length; i++) {
    String argument = arguments[i];
    // ... Do something with argument...
}

How many times have you typed out a similar for-loop, mistyped arguments the 2nd time, made a syntax error by forgetting a semicolon, etc, etc. Same goes for writing a main method or a try-catch block. Even typing an enhanced for-loop in the above example is slow.

To me they’re code zombies. Nobody likes them, everybody wants to get rid of them asap and just when you think you’ve killed one, it wakes up again and doesn’t stop… ever.

This is where templates bash in with modified shotguns and spiked baseball/cricket bats. You use templates to rid yourself of these code zombies, because life’s too short and fast to have to write long and slow code.

Continue reading