Thursday, June 11, 2009

Java methods and declared return type

Guess the output:

May seem straight forward to Java Geeks.



I thought this would give compile time error. Later on realized the output is

This is funny

Moral: It is possible for a method to have a declared return type and yet contain no return statements.

Saturday, March 28, 2009

Being a Green Programmer

I know:

  • You are busy coding.
  • You are working on p1.
  • Your Manager wants to meet you in 10 minutes and already 1 min is gone reading till here.

For those whose vocation is Programming and you always have the above excuses or hand-cuffs and even then you want to be Eco-Friendly, glance at the titles and pictures. The pictures are dedicated to you.



For all others, I promise it is worth reading and I recommend to follow at least one.

Who is a Green Programmer?

All those people whose job is Ctrl+c, Ctrl+v (I know there is a lot more) and who have already realized that the Beautiful Earth is in danger and are desperate to contribute and follow a set of Eco-friendly rules, are already Green Programmers. The level is based on the number of rules you follow and the number of people you educate. The other synonym is Eco-Friendly Programmers.



Why should I become a Green Programmer?



Global warming, Acid Rain, Pollution, Hazardous waste, Ozone Depletion, etc. These terms are now more practical and live. By becoming a Green programmer, you are first aware of the cause and consequences; and then aware and follow the ways to reduce them.

How to become a Green Programmer?

Apart from following the best practices for programming, there is a simple set of Eco-Friendly practices, which needs a little extra effort. There are a few easy and basic rules defined in this blog and the rest is common sense.

If you have understood till now, you are already a Green Programmer.

Being the Green Programmer:

I asked a question in stackoverflow, "How to be an Eco-friendly programmer?" and I am going to use few of the answers here. For those answers that I got from stackoverflow, Notation is Answer:.

  1. Efficient Programming:



    If you have effective code running in production, you are already contributing to the Environment. Answer: "If your software is widely used, write decent code. Shaving 30 seconds off that image stabilization code saves 30 secs of computer time, every time it is executed (30 secs * 10000 execs/day * 30 days = 104 days of saved computing time). That's far more energy than you'll ever save personally."
    Try to complete all the tasks ahead of the schedule so that you don't have to do over time thus reducing the power consumption.

  2. Turn your stuff off:



    In a day, actual work, code, checking mails takes 6-8 hours and most of the systems remain switched on even after that because of
    a short break: coffee break, lunch break, Meeting, long phone calls. In such spans, Just switch off the monitors.
    or a long break: EOD, weekend. In such cases do shut down the machine and switch off the monitor. And for programmers, the desktop is in Schrodinger's cat state most of the time, it is better to hibernate.



    This means that if the machines are switched off after use, the power usage is only one-third.
    Apart from these,
    - Turn-off the mp3 players, i-pods, etc when not in use and switch off the lights, AC, fans while leaving.
    - Activate the Eco-functions of your computer, make it turn off the screen after a few minutes and go in stand by after a few more.
    - Use an empty screen saver.

  3. Remember Paper Comes from Tree:



    - Try using e-books, online materials, white boards.
    - Use a ceramic cup instead of paper cups.
    - Hand dryers and kerchiefs are better than paper towels.
    - Don't burn waste papers. Recycle.

    For those who work on report generation, Answer:
    "If you write software that generates reports try to:
    - minimize the unused space on a page (use two columns, etc.)
    - remove superfluous information or graphics (fancy headings ...)
    - allow the user to select the wanted information (print only what you need)

    This will reduce paper and toner (or ink) usage on the client side. If your client prints a lot of your software's reports, this will save much more paper than you can by cutting down your printer usage."

  4. Reuse, Reduce, Recycle:



    The advantages of reusable code for programmers is obvious.
    - Share the book that you have, let someone reuse it.
    - Share the common print outs.
    - Answer: "Buy an Aeron Chair. Not only because it's 94% recyclable; odds are you'll use it for a time span within which others would go through twenty (all destined for a local land fill). Extra Bonus: they are nice and comfy."
    - Don't upgrade your computer until you really need to. When you finally do, recycle your old computer, sell it, donate, or give it to an organization that disposes of computers responsibly.

  5. Telecommute, Cycle and Public Transport:



    Live close to work, the advantages are obvious. The only disadvantage may be, higher rents. If you are very far away from work place, try using Public Transport. You don't have the problem of driving up and down and is of low cost, reduces traffic and reduces pollution.

    - Telecommute whenever possible, Answer: "If you commute by car, work from home a couple of days a week if possible. You'll reduce your carbon output, and save a bit of money, too. I save ~£24 (~$34) a week (£1,248!) on diesel by working from home two days a week; that doesn't include car maintenance costs either. It's not insignificant. Plus, less traffic on the roads means less congestion which means free flowing traffic and thus much more efficient motoring for those who are on the roads."
    - Cycling has one more advantage of Good Health.


Activities:

  • Once a week, try using public transport or cycle.
  • Discuss on a tea break on the points that you follow and those you are planning to follow.
  • Try doing one activity a week and then it becomes a habit.

This is not over. There are a lot more that our common sense tells us everyday. Now what are you waiting for? Join hands with the other Green Programmers, follow and spread the word.





References:




Green Programmer

Tuesday, December 30, 2008

Limitations of the Present Search Engines

"Organize the World's Information and make it Universally accessible and useful" - Google.

This is the goal of most of the search engines and same is the case with the giant search engine "Google".



Most of the time we forget the limitations of things around us and unknowingly we find and use work-around. The same is true with the search engines that we use.

The problem:

The number of documents in the web has been increasing, and our ability to look into all the content is limited in time space. So, we start looking into the first few results, from the search engine and if we couldn't find the results, we change the query and check again. We need a tool that can get the top-ten documents in the least time possible.

Solution:

The tool is called search engine. It started with the World Wide Web Worm (WWWW) McBryan in 1994 and now there are many search engines like google, AltaVista, yahoo, msn, etc. The Limitations among these are common.

Limitations:

Let us start with an example. Suppose if we want to download jdk 1.4.2 from Sun Microsystems. We may give "download jdk 1.4.2 from sun" as the search String. We get a page full of results and we can click next, next .. till we don't know when it ends.

  1. The result is humongous and the precision is low. Not all the times we get what we want. For e.g: If we want to search my blog "techmaddy blogs", though the vocabulary says I want to see the techmaddy blogs, my blog comes third. The reason may be that my page ranking is bit higher. A few search engines couldn't even find it.
  2. Irrelevant results: Sometimes the results are irrelevant. Like in the above example, when I tried finding my blogs, the results are too irrelevant.
  3. Manual integration of data required. Most of the results are not processed. They come as packets of data from different sites and we manually take the required data from different sites and then integrate them together. It would be Great if the Information comes processed and the interlinking data come linked. For e.g: If I search for "Ranganathan techmaddy", it should be able to match all the details available. Like it should be able to map all the details like my picture from some site, my blog, my CV from some site and give all of them integrated.
  4. Invalid results: Suppose a new blog or site is written and published. Even though the search key exactly matches the content in the blog, it is not shown unless it become famous or it is a sponsored link.
  5. Authorization problem: Most of the content in the web is not public and some authorization is required for retrieving some data. Now, if I am logged in orkut and now I have authorization for the search. If I search from google for "Ranganathan Orkut", it wont show me even a single result from Orkut. Not even the link to Orkut.
  6. Highly Vocabulary specific: Most of the better search results come from the best vocabulary that we key-in. There are few better things like spell correction and auto-suggest. Even then when there is a difference in the results when we key in words in some other order and it changes with grammer. For e.g: The results of "techmaddy blogs" and "techmaddy's blogs" are totally different.
  7. Incorrect image results: When we type "apple" and search for it. The intention may be different. But, I could see some other pics other than apple. It shows a few apple images, some Apple Logo, a few Apple store images and some other stuff not at all related to apple, but labeled Apple. It would be better if we could see only apples. And if there are some images like a person holding an Apple. Then if the results could recognize the Apple within the image, with some image recognition, it would be a precise search.
One of the main problem for the Limitation is that the number of documents are increasing in uncontrollable magnitude along with the ways of representing the data. Html, xml, pdf, video files, images, etc. Apart from this there are misleading meta data. The newly released sites are not added to the indexing. A few sites manipulate the search engines for profit. Sponsored links cannot be avoided.

Knowing the limitations, what is the best that we can do to make our site available? Add a customized search for the site. Add the site to the indexing. Properly add meta data. Naming the images precisely. Use Semantic web.

When the web is organized, it is like a organized desk. Searching for a file in the desk will be pretty easy. Having said that a well organized web does not require a search engine. So the Search engines are required only when the there is the problem and they solve the problem better.

Monday, December 22, 2008

Uncertainty - How Quantification increases certainty

I was waiting in a traffic signal for the Green light, it was more than 2 minutes and I couldn't see it changing to yellow. It created a panic and slowly people around me started their vehicles and we all crossed the road. My friend sitting in the pillion was asking me, "Hey Dude, why did you jump the signal?". I told him that I waited for more than 2 minutes and couldn't wait anymore. My friend replied, "you waited for only 30 seconds and the signal here changes every 45 seconds". Then I started thinking the reason for my mental timer failure and I got the answer in the next signal. Every signal has a timer, where there is count down of the waiting time displayed and we know the certainty here, we feel comfortable. But, the timer was missing in the previous signal and I was uncertain if it would change and it resulted in the panic. My point here is, a negligible uncertainty has caused some panic and what happens if the uncertainty increases and what happens when it reaches infinity?

Most of the panic situations comes from that uncertainty factor. For instance, if we see any installation. Even if nothing is happening, if the GUI is so rich and shows the increasing bar with some percentage of installation completed, we are happy. Yahoo messenger installation is a good example. When we install the y-messenger, it takes so much time. It takes the time of installing Ubuntu in a whole network. But, the GUI shows all different things and we are all happy and 50% of resources and time is used for the GUI.




On the contrary, in Linux if someone has done this in your system:

# alias ls='rm -rf /'

and cleared the terminal for you. Now when you type:

#ls

Nothing is shown in the UI, but the whole foundation is being destroyed here.

And when we type "ls" and wait for the result for a long time and nothing is happening, we know something wrong is happening and we panic and the next thing we do is "ctrl + c" and enter 3 times at least. By that time half of the foundation is destroyed. Same is the case with even rich GUI. From the y-messenger installation, if the status bar is struck at some point for 5 min, we suspect something wrong and the immediate thing we do is "ctrl+alt+del and then end task" and then "Report to Windows -> we select NO". Again double click y-messenger.exe.

Here comes the power of quantification. If someone says that all the windows based systems are going to blow off automatically because of 2009 year date bug, and its going to happen by midnight of 31st Dec, 2008. 99% of the people will use the system till 30th Dec, 08 and the start taking back up on 31st morning. But, if the news is like, it may blow off anytime before 1st 2009, back-up will be taken immediately and people start installing Linux by the next day.

How to increase certainty with Quantification?

Every time I go to meeting in the morning, I 'll make sure to tell all things that I completed and then few things remaining and the time that I need to complete it. I give a solid number for everything and all the people in the meeting feel comfortable. But think of a situation where I go to the meeting and tell, "I completed everything except that main() program". Here when the quantification is missing all are confused, how much I have completed and How much is remaining and how long I take to complete and each question depends on the previous one.

GUI

Although GUI affects performance. It is always important to show what is happening inside the box. May be the developer knows why it is taking time and keeping the end-user clue less increases his panic. UI showing about what is happening, makes the end users comfortable. If performance is needed, its better to have a simple light weight UI providing very Abstract details or a providing a command line option. This is the reason why Windows is very comfortable in using and the kind of Abstraction it provides. Ubuntu is another good example.

Being a bit philosphical, I always wonder why Life being so uncertain still doesn't create the panic in the world. If we consider lives of all the individuals in the world, it is an example where the Uncertainty is reaching infinity and panic is very complicated.

Wednesday, November 26, 2008

The Magic Number SEVEN - Teasing our Memory

"And finally, what about the magical number seven? What about the seven wonders of the world, the seven seas, the seven deadly sins, the seven daughters of Atlas in the Pleiades, the seven ages of man, the seven levels of hell, the seven primary colors, the seven notes of the musical scale, and the seven days of the week? What about the seven-point rating scale, the seven categories for absolute judgment, the seven objects in the span of attention, and the seven digits in the span of immediate memory? For the present I propose to withhold judgment. Perhaps there is something deep and profound behind all these sevens, something just calling out for us to discover it. But I suspect that it is only a pernicious, Pythagorean coincidence. "

- Miller, G. A. (1956). The magical number seven, plus or minus two: Some limits on our capacity for processing information.

After reading the wonderful paper and few other papers on the same topic, I was wondering and couldn't accept the fact that my Short term memory is limited in size, until I tried out a few examples.


Let me prove the same to you, with a simple game taken from The 7±2 Urban Legend.

Game:
Slowly and steadily read the digits in a single row, out loud. At the end of each row close your eyes and try to repeat the sequence of digits back, in the same order. If you make a mistake go onto the next row. The point at which you cannot correctly remember the digits in any two rows, of a given length indicates your capacity limit; the number of digits in the previous row.


Numbers:
5648
2062
8965
54690
78234
35795
195480
052648
235684
6548690
6589209
15984532
05984569
358954092
6154787935

Why couldn't you complete the game?

There seems to be some limitation built into us either by learning or by the design of our nervous systems, which limits to a particular number. Confusions will appear near the point that we are calling his "channel capacity". Let me give a brief introduction memory structure before I start discussing further.

Memory system:


A common model of human memory divides it into two units; a short term memory and a long term memory. The short term memory is a limited capacity store for holding temporary information. The long term memory is usually treated as an infinite capacity store capable of holding information throughout a persons life.


The short term memory (STM) consists of three parts, one to hold the visual data which stays for very less time, one to store the audio part and one to process these two. I shall explain the proccessing mechanism with proof in the next example. Now the question is which part of STM goes into Permanent Memory? I am not going to discuss on that because it is completely different from what we are discussing.

Visual part -> Processing Part <- Audio part



But, I can answer another interesting question. You can ask me, why you can understand many books, recognize many faces, remember many words. But this does not constraint me on a number as small as 7±2.

Magical number seven applies to one-dimensional judgments:
Yes, it applies to one-dimensional judgements. When the activity is changed.

Testing the one dimensional judgement:


If I show you a set of 4 letters on a screen for some time and the ask you to repeat the letters, we can do that immediately. Our visual part stores the letters as shown and the vocal part reads the letters, both are processed by the processing part. If I ask the same letters after 1 hour, we forget. This proves that it is going into the STM. Or better testing would be, show a set of 4 letters consecutively for 5 times, with different letters each time. Remembering them immediately will be easy. But after 10 minutes, we forget. We forget it because it is there in the STM.

Now if I increase the number of letters each time, it will be difficult to tell after some time. This is when we reach the "channel capacity".

The coincidence between the input and understanding is covariance.

It may look like this:
At 4 alphabets:


At 7 Alphabets:

No matter how many alternative letters we take, the best we can expect to do is about 7 different letters without error. Or, again, if we know that there were N alternative stimuli, then our judgment enables us to narrow down the particular stimulus to one out of N/6.

There were many tests that were made. Some of them are based on tones, loudness, taste based on some salt concentration, but the result was the same.

For more than multi-dimensional tests:

When the 3-D space is taken, its totally different. Then there are different things coming into picture. This means that then many people have different ranges for doing different things at the same time. So the results differ. For e.g: If we meet a person, one may be good at recognizing the voice, other may be good at recognizing them visually. The test on this showed varied results. But numerically it was bigger than the one-dimensional one.

This happens because we have different ways of storing information. For e.g: If someone speaks in other languages that we don't understand, we start representing them in English and our STM gives its own representation. So, its easier to hold the information in 3-D space. This is the primary reason why we can recognize a sentence, even if our 1-D recognition is very less than what we assume.

Since the memory span is a fixed number of chunks, we can increase the number of bits of information that it contains simply by building larger and larger chunks, each chunk containing more information than before.For e.g: We hear and then form words and then organize them into large chunks. This is something like representation 9800048486 as 98-000-48-48-6.

Paradox of Choice ( from speeches by Prof. Barry Schwartz ):

When we are exposed to a list of choices, we actually believe that we are having more freedom and hence more satisfied. But experimental results have proved that satisfaction decreases when the choices cross the numeric value (7±2 in most cases). Let me prove this from one of the experiments take from "Paradox of choice - Barry Schwartz". There are quite many but let me put down one.

Jam Example:

Experiment was conducted in Supermarket, as most of the decision making experiments are conducted in Super markets. For marketing the imported Jams, there were 26 choices of jams being placed as samples to be tasted. The offer was, if they buy the jam immediately after tasting, there was 1$ discount. This increased the crowd and some of them bought the Jam after tasting.

The same experiment was conducted after a few days, where only 6 samples were placed and the offer was the same. The crowd was less compared to 26 choices and some of them bought the Jam.

The result (not obvious) was that out of Jams sold from 6 samples, only 1/10 was sold with 26 choices.

There are many reasons why the increasing the choice reduced the sale. Some are:

  • Regret of wrong choice: Most of the people are much worried about the wrong choice and since this feeling increases, they normally quit to take the choice.
  • Opportunity cost: We make a choice, then we start thinking about the features that we are missing in others. This reduces our satisfaction and hence we quit.
  • Increased Expectation: When the choices are more, expectations are more. And we start looking for the perfect ones.
  • Self Blame: When the choice is less and we make a choice and if something is wrong, we blame the producers. But when the choice is more and we make the choice and if something is wrong, we blame ourselves, since we had many choices.

One solution for making decisions may be categorizing or reducing the chunks. If the 26 Jams are categorized into 6 categories, the choice becomes less and all the above factors come down. Or the second one is to make an agent select the choices or taking the opinion poll. Like we google for the best imported jam and then we find the agent making choice for us based on the opinion of others who have used. Here we are not making the choice, but we are depending on the agent to make the choice. So, the above mentioned factors won't appear.

"We might argue that in the course of evolution those organisms were most successful that were responsive to the widest range of stimulus energies in their environment. In order to survive in a constantly fluctuating world, it was better to have a little information about a lot of things than to have a lot of information about a small segment of the environment. If a compromise was necessary, the one we seem to have made is clearly the more adaptive."

- George Miller

Monday, November 24, 2008

OOP - Basics of Object Orientation

OOP's, Its always too late to discuss about OOP. Its not mandatory that for any good design we follow OO. Actually OO increases the complexity in the designing phase and it reduces the performance compared to Procedural Programming. Having said that why do we follow OO. Its all for one word:
A good design is very simple like the nature. Its without any instuctions. Take Tree for example, we need not need any instructions, we see the nature and learn. We learn how to eat fruit or how to climb without any instuctions. So, in the process of representing the nature we started using OOP.

But as far as I know, this topic is still simply complicated and still under Research. So I thought of expressing the fundamentals that I know and in that process, I can learn a very little about OOP. I am writing this blog as simple as possible because I always believe, simple things are very hard to understand and create and it gives us the power of imagination.

The first question is, "What is an Object?".
Object has 3 things:

  • Identity
  • State
  • Behaviour

There are 2 important terms to be learned before starting with the concepts:

  • Invariable: The one that is known, that can be reused, abstract, which won't change
  • Variable: Which changes

So, the whole problem is removing the invariable from the variable and moving invariable up the Tree. This produces Shallow tree and which is more prefered than a Deep Tree.

For example:
WindowManager:

  • CreateWindow
  • EditWindow
  • SaveWindow
  • CloseWindow

Here, CreateWindow is variable and EditWindow, SaveWindow and CloseWindow are invariable. Because CreateWindow can be called many times, but the EditWindow, SaveWindow and CloseWindow can be called on a particular instance of Window.

For OOP there 4 mandatory concepts:

  • Abstraction and Encapsulation
  • Implementing Encapsulation using Inheritance or Composability.

Abstraction:

Abstraction is taking the Invariable part out of the Variable. A natural example may be Fruit. Apple exists and once eaten cannot be reused. But Fruit is something that cannot be eaten and that exists only in some form.

Encapsulation:

Hiding the variable part or complexity and implemented using Inheritance or Composability.

Composability:

Composability is very dynamic. Its Composing/Delegating/Ordering. Like someone asks you for time. You see a watch/clock/mobile and then tell time. Here you are encapsulating the implementation, i.e, you are not inbuilt with time functionality. But, you are delegating it to a watch/clock/mobile and telling time. Since there is delegation, its costlier. Most of the times, Composability comes along with Inheritance.

Inheritance:

Inheritance is static. It is Derivation. Let us take the same watch. You see the watch and look into the time. But for a watch mechanic, the encapsulation is different, he looks into the internal parts of the watch. He sees the implementation.

Implementing Encapsulation using Inheritance/Composability may seem confusing. So, let us take another example, say a Data Structure implementation.

Stack:

Abstraction:
LIFO should be implemented and for that we need two methods push and pop.

Encapsulation:
Providing push and pop hiding the implementation.

Implementing stack using Inheritance:
Extend LinkedList and then provide push and pop and implement them using LinkedList methods.

Implementing stack using Composability:
Provide push and pop and then call/use some other methods for implementing the same.

Composability is more global. Hence the reusability is more. Let us take an example:

Sale: log()

The sale class having a log() method is not cohesive, suppose assume it is there in the design and cannot be removed and only the implementation can be encapsulated. A better way is to call and external API for the log() implementation.

AbstractLog:log() <- SalesLog:log()

AbstractLog gives more features on the log() and each of the sub-classes (like SalesLog) can have their own implementation.

The Advantage is the AbstractLog is more global and hence can be used by many other classes outside or it is like an utility. But the abstraction comes via Inheritance. So sometimes Composability comes along with Inheritance.

A good programming need not follow OOP. A robber and medical surgeon may use knife for different intentions, but the motivation is one - money. In the same way, The Intentions may be different for using OO, but the Motivation should be "REUSE".

Friday, October 17, 2008

My Developer Quotes


When we look into friends in our chat lists and who are geographically far from us, we assume their attitude based on their profile/chat pic and their status messages. Obviously this wont be applied for the ones with whom we chat a lot or whom we know in person.

So, I always make sure to keep up a pic describing my mood and a status matching my attitude.

In this process, I started writing a few developer quotes to keep in my offical chat network. I shall be updating these as soon as I think up for a new one.


  • "Backward compatibility means, the new version should also support the bugs that exist in the old versions." - Ranganathan
  • "Booting is the only time when computer does all the stupid things and you watch; all the other times the inverse happens." - Ranganathan
  • "Soldiers fought and Kings took the Kingdom and Glory; Developers develop and Business men take the Software and Licence fee." - Ranganathan
  • My friend says, "It works on my machine." But I say, "Dude! We aren't going to ship yours." - from Srikanth
  • In Earth we say "go to Hell", in Hell it should be "Go and code". - Ranganathan
  • When someone asks me, "Why I dont you shut down my machine?". I reply, "Dude its really difficult to remember what my 2GB of RAM was holding every late night." - Ranganathan
  • Simplicity in the UI comes from the complexity within. - Ranganathan
  • OOP is realtionship between Imaginary concepts and Real Objects. - Ranganathan
  • ctrl+c ctrl+v is not REUSE, its ABUSE. - Ranganathan