The Compulsive Coder, Episode 4: You Create, It Assigns

by Jeff Langr

August 30, 2016

roomba long exposure

Image courtesy Chris Bartle. License.

Some languages are destined to be always overly verbose. One feature I’d hoped for in later versions of Java was implicit typing. In C#, for example, you can declare:

    var customer = new Customer("Jose Cañusi");

It’s not too hard for the compiler to figure out that customer is of type Customer.

Java still has no implicit typing. Any time you need a non-specific Customer instance, you need the following code:

Customer customer = new Customer("Elmer Sklue");

I’m out of breath doing all that typing! Well, not really: I didn’t do all that typing. But I’ve suppressed a scream too often pairing with folks who do type the word customer three different times.

We have computers to compute for us. Never mind that the Java language is a cranky old uncle, you at least have a Generation Z development environment if you’re using Eclipse or IDEA.

I’m pretty sure I first saw J.B. Rainsberger demonstrating the following tip at least a dozen years ago.

Type the instantiation (right-hand) side of things first:

new Customer("Anne Teak");

Do not type the left-hand side! Instead, press Cmd-1 (Quick Fix; the corresponding keystroke is Alt-Enter in IDEA). Select Assign statement to new local variable and press enter. If you’re young enough to store away another shortcut, use the slightly more effective Cmd-2-L key combination instead.

Unnecessary typing represents distraction, waste, and risk.

Pingback: The Compulsive Coder, Episode 1: The Stub Comment

Pingback: The Compulsive Coder, Episode 2: Syntax Coloring

Pingback: The Compulsive Coder, Episode 3: Typing Isn’t the Bottleneck

Pingback: The Compulsive Coder, Episode 4: You Create, It Assigns

Pingback: The Compulsive Coder, Episode 5: Extract Method Flow

Pingback: The Compulsive Coder, Episode 6: this Duplication is Killing Me

Pingback: The Compulsive Coder, Episode 7: Please AAA

Pingback: The Compulsive Coder, Episode 8: You Might Not Like This

Comments

Michaeli September 24, 2016 at 4:16 am

http://openjdk.java.net/jeps/286

Was sending you a question and noticed I had just seen this fly over the wire on Reddit the other day. Great minds think alike.


Share your comment

Jeff Langr

About the Author

Jeff Langr has been building software for 40 years and writing about it heavily for 20. You can find out more about Jeff, learn from the many helpful articles and books he's written, or read one of his 1000+ combined blog (including Agile in a Flash) and public posts.