the blog for developers

Method names in Java can be named _ and why static import is a good thing

When formatting messages with Messages.format code becomes quite noisy. So I thought why not use a special char for the method name? I used “_” which seems to be unintrusive.

Messages.format( “FILES_WRITTEN”, files);

then becomes

Messages._( “FILES_WRITTEN”, files );

combining this with the new static import feature from Java 1.5 (great timing :-) this becomes

import static api.cintoo.messages.Messages.*;

_( “FILES_WRITTEN”, files);

which looks shorter and less noisy than the first and as the default Sun solution for i18n.

I think about using “$” for the method name,

$( “FILES_WRITTEN”, files);

looks perhaps more familiar with Prototype.js using $ and this also looks like templating engines as Velocity/ JSP.

You can leave a Reply here. Of course, you should follow me on twitter here.

You can share this post!
Do you want to tell others about this article? Use the social bookmark icons to submit this artice to the service of your choice. Thanks.

About the author: Stephan Schmidt has more than 15 years of internet technology experience and 10 years experience in agile. He was head of development, consultant and CTO and is a speaker, author and blog writer. He specializes in organizing and optimizing software development helping companies by increasing productivity with lean software development and agile methodologies. Want to know more? All views are only his own.
Leave a reply.

Comments

john smith

You’re kidding right? You think a method name of _ is a good thing? Then hiding it with a static import is a good thing?

It’s not april fools day. Where’s the “I’m Kidding” at the end of the entry?

Tom

Three cheers. If it makes the code in question clearer, I agree. And Ctrl Click in the IDE will give all the details someone else might need when looking at the code.

stephan

Yes, this is no joke. I’m a great admirer of readable code. Which usually includes understandable and descriptive method names (the Messages code is not that bad).

After i18ning a big project most of the code was littered with i18n constructs. This reduced readibilty of the original business code (i18n understood as a orthogonal, non-business aspect) and made the code a lot less readable than before. To the point where lots of i18n was littered through Swing, display, logging and error code and the original business code was not understandable anymore.

With $ and _ as method names, the i18n part of the code fades away and no longer blocks the view to the really important code, the business one.

But of course I would strongly regiment the usage of static imports in my projects and control the code with checkers or reviews. The same goes for $ and _ as or in method names. As always, don’t be ideological, but use the best tools for the job. And if the best tool is $ as a method name, then you should use it I think. For people who can’t live with $() or _(), Messages still supports Messages.format().

@Tom: Yes. In projects there should be a i18n guide with i18n guidelines for keys, words, wording, grammar and style. And the guide should explain how i18n is done, so the developers know what $() means. For reading code from other projects (open source) the IDE helps as you said.

Leave a Reply

What people wrote somewhere else:

Additional comments powered by BackType

Guide to CodeMonkeyism

Over the last 4 years I wrote many articles on this blog. To make it easier for you to find the relevant ones, I've organized them into topics.

Top 10

6 reasons why my VC funded startup did fail

Go Ahead: Next Generation Java Programming Style

Java Interview questions: Write a String Reverser

The dark side of NoSQL

7 Bad Signs not to Work for a Software Company or Startup

Is Java dead?

Scala vs. Clojure

Never, never, never use String in Java

No future for functional programming in 2008 – Scala, F# and Nu

Clojure vs Scala, Part 2

Java Developer

Is Java Dead?

Go Ahead: Next Generation Java Programming Style

Be careful with magical code

All variables in Java must be final

Never, never, never use String in Java

Bending Java: More readable code with methods that do nothing?

NoSQL Guy

NoSQL: The Dawn of Polyglot Persistence

The dark side of NoSQL

Essential storage tradeoff: Simple Reads vs. Simple Writes

Sharding destroys the goals of your relational database

The unholy legacy of databases

Startup/CTO

Development Dream Teams

6 reasons why my VC funded startup did fail

American vs. European style of Software Development

12 Things to Reduce Your Lead Time and Time to Market

The high cost of overhead when working in parallel

Essential storage tradeoff: Simple Reads vs. Simple Writes

Job Seeker

Another Good (Java) Interview Question

7 Bad Signs not to Work for a Software Company or Startup

Java Interview questions: Write a String Reverser (and use Recursion!)

Java Interview questions: Multiple Inheritance

As a Manager: What I value in developers

Top 10 Tips (+1) to Get a Pay Raise

Agilist

What Developers Need to Know About Agile

5 Practices Better to Change in Your Scrum Implementation

Scrum is not about engineering practices

ScrumMaster and ZenMaster: The joke of certification

What is Trans-Scrum?