Search This Blog

Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Tuesday, February 03, 2009

Relearning Coding

 

First of all, I should admit, I have been spoiled! I have been spoiled by the IDE features of Visual Studio in C#. And now these days, I am getting to rectify my spoilt habits of coding. I am having to code in C++ these days. Intellisense is not even 10% as good in C++ as it is in C#. This post documents some of the things I have re-learnt over the past few weeks.

1. Write every line of code only after understanding fully the implications of what you are doing. There is no extra intelligent IDE working behind the scenes to redden the areas where you have slipped up. And this becomes all the more important if you have to recompile because of a semicolon or a incorrectly typed variable or a missing header file.

2. Never assume anything after seeing the name of the function. Do not use any function unless you have gone through its code and seen how it is implemented and how it is working. This is also for learning purposes. Usually you learn a lot after seeing the best practices.

3. Uncover your hidden assumptions. Train your mind not to make any assumption what-so-ever. An ancillary point is that you should check for errors after every single line of code. The checking errors part is so hard to do.

4. Always follow the principles of information hiding. Make only as much visible as is important. For instance, if you have a loop like this :

int i;

for(i = 0; i < 100; i++) { }

then this will be much better:

for(int i = 0; i < 100; i++) { }

Since in this i is only visible to the loop. You should follow this principle not only at the loop level, but also at the bigger levels like classes, header files, dlls and basically at every place you are coding.

5. And before I forget, always deallocate the memory you allocate. This becomes really hard and easy to forget if you allocate memory in one function and use it in another. Also, never try to work with null pointers.

This post was meant more for self-documentation and might include a second part as I get to know more and more stuff about coding.

Saturday, January 10, 2009

Professional Coding vs Academic Coding

As usual I will start this post with a caution…

Caution: In this post, all the comments are being made by me(still in college!), an intern at Microsoft who has not written a single line of shipping code till now. All these comments are based on my very very limited experiences. So, don’t take my word for anything. Also, note that I will not be talking about any of the internals and there will be very little or no inside information to be taken from this post. So, now the long rider is over, I can start with the post.

STUDENT MICROSOFT
Hey, wouldn’t it be cool to have this X functionality in the code. Hey, wouldn’t it be cool to have this X functionality in the code.
Will I be able to code this… Let us made a PoC(Proof of Concept) for it.
Start churning code… PoC shows its possible. After Devs, PMs and Tests agreement, it is decided to keep the X Functionality in the code.
Still Churning code… PMs sit down to prepare a spec for it detailing the UI, various scenarios and how the software should respond in various conditions.
Frustrated and impatient quits developing the feature X. One brave dev makes the design for the X Feature in which he tries to make sure that the feature is extensible, testable, modular and all those heavy terms of software Development.
On Google Search finds one cool project on SourceForge which does the same. There is a design meeting in which all the other devs make big holes in the design doc of the developer citing all those heavy terms of software Development.
Very Happy with his find, student goes out to have fun with his friends. Dev Makes some changes and after some iterations the design is finally approved.
Comes back, tries to compile the code and fails. Posts for solutions to this on a hundred forums… (sometimes with a female account) Gets down to coding.
Somehow, gets an answer at one forum and is finally able to compile the code. Coding Complete. Goes out to a senior dev for code reviews.
Finds out that the code is written in some obscure language(read PERL) and has no idea how to call it from his own C/C++ code. After doing some changes finally submits it to the repository. (Note that this happens only after some iterations of changes and mails being exchanged to and fro)
Again does googling and posting on other 100 forums and then goes out with friends. Code submitted to the repository but there is a build break.
Finally gets an answer and is very happy and goes out to enjoy. Comes back and tries to do the exact steps as entailed in the answer and whoa! the code is integrated. Build Break resolved, the X functionality is incorporated in the product.
Now is the time to enjoy… Testers prepare tests scenario which are then approved by the managers.
Shows it to a friend who manages to crash the app in one second. Testers write automated test cases for the X functionality (This test code is actually longer than the code written for X functionality)
Gets down to fixing it. Bugs found and assigned to some unfortunate dev who gets down to fixing it. Actually an assigned bug is shown with a gift icon in the top right corner of the desktop!!!
Fixed and shown to professors who work with it for 5 minutes and approves it. Bugs fixed and this cycle goes on for a lot and lot and lot of time(Read M1, M2, M3, M4 …. Alpha, Beta, Gamma, ZBB etc  etc etc etc )
Enjoy!!! Enjoy!!!

You decide which is better….

Sunday, December 14, 2008

Foodforce 2 Alpha Release

 

For all those readers of the blog, who have been following the Foodforce2 project, we have some good news! Alpha version of the game has been released now at : http://code.google.com/p/foodforce/downloads/list. The game is playable both on Windows and Linux platforms.

Also a new blog has been started for the project which will carry all the news regarding the game at : http://foodforce2.blogspot.com/

 

Screenshot

You are welcome to send in your feedbacks at foodforce2@gmail.com and can also tell us about what you feel in the comments section.

Sunday, December 07, 2008

#nsit @ Freenode

You can find me on IRC at #nsit channel usually with the nickname “deepank” (yea yea I could not think of a better creative name!). Now, why this sudden urge to start a new channel at IRC for the college. Well first of all, its not my idea, it was Mohit’s idea originally and I brushed it aside that time.

This is how my thought process went : who wants to come to chat on IRC when there are many better chat clients available like Google Talk. But then I thought: hey there is one feature that Google Talk does not have. And the feature is: Chatrooms or channels. At IRC, a channel might contain hundreds of members who can all chat simultaneously on a single window. If you have never been on a noisy IRC Channel before, you might think that it will be hodge-podge, believe me it is not! Humans have a beautiful ability to self-organize when left on their own devices.

But this was not the only motive behind starting up with the IRC Channel. IRC has the unique marketing appeal of being geeky. It has been made hard to use with the lack of good IM Clients. Nearly all of them require you to learn to use commands, and using commands in today’s user-interface crazy world is a sure shot way of advertising that you are geeky :)

So, I believe that the new IRC Channel will attract all the programmers and geeks of the college. It will not have 100s of members, rather a very small following of 4-5 people at best. In the past two days, its membership has not gone past 5. But, I believe that the membership will rise slowly and steadily to over 10-20 people in 2-3 years.

The channel is here to stay… and I would love to hear back in comments.

Sunday, October 19, 2008

AI Challenge

 

For those who do not yet know what AI Challenge is, I introduced it in a previous post here. Innovision’08 completed yesterday with the AI Challenge also being hugely popular and largely successful. We had 26 code entries for AI Challenge this time around with some of them spanning thousands of lines of code. This is roughly 250% increase in participation from the first edition of the AI Challenge in the year 2007. With over 96 teams (of 3 each) registering and 26 teams being able to successfully submit running codes, the challenge was hugely successful.

The simulations were done on-site yesterday with open invitations to participating teams to see the simulators crunching it out in real time. The teams were divided in 4 groups with the league stages involving matches of every team with every other team in the group twice. Two teams were then selected from each group to go into the Super 8 stage of the game from where we got our final 3 winning entries. But yesterday’s simulations were not all, the work on the challenge started around one and a half months back and it ran online for 15 days.

All the congratulations are due for Mohit, Vijit and Chirag as they have done some really wonderful work during this period. They have had sleepless nights, long debugging sessions and attended phone calls till 1 AM at nights while solving the queries of the participants. And I have still not counted the usual rigors of organizing an event. We have all learned a lot during this small period of time and I sincerely believe that the third edition of AI will be even better. Some of the additions planned for the next edition are : Graphical Simulator, Automated Code-runs and of-course minimal bugs.

So what are the next steps ahead of us:

1. Make the codes, rules and other docs available online at : http://code.google.com/p/ai-challenge/ in the downloads section of the project.

2. Write letters to winners asking if they would like their codes published online with due credits under the GPLv3 Licence.

So, the challenge will be still available for playing and you can still send us your entries at either (deepankgupta [AT] gmail [DOT] com) or (mohitgenii [AT] gmail [DOT] com). If we like your code, we will publish it online and provide you with detailed reports and logs on its performance against others.

Before I end, I want to thank all the 26 participating teams who took it as a personal challenge to develop some great codes and be a part of this wonderful event. Hope to hear your comments and complaints about the event here. 

Wednesday, October 08, 2008

AI Challenge and Digital Fortress

We all love challenges, and it is said that if you don’t challenge your brain too often, it will lose its usual brilliance. So, this time around, Innovision, our college’s tech fest presents among others these two technical events which will hopefully bring some entertainment and purpose in the lives of idle reader reading my blog right now. I will discuss briefly the two of them and tell you more about what goes into the making of such events.

AI Challenge: As the name suggests, it is all about Artificial Intelligence, with the theme of this year’s event being Soccer. Yes, you have to control a whole team of 11 players through your code, and your superior strategy should be able to defeat the other team’s code in a computer simulated soccer match.

The idea is simple and the simulator along with sample codes are already available on the website : www.aichallenge.inno.co.in . So, what goes into the making of such an event, well lots of coding! Obviously first, Vijit and Chirag came up with this wonderful idea complete with drawings on chart papers as to how the thing will work. Now, Mohit worked alongside these two brilliant people to formulate a set of rules keeping in mind that these should be simple, yet effective. Now, came the task of designing a simulator which will run the user codes against each other and simulate the whole task. Once, the simulator was made successfully, there were brainstorming sessions and simple sample codes were written and tested out so that an easy idea on how to approach the problem can be provided to the contestants. I was there as always, with the motivation and constant reminders of the mistakes that we committed during the previous edition of AI Challenge!

Digital Fortress: The idea here is to give the reign back to the early hackers/programmers and provide them with some REAL problems to work with. This is not the usual run-the-mill C/C++ programming contest, but rather contains questions which will involve a mix of programming across various domains, Internet based skills, hacking and a cool smart head on the shoulders.

The event’s brain is Assim who always gets these wacky ideas in his head and is a true hacker by his nature. I can simply write another blog post on this wonderful person, but I guess now is not the time. After running Enigmata successfully for two years, which involves solving puzzles, there came a thought, why not make puzzles with a technical flavor. So, came the Digital Fortress. Rohil and Ritwik have been working alongside him to develop some of the most difficult levels which I know would require some really good effort to get through. Looking forward to it getting started soon. Check out more details at: www.inno.co.in/onlineevents.php .

Lets throw the floor open to comments, rants and words of encouragement from participants and observers alike.

Thursday, October 02, 2008

Gandhian Principles for Computer Geeks

 mahatma-gandhi-indian-hero1                          computer_geek

On the eve of Mahatma Gandhi’s birthday, I thought it will be best to teach the Gandhian principles to the computer geeks and programmers alike.

1. Love Your Enemy

I know its hard to do, but you have got to love those nasty bugs that crop up in the code you write. By love, Gandhiji does not mean that you have to sing love songs to them, but you have to understand them and have compassion for them. Understanding why they occur can be critical in your approach towards defeating them.

2. Always be truthful

This is very important, in case you bring down the whole system while you were playing around with the system of your organisation, be truthful. Take up the matter in your own hands to fix it and admit your mistake to the system Adminstrator.

3. Never use violence

This is one of the major principles of Gandhiji. Whenever, you encounter that undiscoverable bug or a heisenbug don’t just crash your computer and/or any of its parts. Instead, take a deep breath, control your anger and go out to have a coffee.

You have to understand that while unneccessary violence will help you vent your anger, it will only make your enemy gloat at you. Thus, keeping an impassive face, feigning resignation by moving away and then attacking it after some time (non-violently of course) always works for me. 

4. Try to win your enemy over to your side

This is one very simple principle. If you can’t correct the bug, make sure you make it known to the people that it is a feature in the product. Many of the major features might have come this way and we would not have known.

5. Don’t be angry; suffer the anger of your opponent

You don’t have to show anger to the opponent, it only causes the bug to gloat in happiness. Rather suffer its anger, slog it out, it draws the attention of fellow geeks to your cause and they will come out and help you when they see you spending hours in your cubicle while you should be out there playing foosball/TT with them.

6. Wean your opponents from error with sympathy and patience

This applies when you are coding. You do the coding with patience, sympathy for the bugs, and you will find yourself debuging less.

These principles appear to be hard to follow and might prove to be slow in working, but there is no other principle in the world that can help you more.

As usual waiting for the comments.

Tuesday, August 12, 2008

Electrical Switches and the power of Defaults

Ever noticed a peculiar property of an electrical switch! It is always made like this:

Always when you click ON, the circuit gets completed and the electricity flows. Imagine if it was the other way round i.e. when you put the switch to off position, it inserts an insulator in the circuit and the circuit breaks. In that case, if a switch gets old, imagine switching off and then also the fan keeps running.

So, why did the electrical designers choose the first design over the second one. I think they did because of two reasons:

  1. Most of the times, any electrical appliance is in the off state and the circuit is broken, so it makes sense to do this.
  2. It is much safer and cheaper to implement. Imagine, how would you implement inserting an insulator and think of different approaches to do that, to get the meaning of this.

Although, the second reason would have been the driving force in the case of switch designers rather than the first. But, software engineers often do not get the same luxury. They do not have the second reason to help them decide their defaults because coding wise, often the two options take the same time. So, a software engineer must always keep in mind, what option will be in use most of the time.

Default options are always there to take care of the 80-20 rule. 80% of the users of your product will want a certain functionality more often, you need to know what they need the most and make it available as a default or as near to the user as possible.
A case in point: Office 2007. It does not add any new functionalities over Office 2003; but it changes its default options very beautifully. It makes all the often used options available and displayed in a ribbon, instead of hiding them in multiple menus. Another great idea has been to change the defaults according to the action you are doing. So for instance, when you are writing, you are on Home tab, when you have selected a picture, you are on picture tools in Word 2007.

Another case in point: The difference between Nokia N70 and Nokia 6500. The most striking difference to an end user are the default options. It's a pain to navigate into multiple menus to get to one of the oft-used options in N70, while a newer model understands this pain of the user and makes the most default options available at a single click. The defaults are one of the reasons why Sony Ericcson phones made a huge market in India. They did it because they provided dedicated keys for music, Bluetooth and camera options. I do not mean to take the credit away from their superior camera and music quality, but just another factor contributing to their popularity.


So, the point is, how do I, as a software developer decide, where to keep my default options. Most of the times, the cost or the effort will be the same for implementing any of the options as default ones. The answer is: you do it the hard way. You do it by understanding the needs of your user, you do it by dogfooding your own code zillions of times, and be aware of where you can make it better.


Identify, the oft-used features and make them the defaults.

Saturday, December 15, 2007

A Typical Conversation

 

Hi,

We all studied programming on blue-screens with 17 year old compiler namely turbo C in our school days. What happens when you meet someone who tells you to change. Here is the sample conversation -

Q. Why not to use TC for Programming

Ans. Here are the reasons:

1. Less Horsepower: Seriously guys, 16 bit programming is out of the Window. An integer of 16 bits, even when I do some calculation that involves a multiplication, I do not use int for the fear of overflow. Then comes the problem of the so less the amount of stack space. Try running a recursive program with a large data input set and you will know what I am talking about. I once made a brute force algorithm for a problem which I wasn’t able to solve (I am not very good at solving problems) and was out of stack space in TC, went to Linux, ran gcc and got my answer. In those days, I did not have the luxury of a Visual C++ compiler.

2. Where are the graphics mate? : Yeah there are no graphics. You are stuck to the some 640*480 resolution on a widescreen 1200*800 laptop receiving flattened and coarse graphics. Then to add to it, only 16 colors, and don’t even think of making a windows or a menu bar. The effort will kill you. Had to learn the assembly programming and INT21H routine just to control the mouse.

3. Ok What about the data structures : Sorry no strings, no bools, no object conversions, no complex data structures like trees, hash-tables etc. Also, you have to be a math genie to do all the mathematical functions coz it provides a bare minimum library of functions.

4. I once scratched my head out thinking how I can run two tasks side-by-side at the same time. Like if I make a DOS game and want to put up a clock for timing you, I have to resort to the crude method of using delays and all to move the clock hand. No events, no timers for me. Also, I run my clock first and then the game and the game gets all freezed up when you repeatedly redraw the screen a 100 times a second. Ofcourse, I dint know about threads or event-based processing instead of the sequential processing of DOS.

5. Grand Old Man: Turbo is too old to get hold of new ideas like music or web. So, comes the problem of no integrity with web, music, other processes etc.etc. etc.

Q. Surely, I can do something with it, I mean I have spent all my life programming in it?

Ans. Oh yes you can. You have achieved quite a lot in it. Tell me all about what you did and how many lines of code it took you.

Q. I made a ummmm ….. a tic-tac-toe game in TC J and it was just a 1000 line code.

Ans. Make a similar code in Visual C++ or GTK with GCC in just 500 lines.

Q. Ok I agree it is a horrible compiler, but I still love the way it displays help and helps me write my code.

Ans. That’s just because you have still not seen the world of IDEs .Modern IDEs have code-folding, auto-indentation, paranthesis checks, TODO Lists, Documentation features, online help, code-completion and better debugging support.

Q. But won’t so many features bog a beginner like me down?

Ans. No they won’t. First of all the first time you bought a computer, did you start hacking into its registries and making changes like minimizing delay times on menu items. But you still found windows interesting enough even though every menu item took 200 ms more to open than it does now. Same are these features, they will only help you save those 200 ms if you look for them. Otherwise they are just there.

Q. But I have heard MSDN help is bad for a beginner. Linux Man Pages are tough to read.

Ans. Yes they are. But Turbo help too was difficult to read at one time. Wasn’t it. Give it some time and you ll catch on.

Q. Even though I am using TC what harm am I doing to myself? I am just not gaining benefit from features as you said earlier.

Ans. What I said earlier was different. You were not using the features that were available. You would have caught on at some point of time. Here there is no hope!

So, you are harming yourself. With each passing day of working on TC, you are forming opinions about your capabilities in programming on the basis of the capabilities of TC. You are re-inventing the wheel everytime and getting more frustrated by programming by the moment.

You fell yourself not capable enough to do the things these professionals do. Why? Just coz you don’t have the right tools.

Q. So, what’s the bottomline?

Ans. If you are using TC today also, please dump it and move on.

Saturday, April 14, 2007

Google Summer of Code Proposal

Introduction:
We set up an ad-hoc network in which we provide IP Addresses to the various nodes using zeroconf. Zeroconf is used to provide network addresses for the various nodes, without the need of a DHCP Server. I am planning to use Avahi service for implementation of Zeroconf. The numeric addresses are already chosen randomly by the IPV4 and IPV 6.
The coding will be done using C and C++. The project will involve developing a basic infrastructure for ad hoc communication using Bluetooth and then extending it to provide various types of functionality.

Use Cases

1. I want to share a file with my neighbor: Phone starts detecting for phones which want to share around me. After the establishment of connection, I am given an option to send file. I click OK. File is sent over the newly established network.
2. Chatting:
a) Join an existing chat going on.
b) Start a new chat network
3. Surfing internet: If I have a laptop or computer nearby; I can connect to it using BlueZ D-Bus API and start accessing internet. Need to provide a GUI for doing this painlessly.
4. Audio Chatting: I want to chat with a person with whom a connection has been established through voice and not just text.

Deliverables

Required
1. Make a GUI application to facilitate the following:
a) Searching nodes
b) Getting information about neighbors.
c) Sharing files
d) Connecting to internet through an internet connected nearby node such as laptop
2. Make a chat application which allows text chat.

Optional
To Allow Audio Bluetooth communication by porting ALSA-Bluetooth to openMoko . (2)

Schedule:

1. Analysis: This phase involves interaction with the mentor to estimate the exact requirements. The exact project modules will be determined in this phase. Project infrastructure such as code repository and website may also be setup.
2. Development I: This phase will involve integrating BlueZ D-Bus API and Avahi to set up an ad-hoc network through bluetooth. This network can discover the nodes and files etc on the nodes.
3. Development II: This phase will involve the development of GUI for the easy application of all these things. The aim would be to integrate this with the native chat functionality provided with openMoko.
4. Development III: Integrating audio and other refinements.
5. Testing: This would be the final stage of the development. In this stage the final testing and bug finding will be done. The objective of this phase would be to ensure smooth functioning of the project.