Saturday, March 27, 2010

Exception Handling

Exception handling is one of the good features of Java and C#, which is highly abused. The following are the questions that one should ask, while working with exceptions.

If you are catching an exception, and throwing it without taking any action, why do you need to catch an exception in the first place?

If you are opening a resource, and if you must close the resource once it is done, how do you make sure that, the resources are always closed?

The following is the best way to handle this.

open connection
try {
Use connection
} finally {
close connection

Let's suppose, if you have opened two resources, then many people try to do like below.

Declare Resource1
Declare Resource2
try {
Open Resource1
Open Resource2
Use both the resources
} finally {
if(Resource1 != null)
Close Resource1;
if(Resource2 != null)
Close Resource2;

There is a problem in the above code. If closing of Resource1 throws any exception, then Resource2 will not be closed. The better way to do this is,

Open Resource1
try {
Open Resource2
try {
Use both the resources
} finally {
Close Resource2
} finally {
Close Resource1;

If you have multiple resources, then the nesting level will increase. But, that is not a problem. Having single try finally block gives problems in case of failures. For any general database query, there would be three try finally blocks (For Connection, Statement and ResultSet). It is always a best practice to have try finally blocks for each resource you are opening.

If you are developing a library, which can be used by many others.

If you are getting an unexpected exception from another library, why do you want to catch it? Why not throw it without catching?

For example, you are developing a library to return some data from the database. Let's suppose, you are trying to open a database connection from a configuration file, and it throws an exception. Why do you want to handle it? Why not throw it to the upper layer, and let it handle. If you are planning to catch it, log it, and rethrow it, are you sure that, it is fine with all the developers who use that library. If all the components are logging essentially the same error, won't the logging become cluttered. Few developers who use that library, may not want any logging from that library. If you are planning to catch it, and return some error code, don't you think significant information is lost while doing that? Can you come up with all the error cases while connecting to a database? It is possible that, the error is because of not having the configuration file, having the invalid database name, database server, user name, password, or not having access to the db, or it can be anything. Why do you want to capture all those in your code? Let the upper layer decide on what to do. Let the exception go to the upper layer.

If you just have try finally block, and without any catch block, people tend to ask, why are you not catching any exception in the try finally block. Ask the above questions, and see whether you really need to catch an exception or not. If you are just catching the exception and rethrowing it, it is of no use.

If you are developing UI

Are you handling all the exceptions? It is not the case that, you should have catch(Exception) in all the UI pages. If something unexpected happens, is there anyway of knowing what is the exact error and stacktrace. It does not matter, whether you display the error in the page itself, or in a log file or event viewer. If you don't have any way of knowing, how would you debug it? You may not always have access to the production machine, and you may not be able to attach a debugger in all the cases.

Wednesday, March 24, 2010

Automation of Testcases is Must

The following are the general problems raised by many in the Software Industry.

  • The developers are not spending enough time in testing.
  • The code quality of the developers is too low, and there are many regressions.
  • We are not getting enough testers. Everyone is opting only for the development.
  • The test team is always overloaded.
  • Eventhough the developers are ready to change the architecture, the test team is not ready to take the change.
  • Test team is not ready to take even small bug fixes, eventhough the development team is ready to fix.
  • We got few issues in production, but the issues are not reproducible in debug build.

The single solution for all the above is, automation of all the testcases. Any other solution is not an optimum solution for this. Automating all the testcases takes extra time, but, it is definitely worth it.

Once the developer is done with the functionality, the developer must write all the automated testcases with respect to the functionality, including the scenarios that are impacted by this feature. The automated testcases can be reviewed by a tester or any other person to make sure whether the developer covered all the scenarios or not. The goal should be, zero manual testcases to execute. If the scenario is too expensive to automate and not very frequent, then probably manual testcase is fine. But, the number of automated testcases should be minimum 99%.

For any developer, for each small feature/bug fix that he/she does, apart from the testing of their feature/bug, if they have to spend more time in doing manual testing for the existing functionality, they will get bored. Eventually, they may not spend full effort in testing existing functionality. That may lead to regressions, and code quality may go down. The developer would be blamed for the regressions, and if this continues, then the project would be forced to have a separate test team which tests everything before releasing. But, if there are automated testcases, before checkin, the developer can run the entire suite of testcases and make sure that there are no regressions.

If the developer is working on a very big functionality, the entire development would be in different phases. Initially, the developer writes for main line scenarios, then tests those, and implements for the other scenarios, tests, and finally refactors the code, and again tests those. While doing these, in the last step, if the developer misses any testcase, and if there is a bug in that, he would be blamed for that. Instead, if the developer writes the automated testcases, then after that, there will not be any need in testing further.

If anyone has good programming skills, then they will not be interested in doing manual testing. But, they may prefer to automate the testcases. If a company does not get enough testers, they should understand why they are not getting testers, and fix the actual issue. If they force all the testers to do manual testing citing reasons that they don't have enough time, then they will not get enough testers to test those. If there are not enough testers, then obviously, testers would be overloaded.

If a developer fixes some bug or changes some architecture, and if it is easy to verify that the existing functionality is not broken, then that can be taken at any point in the release. If there are no automated testcases, most of the testers would not prefer to take those at the end of the release.

Some times, testers test with debug build, and do not execute the same testcases on the release build. Finally, in the production they get issue with the release build. Eventhough, most of the time, the code reviews (if at all the development team has) take care of this, in case of issues like timing etc., it may not be very obvious.

All the above problems would be easily solved by automation of all the testcases. All the project leads tell that it is very expensive to do that. Still it is worth it.

Sunday, March 21, 2010

Don't tell 1000 times

If you want to give feedback to anyone, tell him/her once or twice. You can give one or two examples on that for better clarity. If that person asks for more specific information, then you can give all the details that he/she has asked. For any sane person, telling 2-3 times is enough. If that person does not listen, then you can tell the same after few months. If that person does not listen even after that, then you can proceed as if he/she is never going to listen.

But, without asking if you are giving one example everyday for a month, or 20 examples in one day, you are just breaking the relationship. Never ever think that, it would be good for that person by providing more feedback. It just breaks everything.

Thursday, March 18, 2010

Video Gadget For Blogger and IGoogle

For a long time, I have been thinking of how to promote Loksatta Videos on Loksatta News. I developed a google gadget by which the videos can be shown in a small space. This is useful for sites like LoksattaVideos, which have only videos in all the posts. This gadget displays the latest videos from that site, and the videos can be watched from the gadget itself.

To add this gadget, open and goto Layout -> Add Gadget -> Add your own, and enter the gadget url In the preferences, the URL and feed has to be entered. There are other options to change the width, height and no.of videos to be displayed etc. Once the gadget is added, the latest videos can be watched without going to the actual site.

There is one problem with google gadget. Google always refers to the original URL. So, if the original gadget in the given URL is changed, then all the blogs that use this gadget would be effected. That's why I have to keep different URLs for different versions

Gadget V1.0 :

Latest Gadget (V1.0) : (Not Recommended, since it may change).

Monday, March 15, 2010

If Hitler Had Won the War

During the second world war, Subhash Chandra Bose asked Hitler for the help. In that war, Hitler had lost. Had he won the war, and if he had made Subhash Chandra Bose as the leader of India, what would have happened? Would we have lost Pakistan? What would have been the positions of Gandhi and Nehru?

The bitter truth is, the world respects only those who wins.

Taken from All in one by Omkar

Friday, March 12, 2010

Nadendla Bhaskara Rao - 1984 - till now

N.T.Rama Rao and Nadendla Bhaskara Rao went to court that, Telugu Desam Party belongs to them. The judge Devaiah gave judgement in favor of NTR. After few days, Devaiah resigned and joined Telugu Desam Party and became MLA, and then he later on became Finance Minister in the government of TDP.

After the death of Indira Gandhi, Nadendla and his supporting MLAs went to Delhi. After the last rites, Nadendla wanted to join Congress party with all his supporters in the presence of Rajiv Gandhi. But, his supporters were not interested. So, he did not join. NTR dissolved the government, and elections were conducted. For the elections, Rajiv Gandhi wanted to give few seats to Nadendla Bhaskara Rao. But, P.V.Narasimha Rao did not allow that. Then, Nadendla started, Prajaswamya Telugu Desam Party (Democratic Telugu Desam Party).

In 1989, by the call from Rajiv Gandhi, Nadendla merged Prajaswamya Telugu Desam Party in Congress. Rajiv Gandhi asked him to contest from Vemuru constituency. But, P.C.C. President Marri Chenna Reddy gave B-Form for Tenali constituency and asked him to contest from there. The reason was, if Nadendla contests from Vemuru, he would win, otherwise, he would lose. They wanted Nadendla to lose in the elections, so, they gave the ticket for Tenali. But, Nadendla won in that election from Tenali with a good majority.

After Chenna Reddy became Chief Minister second time, Rajiv Gandhi asked him to take Nadendla in the Cabinet. But, Chenna Reddy was not interested in that. Even Nadendla was also not interested in joining the Cabinet. After seeing the politics in the state, Nadendla lost interest in the politics. Rajiv Gandhi made him as the in-charge of the Karnataka state party.

Nadendla has been supporting Nehru's family since the beginning. When Seetha Ram Kesari was the president of Congress, he told that, the party president post should be given to Sonia Gandhi. He also said, someone from Nehru's family should take reins.

In 1998 elections, Congress party asked him to contest from Vijayawada Parliament constituency. But, Nadendla was not interested in that, because Vijayawada is a very developed city, and it is not that much easy to contest, and he asked for Tenali constituency. But, they gave Khammam constituency. One young leader who expected Khammam seat has opposed him from the beginning. Still, Nadendla won in that elections.

In 1999 elections, Nadendla thought, all the sitting M.P.s would be given the same seats. But, Nadendla was given Secunderabad, and he lost in that elections.

Tuesday, March 09, 2010

Reservations - When Will It Stop?

Whenever I talk about abolishing the reservations, the immediate reply that I get from many is, "Don't think just from your perspective. See the situation of the poor people in those communities. They don't have anything, and they desperately need something for them to improve. That's why we need reservations for some more time."

From the definitions of many intellectuals, that is nothing but, those people are begging, and this person is trying to donate.

If they want to really improve them, then they should solve the root problem. If there are not enough schools/colleges, then they should increase the no.of schools and colleges so that everyone can study. In jobs, if they cannot get it, because of lack of skills, then those people should be trained in those skills to compete with everyone. Without training in the skills, if they are taken into job, then only the organization will suffer. There will be only pseudo-improvement in those communities, and no real improvement.

My next question to them is, why don't they introduce reservations for doctors who treat politicians? Their immediate reply is, don't go to that extreme.

Why cannot we go that extreme? If a person is in a prominent role, he/she can control lives of many people. In that case, that person is more important than a doctor. 545 M.P.s can control lives of 100 crore people. Even a small reduction in the quality of the M.P. can easily effect crores of people. That is not at all acceptable for anyone who wants to improve their life.

The president (Pratibha Patil), The head of the alliance that formed the government (Sonia Gandhi), The head of the most important house of India (Meera Kumar), the head of the chief opposition party of the most important house of India (Sushma Swaraj) are women. They reached that position without any reservation. Still, all of them are involved in bringing the reservations for women.

The elections already have too many problems in India. People vote for money and Liquor. Political parties do rigging. There is a problem with splitting of votes (Let's suppose one good candidate and one bad candidate contests, and good candidate wins. If another good candidate contests there, he/she would increase the winning chances of the bad candidate. The more no.of good candidates contests, the more is the winning chance for the bad candidate). The no.of seats in the Assembly/Parliament is not proportional to the supporters of the party. If a party gets 2% of votes, they may not have 2% representation. But, a party which has 20% of votes may have 50% of representation. When we are already having these many problems, introducing yet another restriction causes a significant trouble for the country.

It is very unfortunate that the Women's Reservation Bill has been accepted in Rajya Sabha. Today is a very sad day.

Saturday, March 06, 2010

Nadendla Bhaskara Rao - 1984 - As C.M.

Nadendla Bhaskara Rao has done the following when he was the Chief Minister of A.P. for one month.

  • Increased the retirement age from 55 to 58. (Marri Chennareddy increased retirement age from 55 to 58, and NTR decreased it from 58 to 55.)

  • NTR took Rs.30 crore from Tirumala Tirupathi Devasthanam (TTD) and credited to the state government. Nadendla returned that money to TTD.

  • Decreased the price of milk by 30 paise per liter.

  • Green ration card holders used to get 5kgs of rice per head, and Nadendla changed it to 25kgs per family.

  • Increased the grants to hostel students from Rs.125 to Rs.175.

  • Started two orphanage homes in every district.

  • Cancelled Dharma Maha Matra, since Lokayukta had the same power and responsibilities.

  • Granted Rs.10 crore for the development of the old city in Hyderabad.

  • Introduced 3 grace marks for those who failed by few marks.

  • Increased the engineering seats to 240, and reduced the fees from Rs.5000 to Rs.3000.

  • Introduced 20% management quota in private engineering colleges, and cancelled the recurring grant to those colleges and saved Rs.7 crore to the government.

  • Started Engineering and Medical colleges for minorities.

  • Cancelled slab system in cinema theatres.

  • Returned sales tax on sugar cane to farmers.

  • In 1984, there was drought, and to save the farmers, waived off the current charges for 5 H.P. Motors.

  • Postponed the recovery of I.M.S.Loans due to drought.

  • Decreased the tax on tourist buses from Rs.250 to Rs.150 per seat. (The tax was increased by NTR.)

  • Reintroduced the pension scheme for MLAs and MLCs. (It was cancelled by NTR.)

  • Restarted all the academies for fine arts. (It was cancelled by NTR.)

  • Accepted the report by Muralidhara Rao commission on backward classes.

  • Every year from Jan 1st to Feb 10th, there would be an exhibition in Hyderabad. The lease agreement was expired in 1983 and NTR did not extend it. Nadendla extended it for few more years.

  • Increased the ex-gratia for R.T.C. Employees from Rs.750 to Rs.1000.

  • Permission granted for many B.Ed. colleges.

  • Congress government gave land for constructing house to all the MLAs for less price. When Telugu Desam Party formed the government, NTR was not interested in giving land to MLAs of TDP. Nadendla gave land to all the MLAs.

  • Cancelled Detention system in Osmania University. (Only Osmania University had this system and no other University had this).

Taken from the book Naa Jeevitha Prasthanam (My Life Journey) by Nadendla Bhaskara Rao.

Wednesday, March 03, 2010

Statistics and Ranking - Sky is the Limit for Interpretation

Every software company in the world would be in the top 10 of something or other criteria. (Don't take it literally, take it in the spirit.).

The criteria can be any of the following, and for every criteria, there would be some survey organization, news channel, or a magazine which publishes the top 10 for that, and the companies in that list will publish that as the big achievement for them.

  • Revenue
  • Profit
  • Profit Margin
  • Revenue Per Employee
  • Profit Per Employee
  • Absolute Growth in Revenue
  • Absolute Growth in Profit
  • Percentage Growth in Revenue
  • Precentage Growth in Profit
  • Increase in Profit Margin
  • No.of Employees
  • Absolute Increase in No.of Employees
  • Percentage Increase in No.of Employees
  • Cash Reserve
  • Cash Reserve Proportion to Revenue
  • Cash Reserve Proportion to Profit
  • Cash Reserve per Employee
  • Diversity in the no.of businesses
  • Diversity in the Employees (Male/Female Ratio)
  • Projected Growth
  • Most Innovative Idea
  • Most Promising Idea
  • Best Employer
  • Top Paying Company
  • Top Recruiter

If we come to politics, there is nothing better than the statistics of Telangana. I don't want to go deep into that. But, I am mentioning what prompted me to write this post. Few days back, Dr.Jayaprakash Narayan participated in a chat session on IBN Live. In that, one person asked on what factors, Telangana is growing much faster than Coastal districts. He answered with all the statistics from 1994-2007.

Since, No telangana supporter agrees that, Telangana is developed, they started their own campaign on this. They have prepared one document criticizing Dr.JP and they gave their own interpretations on this. I don't want to go deep in that either. But, want to mention just couple of points in that.

They have criticized Dr.JP for using growth which is a bigger number, where as the absolute value is much less than that. For that, they took an example of one district in Telangana which has very high value growth and less GDP, and another district of Coastal which has very less value growth and high GDP, and compared both. Then, he criticized Dr.JP for giving incorrect information.

In another section of the same document, they mentioned the GDP values for Telangana and Costal districts. The difference in the GDP for both the areas is around 4%. This clearly says that, their previous point is invalid. If both the areas have almost same GDP, and if one area has better growth, then the area that has better growth is in better position. From their logic only, Telangana is in better position, but, they never agreed to that.

From the overall GDP of Telangana, they removed the GDP of GHMC (which spans in four districts, and which has high GDP), and then calculated the average GDP. Then, they calculated average GDP of Coastal districts without removing GDP of Visakhapatnam and Vijayawada and came up with another number. Then, they declared that, since the average GDP of Coastal districts is more than the average GDP of Telangana, they need to have a separate state for Telangana. If Coastal area people also do the same, (removing GDP of Visakhapatnam and Vijayawada, and including Hyderabad), then the numbers will tell exactly the opposite.

We can draw whatever conclusion we want from the statistics. If we want to show, we are in the better position, we can find some criteria, in which we are at top position. If we want to show, we are in the worst position, we can find some other criteria, in which we are at bottom position.