Monday, November 24, 2008

Singletons

In the previous post I had said that it may not be a good idea to have static attributes in your class. The Singleton design pattern also uses a static attribute to hold the Singleton instance. Even though there are valid uses of Singletons, lately this pattern has come under considerable attack. Here is another page on the Portland Pattern Repository Wiki, that outlines a practical problem someone faced while using Singletons.

Google has an open source tool to detect Singletons. They call it the Google Singleton Detector. They have identified 4 types of Singletons, namely: Singletons, Hingletons, Mingletons, and Fingletons. You can find definitions for all the funny _ingletons on their wiki, but for those of you too lazy to visit the wiki, I will quote them here:

Singleton:
A class for which there should only be one instance in the entire system at any given time. This program detects singletons which enforce their own singularity, which means they keep one static instance of themselves and pass it around through a static getter method.

Hingleton:
Derived from “helper singleton,” a Hingleton is a class which turns another class into a singleton by enforcing that class's singularity. The offending class is the Hingleton, which "hingles" the other class.

Mingleton:
Derived from "method singleton,” a Mingleton is a class which has any static method that returns some state without taking any parameters. Many will consider these edge cases and not want to report on them.

Fingleton:
Derived from "field singleton,” a Fingleton is a class which contains static fields. Many will consider these edge cases and not want to report on them. The idea is to use this tool to identify and remove all the _ingletons from the code.

Here is a very good blog post by Misko Hevery explaining why Singletons should be avoided.

And here is a video by Misko Hevery on Global State and Singletons,

Wednesday, November 05, 2008

Using static attributes in Java

As you will probably know, the 'static' keyword in Java is used to denote fields, methods, and inner classes that do not belong to any instance of a class. They belong to the class itself and can be accessed when the class is loaded. In this post we will focus on static fields and very briefly discuss pitfalls of their usage.

Static fields are used for a variety of reasons:
  • To define constants
  • In the Singleton design pattern
  • As global stores for application wide settings
  • For mapping default objects (like formats) with Strings
  • Several other reasons depending on the requirements
A few days back I was reviewing some code, and I realized that mutable static fields were being used in way too many places. This can make the code very brittle.

When an object has a mutable static field, for all practical purposes this becomes a global variable, and thus has all the pitfalls of global variables. Some object might change the value, and another object that was depending on the old value will behave inconsistently.

An overuse of static fields is a code smell. If you use FindBugs for code analysis, it will catch such instances. FindBugs has several categories of bugs for static fields:
A perfectly valid use of static fields are constants, like the one shown below.


However, there are several uses of static fields that border on making your code unstable. In future posts I will discuss various ways in which static fields are used. I will also discuss potential pitfalls of the usage and how to avoid such situations.

Monday, September 22, 2008

Figure it out yourself

In this blog, Guy Kawasaki writes about 10 (real world) things college students ought to learn. He has raised some very good points that are not taught in school but are extremely important at work.

In point #4 he says:

How to figure out anything on your own. Armed with Google, PDFs of manuals, and self-reliance, force yourself to learn how to figure out just about anything on your own. There are no office hours, no teaching assistants, and study groups in the real world. Actually, the real world is one long, often lonely independent study, so get with it. Here’s a question to test your research prowess. How do you update the calendar in a Motorola Q phone with appointments stored in Now-Up-To-Date? (I’ll send a copy of The Art of the Start to the first person with a good answer.)

This brings to mind ideas that I have been trying to talk about for a while: self learning, informal learning, and continuous learning. I would again like to take this opportunity to remind you of the importance of learning continuously. Sometimes there are things that have to be learned because the need just came up, but most often it helps to have a plan. In my next blog I will write about how to make such a plan... so stay tuned :-)



Notes: This text was originally posted on my earlier blog at http://www.adaptivelearningonline.net
Here are the comments from the original post


-----
COMMENT:
AUTHOR: Sachin More
URL:
DATE: 08/24/2006 07:30:53 AM
I would like to add one more line from the same blog...

The purpose of learning is "not to" prepare for working but to prepare for living(This is what I personally belive in). Working is a part of living, and it requires these kinds of skills no matter what career you pursue.

However, "there is much more to life than work".
Why do we forget this?



COMMENT:

AUTHOR: Tarun Chandel

URL: http://tarunchandel.blogspot.com

DATE: 10/19/2006 10:07:15 AM

I had a chance to meet Guy at IBM Software Universe in Mumbai recently. Guy is very cool person and loves to live life. He is fun to talk to. He is VC by profession and has missed chance to invest in companies like Google, Yahoo. He is a very experienced man and just 5 minutes of talk with him can leave you charged and inspired to follow your dreams.

Coming back to the current post I think life teaches us a lot of lessons and if we keep our eyes open and keep looking for information, there is more than enough information avilable around us. I think the most important ability to have in today's world is to have ability to learn, learnability. Learn new things, make sense of things changing around you, ask right questions and find answers to those questions based of facts.



COMMENT:

AUTHOR: Parag

DATE: 10/19/2006 12:28:22 PM

That's a good point Tarun... make sense of things around you and ask the right questions. The right questions will usually lead to the right answers.

The ability to find connections between seemingly unrelated areas is also very important. it helps in broadening a persons perspective and find ingenuine solutions to problems.

--
Parag

Friday, July 11, 2008

Docking Frames


Docking Frames is an excellent Java docking framework. There are a few other docking frameworks out there also, like FlexDock, MyDoggy, and VLDocking, but from all of them I found DockingFrames to be the best for my requirements.



DockingFrames just works. I found very little inconsistency and bugs in it. Also it uses static variables very judiciously which was important for our use, since we might want to use docking in multiple places in the application, such that each of those do not know of each other. I also found the lead contributor to be very responsive. He was extremely prompt in answering questions and always gave good suggestions.



DockingFrames uses the concept of themes for docking. A theme specifies things like icons, buttons, tabs, etc that appear in the panels that contain the docked components. In the beginning I used the BasicTheme and the FlatTheme. However, BasicTheme uses angular tabs (for selecting components that are docked in tabs), which I liked more than the Flat tabs in FlatTheme. However BasicTheme uses more borders around the docked components than FlatTheme. I wanted a combination of these two themes. Something with angular tabs but fewer borders.



Implementing such a theme is possible by creating custom themes.



In the next few blog posts I will talk about how I created a custom theme in DockingFrames.

Sunday, June 01, 2008

Coming back


It's been a long time since I have blogged here.



I maintained a blog at http://www.adaptivelearningonline.net for almost 2 years. However, it was a hosted blog and I was unable to update it regularly. Consequently I have stopped that blog.



I hope to restart sharing what I learn by posting here again.

Wednesday, May 28, 2008

Locking Screen on EEEPC

The Asus EEEPC does allow locking the desktop using CTRL-ALT-L. However there is a bundled utility kdesktop_lock that allows just that.

To lock the desktop type the following line in a terminal.

kdesktop_lock --forcelock

This will lock the desktop (with the taskbar still being displayed)

Here's how you can add an icon to the EEEPC's SimpleUI, so that you do not have to type in the command every time.

Add this to the /opt/xandros/share/AsusLauncher/simpleui.rc

       icon="iptv_norm.png"
selected_icon="iptv_hi.png">
Lock Desktop