Archive for the 'Programming' Category

What programming language should I learn?

Mar 02 2010

Sameer asks what programming language he should learn. Below is an edited version of my reply.

The plumber comes to your home and asks: “What tool do you want me to use?”.

What will be your reply? “I don’t care! Fix the damn problem”.

That’s right. Everybody wants to get their work done, and get their problems fixed. They don’t care what tools you use. As a developer your job is to solve problems your costumers have in the most effective manner. This in turn means that you cannot use the same tool for every type of problem. Can you use an electric driller to fix a small leak in the pipes? No. You may want to use the duct tape for that.

Which language should I learn is a wrong question to begin with. Languages are tools in the bag of a software engineer. Before deciding upon the programing languages you want to learn, you should decide what type of problems you would like to work on. Would you like to work on web technologies? Would you like to work in the Linux ecosystem? Would you like to work in the mobile platforms? There are a million different niches in programming world and you have to ask yourself all those rhetorical questions that comes to your mind and then decide which language suites your choice.

This does not matter that the tools are not important. They are; but they are not more important than the problem at hand.

How many programming languages should you learn?

There is no point in trying to learn as many languages as you can. What you should do is to try and learn about as many languages as you can, and then decide which languages you should gain expertise in.

Going back to our analogy, what tools do you think a plumber should carry in his bag? “Enough tools to get his work done.”

Exactly. If you know to use just one tool, you may be forced to work with other people who can use some other tools. This happens in most corporate IT companies. In large companies you will be working with other people who have expertise in programming languages and tools which you don’t know how to use. This has the advantage that these people will be real rock-stars in their own narrow fields. Instead if you want to work in places like startups (or if you want to work as a freelance developer), you may want to know a little bit of every type of tool out there.

Of course you don’t have to know to use every type of screwdriver. You just have to be expert in using one good screwdriver model. Similarly you don’t have to be an expert in every web development language. Just learn a pretty decent one and you should be fine.

Every programming domain has its set of tools to help you develop softwares. If you are developing an enterprise website, you may be working with technologies like Core Java, Servlets, EJBs, XML, Unix Shell Scripts, Log parsers, Databases, Various web-servers etc. This means that in addition to programming languages there are many other technologies related to programming that you should master in order to be a good programmer.

One more thing you should know – all the programming languages are inherently different from each other. Some languages are easy to program in (eg Python) while some others are difficult (C/C++). I am not referring to the expertise needed in learning the language. I am referring to the effort required in writing a program after you learned the language. If you work as a programmer in an IT company, you will probably learn a new language (may be as per business requirements) in a very short time span. You will start writing decent code in about 1 week to 3 months time. Then the only thing that will matter is which language you really prefer to work with. So don’t worry much about which langauge is easier to learn; worry about which language is easier to use. (There is a correlation here though. You will find that in most cases the languages that are easier to learn are the easier to program in too)

You can learn a lot about programming from forums were smart programmers hang out (eg Proggit and Hacker News), read the top articles and ask your questions there; you will get in-depth answers.

The biggest secret:

You will become a good programmer only by – programming a lot. Many students don’t program outside their labs and college projects, and they never become good programmers. Try to do some coding in your free time. Try to solve Project Euler problems in your favorite programming language, or try to build a website of your own.

Having said all these here are some specific tips. These may or may not work in your case:

  • Enterprise development: Learning Java is a good. Java is used in many software shops as the primary language. It will take you a long way in most situations. At the same time, I have some objections with using the language from a startup programmer point of view. Read the discussions here too.
  • Web development: Stay away from PHP. It is a badly designed language. Instead, learn Django or Ruby on Rails. If you prefer Microsoft technologies use ASP.Net MVC.
  • Windows development: Learn C# (and probably not Visual Basic). For running C# applications in Linux, check out the Mono project.
  • There are many excellent programming tools or IDEs you should try to master. Eclipse is a popular IDE. Notepad++ is a popular code editor.
  • You should be learn about stuff like Regular Expressions, Unicode, Information Security etc. (I cannot even attempt to list all the topics)
  • Try to keep up with new technologies. You don’t have to learn all the latest languages, but try to have an awareness of the latest trends in programming. For example, web development, mobile phone development etc are areas where lots of innovations are happening. You don’t want to miss any of those if you are intersted in those fields. Then again, the forums I mentioned above will come handy.
  • Learning just one language is not very good idea. Learning a lot of languages is also not a good idea. Strike a balance between the two extremes and try to be good in at least 2-3 different programming languages in different fields. (As explained earlier, different languages are used to solve different types of problems)

Good luck!

One response so far

Wanted: Superman

Feb 08 2010

Some time back, I could not resist sending the following reply to this person at the recruitment agency:

Dear Sandhya,

Is this a joke? Did anyone really go through the so called assignments before forwarding them to me? Are they asking me to build two full-fledged websites in 12 hours? Best of luck finding candidates who can do that.

BTW, if you can get candidates to build these sites for you, why do you have to recruit them? I have to admit, this is an excellent way to get your works done for free.

I am sorry. I genuinely not interested in working for such a stupid company.

(Thanks for contacting me with the offer though. Really appreciate that. Let me know if there are openings in companies which are not looking for superman as their programmer.)

- Niyaz

I am refraining from attaching the two assignments they asked me to complete before the interview. Not because I don’t want to expose the actual company that tried to trick me into building the websites for free, but because when you see the requirements which will make Facebook ashamed of themselves, it will make you sick for the rest of the day.

2 responses so far

The perfect compression algorithm

Jan 09 2010

A story from my college days.

I stormed in with full excitement to the room of two of my friends. I had found the perfect compression algorithm.

“I found a compression algorithm that can reduce the size of any file!”

“That is impossible. What is your algorithm?”

“I cannot share the algorithm right now, but I will tell you once I write a program to compress files.”

“Don’t you know that it is impossible to write an algorithm to compress all files?”

“Yes, but… now I have this algorithm!”

So what was my algorithm?

1. Consider the text to compress as a large number.

2. Find the integer square root of the number.

3. The square root and the error form the compressed text.

For example, let us compress the number 98653784578.

Integer square root of  98653784578 = 314092

Error = 114 (i.e. 98653784578 – 3140922)

314092 and 114 form the compressed text. We just saved 2 characters using our algorithm!

If the original number is of length n, then the square root and the error will be of maximum lengths n/2 each. So this will mean that the final compressed text will be of a maximum length n and possibly even smaller.

Can you find the flaw in the logic?

Looks like I was not the only one who invented a perfect compression algorithm. At least I did not spend 12 years to understand that perfect compression is not possible and I did not get publicly humiliated (not until this post, but who is reading this anyway ;) ). There are various proofs for why perfect compression cannot be achieved.

7 responses so far

Get cached images from your visitors

Dec 12 2009

Jeff Atwood (Coding Horror fame) was in for a horror when he realized that his server crashed and his data was gone and due to some reason, the backup mechanism was not working. The complete data in Coding Horror and the StackOverflow blog disappeared.

Since his blog is very popular, many archiving systems including the Google cache have copies of the pages and I hope that they have by now recovered the complete textual data. The biggest problem in this case is getting back the images. There are not many archiving services that may have the complete backup of the images in the website.

So what should Jeff do now?

Since Coding horror is a high traffic blog, I think there is a way to get back at least some of the images. (The probability of this working depends a lot on the traffic to the website and a bit of luck)

Here are the steps:

  1. Configure the web server to return 304 for every image request. The HTTP status code 304 means that the file is not modified and this means that the browser will fetch the file from its cache if it is present there. (credit: this SuperUser answer)
  2. In every page in the website, add a small script to capture the image data and send it to the server.
  3. Save the image data in the server.
  4. Convert the pixel data to get the original images.Voila!

Capturing the image data

We are going to use the Canvas functionality in HTML 5 to get back the image data.

Here is the code you should insert into the pages of the website. It gets all the images in the current page, loads it to the HTML Canvas, gets the pixel data for the image and sends it to the server through an Ajax post.

This PHP script (Can PHP rescue Jeff? ;) To be fair, the server side code is trivial) saves the data to files in the server. Note that the files themselves will not be images, they will just contain the pixel data of the images. In addition to this, we are also saving the original file name and the image dimensions. This means that we can easily reconstruct the original images from this data. Data from every visitor is saved in a different file to just to make sure that you have enough redundancy (Watch out for his redundancy filling up your server disks)

Remember that this is a proof of concept code. You will have to modify it to use it in regular production environments and to get some real use from it. There are many limitations to this code. It goes without saying that you will get the image data back from the users only if they have the images cached in their browsers. This script will work only in the latest versions of Chrome, Firefox, Safari, Opera etc. (Don’t ever expect it to work in IE for the next decade). In addition to this, remember that the pixel data will be many times bigger than the original file size and you may have to carefully analyze the disk space usage of this script. (I guess in an emergency, none of these really matters).

You should edit the post URL in the script to match your domain name.

Finally, I have tested the code and it seems to be working (for me, at least). You need to include JQuery in the pages using this script and remember that due to security restrictions in the browsers, you will have to place all these files under the same domain name. Please tell me if there are any other flaws in the code.

[Updated: code changes to reduce the file size by 50%. The decimal numbers were converted to hex and the spaces in between the numbers removed. The file sizes can be further reduced by using the full character set.]

18 responses so far

Recursion & bad examples

Nov 07 2009

If you ask a typical computer science graduate from Kerala to write a program to print the nth Fibonacci number, most of them* will invariably give you the following function:

int fibonacci (int n){
    if(n<2){
        return n;
    }else{
        return fibonacci(n-1) + fibonacci(n-2);
    }
}

So far so good, except that the answer is wrong.

Recursion is the worst way to find a Fibonacci number. The last time I checked it was impossible to use recursion to compute even the 50th Fibonacci number in a personal computer!

If it is impossible to calculate even the 50th Fibonacci number using this function, how could you possibly teach something like this in a computer science course? The only way Fibonacci numbers should be calculated is by linearly adding the numbers in a loop or by using any direct formula you have. Of course for some applications you can speed up recursion by remembering the child nodes in the tree and thereby avoiding doing the same calculations again in some other branch.

The scariest part is yet to come. In many colleges they use finding the nth Fibonacci number as the primary example for teaching recursion!

Why not teach students the best possible way to find the nth Fibonacci number? Why not teach a real world example for recursion? Is it necessary to teach the concepts in computer science using lousy examples?

*Take blanket statements with a grain of salt.

7 responses so far

Using sessions in WordPress

Oct 31 2009

WordPress does not use sessions to hold data. It is a stateless application. This means that if you want to use sessions in your plugins or custom modifications of WordPress, you may need to do a little hack to enable sessions.

Edit your wp-config.php file (located at the root of your blog) and add the following line at the beginning of the file:

session_start();

Now you can use sessions in WordPress. Remember that for most plugins, instead of using sessions to store data you can pass variables in URLs and hidden fields or use cookies, and that is what WordPress encourages.

One response so far

Why you probably should not learn Java

Apr 17 2009

There are a lot of programming languages out there and people are always confused about the programming language they should use to develop their applications. We see a lot of people asking the question “Which programming language should I use?“.

And there are many fresh graduates and new programmers who want to add another programming language to their resume and they too are confused about which one to learn.

There is something called the TIOBE Programming Community Index which lists programming languages based on their popularity. Here is how the list for April 2009 looks like:

programming-languages1

As you can see, Java leads the rankings. It has been at the top for some years now. Since Java is being used for a large number of applications in the corporate world, you can see that there are more requirements for Java programmers than say, Python programmers.

Many graduates and wannabe programmers see all those hiring ads for Java programmers and they come to the conclusion that Java is the programming language they should go for to get a better job because it is the hot thing now. They see all these statements from elsewhere which say something to the effect of “there will be 190,872,452 requirements for Java professionals in 5 years” and they decide that once you learn Java, your life will be changed forever, for good.

Wrong!

There are a couple of things that can go against you when you select Java as your programming language. First, more requirements means more competition. If there is a requirement for a million Java programmers (I am totally making up all these numbers) there is a chance that there are a million Java programmers. You have tighter competition. Comparatively, there are fewer requirements for Python programmers, and this means that there are less number of people using python, and this means that there are less number of people who know python, and this in turn means that you have less competition. So essentially, your chances of getting a job are the same regardless of the programming language you specialize in. You may even land up on a job in Ada or Pascal (There are many big corporations which refuse to move from legacy systems).

The second thing that can go against you is that since there are a lot of qualified Java programmers, the companies that hire you can afford to pay you less. If you don’t take the job, somebody else will, and he will be equally qualified as you. Compare this to a programmer working in Ruby. If you are a Ruby programmer, there are a not much awesome Ruby programmers and companies know this and they will pay you well.

Another problem with running with the crowd is that you will not get an exciting job in programming languages like Java. Most of the interesting startups and companies that work on interesting stuff have moved away from Java and they are using programming languages like Python or Ruby to build their cool products. You want to work in those companies that build something that people use, rather than work in the under-belly of a giant corporation that churns out software like a coke factory.

Paul Graham observes:

if a company chooses to write its software in a comparatively esoteric language, they’ll be able to hire better programmers, because they’ll attract only those who cared enough to learn it. And for programmers the paradox is even more pronounced: the language to learn, if you want to get a good job, is a language that people don’t learn merely to get a job.

If you want to stand out from the crowd, you have to do stuff that stands out from what the crowd does.

If you want to do something other than spoiling your life doing the same thing a million others are doing, do yourself a favour and learn an exciting new programming language that can change the way you think and write code. Learn Python. Learn Ruby.

I say, Learn Haskell.

23 responses so far

Next »