Sunday, February 28, 2010

Passing Functions To Functions In C++, C# & Java

Before I start the article, I admit I used the word "Function" a little loosely in the title. Functions are available in C++, but in C# and Java it would be more appropriate to call them methods of a class because of the Object Oriented approach followed by these languages.

There are a lot of situations where we write modules such that other developers have the flexibility to fine-tune them to meet their requirements. Assume we are writing a module (function/method) wherein a function/method will be called in the middle of its execution but we don’t know the implementation of this function/method nor its actual name. Wouldn't it nice if we could just leave the implementation of the function/method to the developer who will be using the module?

This is exactly what I target in this blog-post. The article will give a quick overview of how to pass a function (actually a reference to a function) to another function in C++; how to create and use delegates in C#; and finally how to simulate a similar effect in Java which has neither pointers nor delegates.

To help the readers understand the concept better, I will be taking a simple example here. In the example, there is an addition module which takes three arguments - two integers and a function/method which is the display routine. It's not a real-world example as I could have just returned the sum back.

An apt real-world example for this approach would a customized sort routine which takes a function/method which can be used as a comparator for the sort. I will leave it to the users to think of more complex situations where such an approach will be beneficial.

Function Pointers in C++

A Function Pointer is a pointer in C/C++ which points to a function. When dereferenced, a function pointer will result in the execution of the function it points to. A function pointer is defined as -

return_type (*function_name)(argument_list)

An important point that is to be noticed here is that the signature of the function to which the pointer can point to has to be declared in the declaration of the pointer. Once that is done, it can be assigned to any function which follows that signature.

Here is an example C++ program which uses function pointers -

Function pointers play a very important in Callback layered architectures. For example, function pointers can be used to register a function with an event handler.

Delegates in C#

A delegate is a type that references to a method. When a method is assigned to a delegate, the delegate behaves exactly like the method. A delegate in C# is defined as -

access_modfier delegate return_type delegate_name(argument_list)

Similar to Function Pointers in C/C++, the method signature has to be specified in the definition of the delegate. But unlike function pointers, delegates are type safe. This is because since Function Pointers are basically pointers, even an improper assignment will not raise any error until it’s too late. On the other hand delegates are associated with the signature specified and any wrong assignment will lead to a compilation error.

Another important feature of delegates is that delegates can be chained together. That is, multiple functions can be executed when the delegate is called. This is done through + and - operations on delegates. Here is how the syntax looks -

delegate_name reference_name;
reference_name = method1_name;
reference_name += method2_name;

Now when reference_name is called, both method1_name and method2_name are executed one after another.

Delegates also allow methods to be passed to other methods using lambda expressions, which can be written in the method call itself. Go through the following example which shows how to use delegates for method passing and how lambda expressions are useful -

Delegates are very useful in event handling, for defining callback methods like a customized sort, etc.

Simulation Using Interfaces in Java

Ok, now coming to the fun part. How can I get the same effect in Java which has neither Function Pointers nor Delegates?

The approach which have discussed above can be simulated in Java using single method Interfaces. The single method interface can be implemented and the class can become an argument of the method. The concrete class passed to the method is used to call the one and only method of the interface.

However, there are three major drawbacks in this -

  • Every developer using the method is forced to create a class implementing the interface
  • The name of the function is fixed, the implementation will change but not its name
  • We are not passing the method anymore but instead passing an object which has the required method

Similar to C#, the interface implementation can be done in-line to the method call using anonymous classes.

Here is a Java program which achieves the same result as the above two examples -

As we know, C# was heavily influenced by Java. So, almost everything that we can do in Java can be done in C#. The interface technique discussed for Java can also be applied in C#, but I guess it’s more easy to use Delegates for such a requirement than creating Interfaces.

Thursday, February 25, 2010

A Day At Deloitte

Last Saturday, i.e. 20th February 2009, my friends and I visited the Deloitte campus to participate in an event conducted by Junior Achievement India in association with Deloitte. We had a wonderful time there.

Junior Achievement India is a member of JA Worldwide. JA Worldwide is the world’s largest organization dedicated to educating students about business, economics and entrepreneurship through experiential, hands-on programs. JA works along-side business ventures to bring the real world to students and to bridge the gap between education and business.

JA India was established in 2007 and has its mark on several cities in India like Bangalore, Chennai, Hyderabad, Mumbai, etc. Know more about JA India at - http://www.jaindia.org/

Deloitte (also branded as Deloitte Touche Tohmatsu) is one of the largest professional services organizations in the world and one of the Big Four Auditors. Deloitte mainly delivers audit, tax, consulting and financial advisory services through its member firms. Know more about Deloitte at - http://www.deloitte.com/

JA India in association with Deloitte invited our college to a business simulation competition - "JA Titan". There were four teams which represented Osmania University at the event. I was a part of one of these teams. My team members were Roopak, Sunil and Harish.

The competition date was announced as 20th February 2010. We were supposed to have an exam that day and were disappointed that we wouldn’t be able to attend it. But as luck favored us, the exam was postponed due to a bandh and we decided to go to Deloitte.

Deloitte had a huge campus and was really appealing. Initially, all the participants were put through regular security protocols and given identification cards. Then we all were escorted to the cafeteria for a light breakfast. The event started with an introduction to Deloitte, JA and community involvement of Deloitte by the CFO of Deloitte, Hyderabad.

There were basically two things that we did that day - attended a workshop by JA on "Careers with a purpose" and participated in "The JA Titan competition".

The workshop focused on the fact that there are a lot of jobs that are available but the students aren’t equipped with the right tools to be employed for the job. It also emphasized the point that most of the students rarely apply what they study at their colleges and generally tend to work in other domains. A noteworthy point of the presentation was the SEEK principle to estimate if an individual is capable of taking a particular job - Skills, Education, Experience, Knowledge.

Post lunch, the competition began. It was an online simulation competition. The crux of the competition was a sales goal where we were to sell a product in a market for 12 successive quarters with the other teams as business rivals in a virtual market. The consumer base was simulated by the program and the performance of the teams were measured with a performance index generated by the program itself. The game involved various market factors like Publicity, Research and Development, Cost Estimation, Price Decisions and many others. We gave a tough competition to the other teams but narrowly missed out in the end. Well, that’s business - you win some, you lose some. ;)

The competition was very exciting and got our pulse going up every time the quarter closed and results were being declared. Play the game at - http://titan.ja.org/

On a whole, we had a great time and were able to learn a lot about business ventures and their management.

Wednesday, February 24, 2010

The Interview

I am sure a lot of you are curious to know how I got into Capital IQ. Read on.

The placement season at our college was pretty dull in the beginning. The first company which came to us was CommVault Systems, I didn't apply for it. CommVault Systems took two of my friends, Roopak and Ratna Paul.

Soon after CommVault, Capital IQ came to our college. Capital IQ was the second company this placement season and almost everybody in my class who was eligible, applied for it. It was announced that Capital IQ would be coming to our college on 13th of August 2009.

Then began my struggle for the job. I started going through my C, C++, DS, OS and DBMS text-books (actually, had to spend some time searching for them at my house). In the beginning, everything seemed Greek and Latin, but after some time, things started coming back on my mind. Pointers, Inheritance, Trees, Scheduling Algorithms, SQL and what not. Then came the twist, Capital IQ tests the aptitude and verbal ability of students in the exam besides their technical ability. Though I had experience with aptitude before, the pressure of technical questions and aptitude questions was over-whelming. I almost thought of giving up, but later decided that I won't go down without a fight. I studied everything what I could in the short span of time and hoped that it was sufficient.

The D-day finally came. Dressed in formal clothing, wearing polished black formal shoes, my friends and I were ready for the interviews. I couldn’t believe that we were the same students who hang out at our department parking, bunking classes. ;)

The placement process of Capital IQ started with a pre-placement talk about their company. In this presentation they explained us about McGraw Hill companies, about Capital IQ and their work culture. They introduced the various work positions that were available at their company. They gave a clear view of what each job profile does and asked the students to make their choices. I felt that this concept was very good as they allowed the student to decide what he/she wants to be - Developer (Software Engineer), Database Administrator, Business Analyst, Quality Analyst and System Administrator. I was certain that I wanted to be a Developer, but as I had some amount of exposure to Databases, I thought I might be able to do justice to the role of a DBA too. They also mentioned that they primarily work with .NET and SQL Server.

The selection process started off with a written test which consisted of a lot of technical and aptitude questions. One boon was that there wasn't a verbal section. The paper was segregated into five sections - the first one was completely to test the quantitative ability of the student. It was reasonable and for students preparing for management exams, it was a cake-walk. Then there four other sections which tested programming capability; SQL and database concepts; the ability to comprehend and explain business requirements; and the ability to generate test use-cases. That’s one section for every job profile. This was a great approach as it would indicate the strengths of a student.

They eliminated a few students in the written round and proceeded to the next round – Group Discussions. This was where most of the screening was done. They divided all the shortlisted students into 3-4 groups and gave each group a topic to discuss on. The topic which I got was - "Has the Satyam scandal tarnished the image of India". I always believed that it hasn’t and supported my viewpoint.

After the group discussions, the technical rounds began. I went through two technical rounds at my campus.

In the first round, I was asked mostly about Data Structures, SQL and OO Concepts. They also asked me a few basic questions about AJAX and ASP.NET as I put them in my resume. There was also an analysis on a few questions from the written test.

The second technical round was with Mrs. Nalini, a senior architect at Capital IQ. I concentrated mainly on my 3-2 project - "Synchronous and Asynchronous Database Replication" in this round, emphasizing on why it was done and its modular approach.

That was the end of Day 1. They didn’t announce the results that day. 4 days later, i.e. 17th August 2009, they informed the placement office that around 7 of us were shortlisted for the next rounds which were to be held at their campus on 19th.

19th was a big day for all of us. Initially there was a technical interview with Mr. Krishna Kant, the director of Capital IQ, Hyderabad. This time I started off with my projects and gave a brief overview on my 3-2 project and my 4-2 project - "Cloud Computing Portal for SMEs". The interview also stressed on why I wanted to be a developer and the various small tools I built before.

Finally, there was a video conference with Mr. Jeff, a senior employee at Capital IQ, New York. This was a new experience as I was never a part of a video conference before. However I quickly got tuned to it as it was pretty similar to a telephonic conference. Here, I was asked about "Service Oriented Architecture" and my presentation at Transition '09, the technical event of CTS and my experiences as a Sun CA.

So finally, after a written test, a group discussion, three technical interviews and a video conference, the good news rolled in on 9th September.

I was delighted to hear that I got into Capital IQ as a Software Engineer, but at the same time was sad that only two students from my college were placed into Capital IQ. However I am glad that as of today, the placement season is on a full swing and almost all my friends have been placed.

Along with me, V.V. Aishwarya, a classmate of mine was also selected as a Software Engineer.

Honestly speaking, I never expected that I would get into Capital IQ in the beginning of this process. But at the end, it was a pleasant surprise. :)

Friday, February 19, 2010

Capital IQ - A New Chapter In My Life

I recently joined Capital IQ as an intern. This blog-post gives a brief overview of Capital IQ. In my subsequent posts, I will be explaining how I got into CIQ and my experiences there.

Before I tell my story, I should introduce Capital IQ to all my readers who don’t know it yet. To know about Capital IQ, you should know "The McGraw Hill Companies Inc."

The McGraw Hill Companies Inc. traces back to 1888. Its primary areas of business are education, publishing, broadcasting and financial and business services. In India it is popular in the educational sector as Tata McGraw Hill. I am sure most of us have grown up studying the text-books published by TMH. Know more about the McGraw Hill companies at http://www.mcgraw-hill.com/

In the financial sector, McGraw Hill shines as Standard & Poor's (S&P). S&P is the division that publishes financial results and analysis on stocks and bonds. S&P traces its history back to 1860 and was acquired by The McGraw Hill companies in 1966. Know more about Standard & Poor’s at http://www.standardandpoors.com/

Capital IQ is a provider of information and analytical tools for investment bankers, money managers and other financial professionals. Capital IQ was founded in 1999 and currently operates as a division of Standard & Poor's after its acquisition in 2004. The company's headquarters is located at New York City and has several regional offices in various places in the world like Boston, San Francisco, Canada, London, Argentina, Hyderabad, Gurgaon, etc. Know more about Capital IQ at https://www.capitaliq.com/

If you want to know how I got into Capital IQ, please read my subsequent posts.

Thursday, February 18, 2010

The Shining Sun

Well, I am sure by this time you would have come to know that I was working at Sun Microsystems Inc. as a Campus Ambassador. This blog-post gives a brief overview of my experiences as a Sun CA.

If you have already gone through my Sun Blog, this post may seem redundant.

I joined Sun Microsystems Inc. in September 2006 as a Campus Ambassador for my college, Osmania University. The job profile was primarily of a technical evangelist at my college. I loved the job as it would give me an opportunity to learn new technologies and at the same time give me experience in event co-ordination and crowd management. So, I applied for the position and after a telephonic interview, I got into Sun.

January 18th was my last day at Sun. The entire Campus Ambassador had been closed that day and I out of Sun now. It was a wonderful and memorable journey. While working as a CA, I got a chance to meet a lot of like-minded people from various parts of the world and had a wonderful time working with them. At the same time, I could learn a lot about the technical products of Sun Microsystems Inc.

During these 17 months, our college reached new heights in the open source world. Here are a few of the major mile-stones reached by my college -

  • Inauguration of S.O.U.R.C.E
  • Software Freedom Day 2008
  • The Ref Contest (Received accolades from several people and the idea was replicated in many countries)
  • Establishment of Sun FTP Server at Osmania University
  • Sun Tech Days 2009
  • Sun support in Infinity 2009
  • 18 internships from Sun Microsystems Inc. as a part of Industry Association Program
  • Software Freedom Day 2009

Besides these major mile-stones, Osmania University was a part of several competitions and technical sessions in this period.

My recognitions as "Sun CA of the month" and "Sun CA of the quarter" were the biggest honors for me at Sun. I also got myself certified as a SCJP and as a SCWCD during this period.

Before I conclude, I would like to thank Sun Microsystems Inc. on behalf of Osmania University for giving us this opportunity and at the same time thank Osmania University on behalf of Sun Microsystems Inc. for their unvarying support and cooperation to the open source community.

If you would like to know more about my experiences with Sun, visit http://blogs.sun.com/gautam and in case Oracle pulls it out, I will replicate it elsewhere and provide the link here. Here is a beautiful picture from James Goslings blog showing the emotion of every Sun employee after the acquisition.