Benefits of MessageFormat in Java

In a specific Java class for the Struts2 web application, I have this line of code:

try { user = findByUsername(username); } catch (NoResultException e) { throw new UsernameNotFoundException("Username '" + username + "' not found!"); } 

My teacher wants me to change the throw statement something like this:

 static final String ex = "Username '{0}' not found!" ; // ... throw new UsernameNotFoundException(MessageFormat.format(ex, new Object[] {username})); 

But I see no reason to use MessageFormat in this situation. What makes this better than just concatenating strings? As the JDK API for MessageFormat says:

MessageFormat provides tools for creating concatenated messages in a neutral language. Use this to create messages displayed to end users.

I doubt that end users would see this exception, because it would only be displayed by application logs, and I have a user error page for a web application.

Should I change the line of code or stick to the current one?

+4
source share
6 answers

Should I change the line of code or stick to the current one?

According to your teacher, yours must.

Perhaps he wants you to study different approaches to the same thing.

So far, in the example you provided, this does not make much sense, it would be useful to use other types of messages or for i18n

Think about it:

 String message = ResourceBundle.getBundle("messages").getString("user.notfound"); throw new UsernameNotFoundException(MessageFormat.format( message , new Object[] {username})); 

You may have messages_en.properties and messages_es.properties file

The first with the line:

 user.notfound=Username '{0}' not found! 

And the second one with:

 user.notfound=¡Usuario '{0}' no encontrado! 

Then that would make sense.

Other uses of MessageFormat are described in the doc.

  MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0}."); double[] filelimits = {0,1,2}; String[] filepart = {"no files","one file","{0,number} files"}; ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart); form.setFormatByArgumentIndex(0, fileform); int fileCount = 1273; String diskName = "MyDisk"; Object[] testArgs = {new Long(fileCount), diskName}; System.out.println(form.format(testArgs)); 

Output with different values ​​for fileCount:

  The disk "MyDisk" contains no files. The disk "MyDisk" contains one file. The disk "MyDisk" contains 1,273 files. 

So perhaps your teacher lets you know about the opportunities that you have.

+15
source

Teachers' method allows for easier localization, since you can extract a single line, rather than a few small bits.

+1
source

But I see no reason to use MessageFormat in this situation

In this particular situation, he does not buy you much. In general, using MessageFormat allows you to screen these messages in a file. This allows:

  • localize messages by language
  • edit posts outside source code change
+1
source

Personally, I would follow the path of concatenation, but this is only a matter of preference. Some people find it cleaner to write a string with variables as a single string, and then pass parameters as a list after the string. The more variables you have in the line, the more meaning MessageFormat is used, but you only have one, so this is not a big difference.

0
source

One of the advantages that I see when using MessageFormat is that when you decide to preempt your lines, it would be a lot easier to create a message, and it also makes sense to see “Username” '{0}' not found! "in your resource file, since one line has access to only one identifier.

0
source

Of course, if you do not need internationalization, this is an overhead, but mostly code, because the teacher wants him to be more "internationalized" (although in fact he is not internationalized, since the string is still hardcoded).

Since this is a learning situation, although he can do it simply to show you how to use these classes, and not as the best programming method for this specific example.

From the point of view of the best way of programming, if internationalization is a requirement, then you need to code it, if not, then not. I just add overhead and time (to write code) for no reason.

Evaluate the other answers, the importance of MessageFormat for internationalization is not only that simplifying the creation of an external file. In other languages, the location of the parameter may differ in the structure of the message sentence, so using MessageFormat allows you to change this to a language, which will not be associated with string concatenation.

0
source

All Articles