Archive for the 'Programming' category

ReImages – Reload images in web pages

Aug 28 2010 Published by Niyaz PK under Programming

This is a bookmarklet to reload the images in any web page without reloading the web page itself.

ReImages

(Drag the above link to your bookmarks bar or right click and add to your bookmarks.)

You can click the link whenever you want to reload pages in any web page without reloading the page itself. This bookmarklet reloads images in <img> tag as well as background images for any element in the page.

This can be very useful in many cases:

  1. During web development (especially when you are working on an image that appears only after you do some action in the page, like on a pop-up).
  2. When some server (or a stupid firewall or some other device)  serves you old cached version of the images.
  3. When a large image in a page did not load completely and when you try to refresh the page it is still loading the broken version because it gets the image from the browser cache

etc.

This is the initial version. Let us call it v0.9.

I have been using it for a while without any issues, but let me know of any bugs you come across.

If you are a web developer, take a look at the wonderful ReCSS too.

No responses yet

What programming language should I learn?

Mar 02 2010 Published by Niyaz PK under Programming

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!

13 responses so far

Wanted: Superman

Feb 08 2010 Published by Niyaz PK under Programming

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.

4 responses so far

The perfect compression algorithm

Jan 09 2010 Published by Niyaz PK under Math, Programming

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.

10 responses so far

Get cached images from your visitors

Dec 12 2009 Published by Niyaz PK under Internet, Programming

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.]

22 responses so far

Recursion & bad examples

Nov 07 2009 Published by Niyaz PK under Programming

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 Published by Niyaz PK under Programming

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.

5 responses so far

Older posts »