Thursday, March 25, 2010

Mutability vs. Immutability and why we care

Do we really care ? We should but we don’t. I mean sometimes people just doesn’t think in terms of mutability or immutability and after the design it becomes headache to change the API (probably because you have already distributed code and now you can’t simply change the stuff !) and it remains there forever.

So, What do you mean by Mutable and Immutable object ?

You can call any object a

Mutable Object -  if state of object can be changed/modified after its creation.

Immutable Object If state of object could not be changed/modified after its creation.

Watch out for: Immutable object  holding a reference of mutable objects.

e.g.  Class Foo is immutable and holding a reference of Class Bar, which is mutable.

bar

Foo

 

What’s the issue with mutability ?

Take it java collection API for instance, you have an Object holding a List of some Objects and you would like to provide a getter method for List, now you can’t just return a List reference by getter method because List implementations are mutable (e.g.  java.util.ArrayList)  and due to this fact anybody can change the List contents, which is not acceptable. Even returning the Iterator could not help as it got remove() method using which anyone could remove the elements. You can use google collections or utility methods from Collections class to return the Immutable versions but it still implements List Interface so you have to check for stray immutable collections before executing the contract (say add() method ) ! This is because all collection types are mutable.

collectionTest

Take another example –  Lets say you have Customer class having reference to CreditCard class, and you have to put getter method for CreditCard (for validations and all ) but CreditCard designed as mutable object. So you can’t simply return the reference of original CreditCard object but you have to make a copy of the CreditCard  object and return the same so even somebody change the CreditCard  your original CreditCard object should not affect. but in this scenario if getter method called hundred times then you end up creating hundred objects (of course useless objects) just because CreditCard was designed mutable.

creditCard 

 customer

There are number of scenarios where people actually required Immutable objects but designed as a mutable and later these objects becomes painful to handle. I have seen people asking why we need immutability and I think we should ask why mutability and does it really what we need, if we have justifiable answers then go with mutability else stick to immutability.

Immutability … what we gain ?

Avoids unnecessary object creation : When we design the immutable object, we are quite sure about its consistency and we can reuse it again wherever required. e.g. String in java is immutable, so java doesn’t need to make new String objects every time (Unless you say so by calling the constructor of String) when program needs it.

Data Consistency: As state of so we are quite sure whatever is object state is same as when object was created.

Thread safety: Immutability comes with thread safety.

Makes code more reliable by avoiding possible inconsistency in object states.

These are some benefits with immutability that matters in day to day programming but there could be various benefits also depending upon the various scenarios, so thumb rule is ask yourself – Do you really need this object mutable ? if you can not justify,stick to immutability.

Wednesday, January 20, 2010

Google collections – Its cooler than I thought !

Google recently launched final version of java collection library based on Java Collection framework called Google collections, So what’s new ? even apache have their collection library version. So why Google developed their own new version when already have one from apache –click here for more information.

Core part of Google collection is “really immutable collection types”. When I said this line to my friend his response was – “That’s ok . but why you need immutable collections ?” same question was asked to Kevin (a Googler – who developed this library) and his response was – “At Google we think in other way – why you need a mutable collection ?” and I agree with this. When I analyze my project then I found more than 90% of scenarios where we don’t require mutable collections at all and if you provide mutable collections where that doesn’t required you could face serious scenarios ( e.g.  anyone could add, remove objects in/from your collection or clear the entire collection altogether ! ).Looks serious issue – so really java doesn’t provide the way to deal with this scenarios ? yes they did  – there are various methods from Collections to deal with such scenarios (Collections.unmodificable …(..) ) but this is just a wrapper around provided collection. So what if you forgot to do that and even you did your collection is mutable itself and you just provided the read only view to others.

Google collections just provided the way to simplify this process and gives you more sophisticated way of doing it, making your collection really “immutable”.Also there are new collection types they introduced called Multiset (Same as Bag from apache collection), Multimap, BiMap etc.

     If you are having problem with comparator then you can try new Ordering class to help you out. Iterators, Iterables class to work with Iterator and Iterable. MapMaker class to build concurrent hash maps  This classes could really save you from pain. But wait there is much more to explore (from Google collections presentation):

Thursday, May 28, 2009

Charts with google !


I was searching for a chart API and came across Google chart api, Google visualization api, JS charts, AXIIS, JFreeChart and many more out there. But personally I like Google chart apiandGoogle visualization api. Both are provided by Google but Google chart api provides static images by hitting a URL with defined parameters but Google visualization api is much more powerful, provides interactive charts where you can click to see data, highlight the part on mouse over and all that stuff. Google visualization api will need a visualization compatible data source to show the data. The process of designing compatible data source for visualization is discussed here. Google visualization api. is also accessible as a part of Google ajax api and we can use a plain javascript to show visualization charts. click here to check out more, also there are lots of options available inAjax playground to play with. Here is an example of visualization chart

With Google chart api its very simple to get charts and most of the cases its all that we required !Google chart api is so simple that just hit a URL and get the image. for example to get the image of pie chart hit the

http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=280x100&chl=Hello|World&chdl=Hello|world

and result is :






if you want plain pie chart then hit

http://chart.apis.google.com/chart?cht=p&chd=t:60,40&chs=280x100&chl=Hello|World&chdl=Hello|world







I have only shown a pie charts but this API provides lots of options such as bar charts, line charts, and much more is available to explore. Also colours , labels, styles can be customised to match the individual page views.

Monday, April 13, 2009

Cover flow effect in javascript.

Recently in our project there is a requirement of image slideshow, so I started working on it. As a plenty of libraries and stand alone Javascripts are available from various resources. I started filtering one by one. JQuery impress me a lot with number of plug-ins available (see examples). EXT-JS is also in the ground with their free library called EXT-CORE and also quite nice. But all these are same effects with a little bit of variations.

So while deciding one of them I started my iTunes and their cover-flow grab my attention, so I started to look for javascript for cover-flow effect. But I haven’t found anything related to JQuery or ext or dojo libraries.

But then I found one site where I got this effect J . A developer - Raith is also looks like inspired by apple’s cover flow effect so he write one javascript that gives cover flow effect to images. You can find cover flow javascript at http://www.classycode.co.uk/coverflow/

Here is screenshots:









GWT OR EXT-GWT

We are using EXT-GWT (GXT - Not an open source project) java library from EXT-JS, a library that let you work on java with GWT (An open source framework from Google) allowing to use EXT-JS components and framework. I'm impressed the way it works also their framework is quite good (I haven't used JavaScript frameworks before to compare ;-) ) Even I hated to work directly JavaScript and that’s why moved to these tools but if you are not like me then you have plenty of options available with you like jquery,ext-core,moo-tools,dojo etc and if you are planning to go in that way then take a look at google ajax-api and google ajax playground - awesome tool.

Please do not confuse between EXT-GWT (GXT) and GWT-EXT both are two different libraries.

But many times I find the question whether should go to plain GWT or GXT?

We also stuck on that point but quite later, so can’t reverse the process. Anyways, but we are happy with the GXT now as our project shapes up. But we had a discussion on many points that I thought would be helpful if your project is in initial stage.

  1. GWT is a good library, but requires a CSS to give a good look and feel. GXT on the other hand comes with pre-written CSS, so you don't have to look much on that area but in certain cases you may require to write CSS for GXT also.
  2. GXT is not an open source and requires a licence in commercial development while GWT is an open source project, so if you can get a good look and feel with GWT by giving a style sheet then think again about GXT (you have to pay for it!).
  3. GXT will give rich internet application view (see examples) but if you like the conventional design then check for GWT (see examples).
  4. Both GWT and GXT have forum support.
  5. GWT and GXT both projects looks promising as GWT recently launched their new version(1.6) and GXT is also in a process to launch their new version(2.0) based on GWT(1.6) currently latest version of GXT is 1.2.3 and it is based on GWT 1.5.3

I think this would be helpful if you are planning with GWT or EXT-GWT.