sebastiandaschner news


monday, february 15, 2021

Welcome to newsletter #47!

I hope that y’all are doing well and can stay healthy. Here in Europe, the situation stays rather unchanged and I try to use the time to focus on programming things, also because as a flow activity it is a great distraction from the craziness of the world :-) In some past episodes and tweets I gave some pointers what new things I’ve learned in 2020, and I’ll continue focusing on that, until the Corona situation allows for different ways of working again.

 

What’s new

 

IntelliJ postfix completion

An IntelliJ productivity tip that has been mentioned a few times but might be still new for a few of you is using postfix completion.

After typing a variable or expression, you can expand and wrap the expression into something else:

For example, after typing "Hello World", you can add .sout and hit <Tab> which will expand the whole thing to System.out.println("Hello World");

Other helpful postfix templates, that I regularly use:

test.nn       -> if (test != null) { ... }
test.null     -> if (test == null) { ... }
booleanVar.if -> if (booleanVar) { ... }
"test".sout   -> System.out.println("test");

I like the way that the creators of IntelliJ are thinking about how people type and think, which often doesn’t follow the order of the keywords in the code, and they add possibilities to wrap the statements afterwards.

 

Productivity reminder: Use text editor templates

And let’s stay in the theme of templates and productivity reminders. What I do when writing technical things, emails, or, well, almost anything related to my job, I’m constantly using snippets, or live-templates, also in my plain-text editor.

I write a lot in VIM, and over time added a lot of words and expressions that I was constantly typing manually, such as my name. The point here is that besides IDEs also most regular editors include such features, and it does make sense to try them out if we’re writing a lot.

Some of my examples:

sd   -> Sebastian Daschner
j    -> Java
jkee -> Jakarta EE
arch -> architecture
app  -> application
swe  -> software engineering
dsom -> Dear Sir or Madam,
date -> `date -I` (e.g. 2021-02-15)

All of these snippets are also expanded by a hot key such as <Tab>, like in our IDE. I started to do this when I wrote my first book, ‘Architecting Modern Java EE Applications’, and since then the list of VIM snippets was growing and growing. As you can see, you can also evaluate expressions, such as the current time or date, or actually anything that you can do on the command line.

If you’re interested in how I’ve added these to my VIM setup, have a look at my Dotfiles repository.

 

Formatting decimal numbers in Java

A straightforward way how to format decimal numbers in Java when you have special requirements to the format, locale, or number of decimals, is to use java.text.DecimalFormat.

This class has been around for a long time and can solve all kinds of String-formatting related situations:

DecimalFormat format = new DecimalFormat("#.##",
    DecimalFormatSymbols.getInstance(Locale.US));

format.setRoundingMode(RoundingMode.FLOOR);

// will "cut" at two decimals
format.format(0.3); // 0.3
format.format(0.303); // 0.3
format.format(0.399); // 0.39
format.format(11.399); // 11.39
format.format(111.399); // 111.39

 

Thanks a lot for reading and see you next time!

 

Did you like the content? You can subscribe to the newsletter for free: