Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
eclipse templates -- creating and setting a permanent variable
In eclipse (java) Window->Preferences->Java->New new templates can be created.
In the field "Pattern" I entered
System.out.println("DEBUG ${label}");
${cursor}
I like old-style debugging, so I use a lot statements like System.out.println("DEBUG A"); ... System.out.println("DEBUG Z");.
The question is, can (and if so, how) I have the variable ${label} to be permanent and auto-increasing?
So that calling it first time, it has value "A", second time "B" and so on.
The question is not restricted to the use-case above, templates are a mighty tool, if you know how to use it.
1 answer
I can't think of a way to remember and increment a counter in templates. However, if you're content with a random number rather than an incrementing counter, then using a date template variable should work. As an example:
System.out.printf("Debug %d\n", ${d:date('S')});
This template will insert a date specifying only the number of milliseconds. In practice, this is just a random 3-digit number. If you want an even longer number to reduce the risk of collision, you could include the seconds as well:
System.out.printf("Debug %d\n", ${d:date('sS')});
The date is formatted according to java.text.SimpleDateFormat. Documentation for templates can be found here.

1 comment thread