http://www.springsource.com/newsevents/vmware-acquire-springsource?__utma=1.989943887005353600.1229312626.1248839927.1249962234.28&__utmb=1.1.10.1249962234&__utmc=1&__utmx=-&__utmz=1.1248780117.26.15.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=spring%20webflow&__utmv=-&__utmk=245757188
‘The Null’ Nuisance
3 February, 2009While working on enahncements on a project already in production, I had a very interesting conversation. Let me give a brief background – the core architecture is all in place and we need to build in new functionality. Of course, refactoring is being done along the road. In a specific scenario, I got into a conversation with a fellow architect on usage of “nulls” and “null checks”. The theme of the conversation was “Should a method return a null or an initialized instance of the class”. Let me take an example:
There is a service method that connects to a database loading records for all users in the system. In the DAO we are loading the recordset from the database and converting to an ArrayList of DTO (ValueObject). A sample code to map the a DTO generally is:
List<User> users = null;
for(int index = 0; index < recordSet.size(); index++)
{
User user = new User();
user.setFirstName(recordSet.getString(“firstName”);
user.setMiddleName(recordSet.getString(“middleName”);
user.setLastName(recordSet.getString(“lastName”); user.add(user);
}
return users;
I had an objection to this style of coding. The simple reason being, on the front-end, I had to put a check for null which was un-necessary. Hence, the other classes that were consuming the results had to write the following code:
List<Users> users = loadAll();
if(users != null)
{
/// do something
}
else
{
if(users.get(index).getMidleName() != null)
{
// show the middle name
}
else
{
// do not show the middle name
}
}
Now, consider a scenario with complex objects having lists all down the hierarchy. It means that before we access a property, we will have to provide a null check. Soon, this “do nothing” null check will become a headache. Someone has coded a null propogation somewhere and we can not trace it. We feel the easiest way is to put in a null check. In my given example, I would have my JSP strewen with null checks cluttering my code.
Unfortunately, this will not solve the real problem. A simple solution is to identify the code where a null reference can be introduced and handle it there. The rest will be happy about it.
More importantly, et us pause for a minute and ask ourselves – Is there something that the application can do, with an object refering to nothing? Let us go back to my example and see how is the application going to use the user list. We need the list of the users to display a report for the users listed. If no users are returned, the uer should see “No users exist”. The UI is no sure, what represents users – a null object or an initialized object with 0 size or an exception. This will mean that the developer consuming the method will have to write these multple conditions for a simple check.
We can do oe of the following:
1. Throwing a business exception that voilates a business logic can be an effective strategy. However, it largely depends on how do you use exceptions in applications. Remember, raising an exception is an expensive operation.
2. Alternatively, you can provide an Empty implementation of the object that can do something useful like logging an info or an error to the log system.
I am not a hugh fan of throwing an Exception, and also because it is expensive, I am exploring the second option. This changes my code to:
List<User> users = null;
for(int index = 0; index < recordSet.size(); index++)
{
User user = new User();
user.setFirstName(recordSet.getString(“firstName”));
if(recordSet.getString(“middleName”) == null) // You can also use StringUtils from apache.lang
{
user.setMiddleName(“”);
}
else
{
user.setMiddleName(recordSet.getString(“middleName”));
}
user.setLastName(recordSet.getString(“lastName”)); user.add(user);
}
if(users == null)
{
// throw new business exception
}
// else we return an initialized list.
return new ArrayList<User>();
This will change the UI code to:
List<Users> users = loadAll();
// code to show the middle name – if it does not exist, it will show up as blank.
The most evident benefits is – “No more if statements for null checks on the UI. Check is being pushed down in the call hierarchy. Hence, multiple methods calling the same method will not have to worry about nulls.”
The most important question is “Is this approach safe?” Nothing ever is. There is no reason for someone to code incorrectly. Of course, we can not on external libraries never to return null references, but when you write your own code, following this approach can lead to a less cluttered application and a better control over source code.
Remember: The approach is not always necessary, just ensure that the null reference should not be catastrophic.
ScribeFire
3 February, 2009I am facing issues using Firefox with wordpress lately and decided to check out alternatives. Firefox’s plugin – ScribeFire seems interesting. Lets see what it has to offer.
How to write unmaintainable code :: Humour
3 February, 2009Read this famous article here: http://www.freevbcode.com/ShowCode.Asp?ID=2547
2009
9 January, 2009A very happy new year to all of you. Thanks for making Scratch Pad a success in 2008. Last year when I started this blog i could not have thought of bringing it to where it is today. For sometime, I have been thinking where do I want to take this blog too. If you think of it, it is not different then where I want to focus on technology.
1. Utilities Java Application Framework (code named ujaf)- Across projects, there are many things that I have been using over and over again. As these Java files are created in a project they carry along with them their package names (specific to projects). This utility aims to bring all such utilities in an open source project under sourceforge.net umbrella
2. Flex controls – I started to write custom controls when i started this blog. For last six months I have working away from Flex. On Jan 10, I am kicking off a new project in Flex (I am very excited). With this, any custom controls I need i will make them as a library and make them available as an open source library
3. Spring – This year will be find me focus a lot on Spring technology stack and use the various projects in my utilities.
4. Portal – I have been working on Portal technologies for last six months. This year will see me get into portal technology and do an integration of the same with RIA.
5. Converting my blog – By now this would have been done. Seems like, this has to wait for some time. By mid of Q2, 2009 this should be through. I hope you like the change.
There may be more things, I am unsure as of now. I have been asking you, if there are things on your mind share with me; it could of same interest.
God bless and have a great 2009.
We are still here
9 January, 2009As much as I would like to move over, I have to be here still. I ran into roadblocks with hosting and seems like this will take some time. Until then, lets continue …!
Bye Bye
7 January, 2009This is going to be my penultimate entry. Not that I plan to stop blogging. I am moving to a hosted solution. The current setup is leading me to compromises and I feel that I am unable to bring the quality out of my posts like posting PDFs for code etc.
This blog will go down during this week and come back in its new avatar. Catch you all there.
Managing Code
12 December, 2008Do you write code locally for personal use/reference? If the answer is yes, then this post is for you. For others, it may be worth still to read it.
I have been working on a set of projects for last few weeks. Today, I wanted to make some major and possibly destructive changes to the code base. Before I started to change code, I took a backup of the local folder into another folder so that I have a revert point. At this point it hit me – I am missing version control, that I enjoy in my corporate world.
I dropped everything and started to setup my laptop with the version control. I did the following:
1. Downloaded SmartSVN or TortoiseSVN as subversion client and install on your machine.’
2. Download Subversion client for windows from http://subversion.tigris.org/servlets/ProjectDocumentList?folderID=8100
3. Downoad Apache HTTP server to front-end the subversion server from http://subversion.tigris.org/servlets/ProjectDocumentList?folderID=91
4. Install svn client and apache. Steps are simple and can be found with the downloads.
5. Configure Apache wotk with SVN. Read the post http://svnbook.red-bean.com/en/1.0/ch06s04.html
6. Start Apache and hit http://localhost:80/svn/. Provide a different port number in case you changed the apache configurations.
7. Create a local repository on your hard disk by using the command line “svnadmin create foo”. This will create a repository in the same folder from where you ran the command.
8. Open a browser and type http://localhost:80/svn/foo. This will show a repository view to you.
Whola, you have your code base in repository. Enjoy it!!
Moonlight – Silverlight for Linux
21 May, 2008Miguel de Icaza published on his blog the first public release of Moonlight, which will support Silverlight 1.0 on Linux.
Moonlight is not a 1.0 release, it is a source code release for interested developers and contributors…
You can have what you want !!
23 April, 2008I started this endeavor to share my information with you all, so that some of you do not get stuck with the kind of things that I was stuck with. But, seems like I have millions to write about. So, I have decided to let you choose what do I blog about. Game plan is very simple, I will write in some of the topics that I want to write about and you can vote for them, or ask for new stuff. As I get the time (which is mostly a few hours on a weekend), I will pick up the ones with the most vote and start writing about it.
Visit the Vote section on my blog.
Posted by Kapil Viren Ahuja
Posted by Kapil Viren Ahuja
Posted by Kapil Viren Ahuja