IT, Scala, Java, Web, Ubuntu

Month: June 2009

It was a Gavin King’s book from which I first heard about method chaining since it is not that popular style in Java.

Bauer and King in their book entitled ‘Java Persistence with Hibernate’ (2007), point out that method chaining is convenient in some cases and is more popular in Smalltalk than in Java for Smalltalk, unlike Java, does not have void type. Thus when a method is invoked, it normally returns the object itself in which the method is placed.

Although it is used to improve readability and to reduce the amount of source code, I didn’t really like this style as it can be less readable and might make code difficult to debug. How can the technique to improve readability make the code less readable? By less readable, I mean the code may possibly be unpredictable or make the programmer confused in some cases.

What is this? It looks like instantiating a Storage object yet the end it assigns String value to String variable named name?

It seems confusing. First, instantiate Storage. Then call several methods in it. Finally call the method which returns a String value. However, the Storage class doesn’t even have any method returning the String value. It is the method in the Item class.

OK, move to the other problem that is difficulty in debuging.
The example code above does not have any compile time error yet when it runs the result is

Exception in thread "main" java.lang.NullPointerException
at com.lckymn.kevin.test.methodchaining.Storage.and(Storage.java:26)
at com.lckymn.kevin.test.methodchaining.MethodChainingTest.main(MethodChainingTest.java:10)

The tenth line in the MethodChainingTest.main method is this.

String name = new Storage().put("A").and("B").and(null).and("D").get(0).getName();

The twenty-sixth line in the Storage.and method is this.

items.add(new Item(name.hashCode(), name));

So this line of the code causes NullPointerException but and() method is called three times in the tenth line of the MethodChainingTest class. Which one of these causes the error? In the example, it is very obvious that the and() method call with null parameter is the one. However, in real-life programme, it is usually much more difficult to find.

So, as already mentioned, I did not like using method chaining. Then again there came a time when it was very convenient and useful to use method chaining. I found it very useful when I was making a XML generator programme for an Ajax application. I made it using Java API for XML Processing (JAXP). It required to generate simple XML based on a given object so it might be too much to use XML data binding frameworks and tools such as Java Architecture for XML Binding (JAXB) and XStream. Although both JAXB and XStream are simple and easy to use and can be used to serialise objects, I wanted to have more control than what the framework provides. JAXP with Simple API for XML (SAX) parsing interface was just suitable for what I needed, yet it is not very pleasant to use the SAX interface. It uses ContentHandler interface when making XML contents and the following code is what it looks like when using it.

As all I want was simple XML for an Ajax application, I had to type ‘null‘ many times as parameter values for namespace URI and local name which were definitely unnecessary for my programme. Since the data transfered through network need to be small, XML sent to the front-end Ajax application had better not have the data such as namespace and schema location information and so on.

However, what I all had was, as shown above, the ugly code which repeatedly calls same methods with many ‘null’ parameters. So I tried to find a better way and eventually came up with that it might be a good idea to use method chaining. Even so, there were still the two problems I mentioned.

The method chaining code example that I showed earlier is, in fact, a misuse of method chaining. That can be much better if it is used properly. After all, it is not the technique that makes the code less maintainable but how it is used that makes the code less maintainable.

Think about this. If a knife is used by a murderer, the result of using it would be a dead body while if it is used by a chef, the result would be a delicious meal unless the chef is the murderer. (I think I used this sentence when I had my presentation in the third year in my undergraduate days. The subject was about IT and ethics, and I was emphasising that technologies have nothing to do with ethics yet how we use these is important when it comes to ethics).

So, how can those two problems be solved? First of all, please don’t get me wrong. I am not saying that the way I am going to tell here is the best, but it is just what I do. That’s it.

So my tastes are
1. The prefix ‘Fluent’ shall be used to tell it is a fluent interface.
e.g.)
public interface FluentContentHandler
public interface FluentStorage

2. void return type shall be used to stop method chaining if the method should not be used with other methods in the fluent interface or if it has some side-effect when using with other methods so that the programmer should be noticed it by void return type.
e.g.)
void endDoc()
void finish()

3. Otherwise, all the methods shall return the type of interface itself, and the name of the normal methods which return the type of the interface itself shall begin with verb.
e.g.)
FluentContentHandler openElem(String qName)
FluentContentHandler setText(String qName)
FluentSaxAttributes create(String qName, String value)

OR
The name of the method which must be used before the other methods shall begin with a verb.
e.g.)
FluentSaxAttributes create(String qName, String value)
The name of the method which must be used after the method the name of which begins with a verb shall be an appropriate preposition or conjunction.
e.g.)
FluentSaxAttributes with(String qName, String value)
FluentSaxAttributes and(String qName, String value)

4. If other types than the interface itself need to be returned, distinguishable method names shall be used. The name of the methods which return other type than the type of the interface itself shall begin with a preposition followed by a noun.
e.g.)
List toList()
Map toMap()
Collection toCollection()

I believe, these can help me to get over the first matter, and the code would be more readable as expected.

So let’s have a look at the new fluent interfaces for my XML generator programme.

The programmer using this interface had better be aware of that startDoc() and endDoc() methods are to start and to end the XML document so use it once and therefore these return void type to warn. Similarly, closeElem() method the type of which is void closes the element with the name given as the parameter so the programmer had better stop method chaining here and open a new element with another method chaining.

Since the ContentHandler requires AttributesImpl as attributes toAttributesImpl() method needs to return AttributesImpl and therefore its name consists of the preposition ‘to’ and the noun ‘AttributesImpl’.

Now not only does it solve the problem in debugging but it is also even more readable. My problem solved! 😀
Well, unfortunately there is one more problem.

I am using Eclipse and it has a nice automatic formatting feature. So whenever I press SHIFT+CTRL+F, it automatically formats the code opened in the editor based on the format configuration. This means if I use that function, I lose the coding style of writing each method invocation on a different line as Eclipse formatter puts all the method on a different line together on one line. So do I have to reformat by myself after every automatic formatting? It’s really annoying.

Fortunately, a few changes in formatter configuration can solve this problem.

Click the ‘Window’ menu -> ‘Preferences’

When the ‘Preferences’ menu pops up
-Expand the ‘Java’ -> Expand the ‘Code Style’ -> Select the ‘Fomatter’

When it runs, it displays the following error messages yet now I know that the fourteenth line causes the error.

Exception in thread "main" java.lang.NullPointerException
at com.lckymn.kevin.test.fluentinterface.FluentListStorage.add(FluentListStorage.java:21)
at com.lckymn.kevin.test.fluentinterface.MethodChainingTest.main(MethodChainingTest.java:14)

A critical security vulnerability in Adobe Flash player was discovered about twelve days ago and Adobe has released a new version which has the problem solved. However, there is no 64bit version of Flash with the security update available yet. Besides, Adobe temporarily removed the 64bit Flash download link so you cannot download it now. Since security is concerned, I had removed the 64bit one and installed the 32bit version with npviewer through the synaptic package manager as it is the new version with the security update. Although this 32 bit one is very unstable and crashes many times, I cannot take the risk of having security problem. Those who have installed the 32bit version then have been experiencing the problem that you cannot click any buttons on the Flash player, open the /usr/lib/nspluginwrapper/i386/linux/npviewer file and add the following line just before the last line.

I think there are numerous 64 bit Ubuntu users who are suffering from the crash of Flash Player just like myself. There is a better way to install Flash Player for 64 bit Linux than installing one from the Ubuntu repository and that is what I am about to write. This is obviously not the perfect solution yet I think the best way to use 64bit Flash Player for now. I hope Adobe will release a stable 64 bit Flash Player for Linux soon. Anyway, here we go!

In my case, the latest alpha version of 64 bit Flash player does not work well (e.g. Video on youtube constantly freezes) yet the previous alpha one works better. So first, try the latest one and if it doesn’t work well, download the following one.

*** This part is added on the 28th of September, 2009 ***I installed the latest alpha version which is libflashplayer-10.0.32.18.linux-x86_64.so.tar.gz today (the 28th of September, 2009). So far, it seems fine.

*** Updated on the 2nd of May, 2010libflashplayer-10.0.45.2.linux-x86_64.so.tar.gz works fine.

*** Updated on the 4th of Feb, 2011
flashplayer10_2_p3_64bit_linux_111710.tar.gz works fine.

Before installing it, if there is a previously installed Flash player, it has to be removed first.

Like this:

Unfortunately I don’t really have time to explain the details nor am I sure if Ubuntu supports his/her graphics card so I’m putting here some information which can be a starting point to get what he/she wants.

-Select the ‘Visual Effects’ tab -> Select the ‘Extra’ effect -> Click the ‘Close’ button.
If Ubuntu or Compiz doesn’t support your graphics card, you may get some error message here.
If you are using ATI or nVidia Graphics card, I strongly recommend you to install EnvyNG which automatically checks what Graphics card you use and finds the proper driver for it. It only works for ATI and nVidia ones. I am using Intel one so I cannot use it and therefore can’t explain how to use it. To install it, open the ‘Synaptic Package Manager’
System -> Administration -> Synaptic Package Manager
and search by envyng then you can see ‘envyng-core’, ‘envyng-gtk’ and ‘envyng-qt’. Install envyng-gtk (I assume you’re using Ubuntu but not Kubuntu) and it will install envyng-core and envyng-gtk.

OK, get back to the visual effect one. After selecting the ‘Extra’ visual effect, it should have some visual effects and now it’s time to customise it to have more effects. If you have not installed ‘CompizConfig Setting Manager’ yet, install it first.