mod_rewrite

If you are using Apache then a great tool is: mod_rewrite (similar third-party tools also exist for IIS, such as: ISAPI_Rewrite). Anyway lets say you have a web-site and you reference pages like this: http://www.mysite.com/detail.php?id=20 if you notice carefully in sites like Amazon, Yahoo and MSN they almost never access a site like this. Instead you will see something like this: http://www.mysite.com/detail/20/. This looks like detail is a folder and so is 20. But that’s just mod_rewrite playing tricks on us.

To achieve this, you have to add the following two lines:

RewriteEngine ON
RewriteRule /detail/([0-9]+)/   detail.php?id=$1

That’s it! mod_rewrite works using regular expressions so the possibilities are endless. For more information check this site out: http://www.sitepoint.com/article/guide-url-rewriting/2 (needless to say the site itself uses mod_rewrite).

JavaScript Error

JavaScript is a great language and a great tool for building sophisticated web-sites. However due to it’s typeless construct it’s sometimes possible to get strange runtime errors. If you are working on a production-level system and don’t want users to see that ugly error message when they come to your site (for whatever use-case scenario) you can add these two lines to your JavaScript code:

function SupressError(sMessage, sURL, sLine){ return true; }
window.onerror = SupressError;

This works by hooking a dummy function to the window.onerror event.

Bit Operations

A friend of mine once asked me what the following code does.

register int a;
a ^= a;

After a bit of thought I realized that “a” gets initialized to zero. But why do it that way when you can do it like this:

int a;
a = 0;

The answer lies in the machine translation for each of these segments. Let’s look at it one piece at a time. The first code segment requests “a” to be part of one of the several general paramenter registers in the CPU. If that’s granted then the statement a^=a; is simple XOR edx, eax.

Compared to the second code: Which moves 0 to a register – 1 instruction does several (I would think around 2) pushl ’s whereas the first one does XOR …,… directly.

I compiled the above two programs GCC reported a net savings of 9 bytes. That’s just with one use of this technique other uses will give a greater net-savings if used properly.

Without a doubt this will affect maintenance and readibility of the code; and perhaps shouldn’t be used on production systems. But when you need to squeeze that little extra bit out so that everything fits on one frame — you have to do what you have to do!

OpenCourseWare (OCW)

MIT started something called: OCW or Open Course Ware. It’s FREE open publication of MIT course materials; starting from aeronautics to english-writing this will absolutely blow your mind away!! If you haven’t tried this, it’s absolutely a gold-mine to save (best of all they are in PDF):

http://ocw.mit.edu/

Also a direct link to all the materails.

Enjoy!

Windows XP SP2

If you are a Windows developer then be aware. Your application(s) might fail under the new Windows XP service pack scheme. Since microsoft is changing some of the defaults this might cause problems for several applications. These links should help you:

Short news
Microsoft ® Windows XP Developers’ Information

PROLOG

This semester I am taking a course in PROLOG. It has been almost 5 weeks now since I have been taking this course; and I strongly believe that every programmer should take at least one course in a logic programming class. I feel a course of this type will open the mind so greatly that it will not only help one program in other languages but help in several other aspects of one’s life.

In institutions like Caltech and MIT, PROLOG is a required course for any computer science students. Unfortunately that’s not the case in my university (@ CSUN).

A one paragraph idea behind PROLOG is the following: thinking of logic as a method of computation. In all other structured/object-oriented you cannot explicitly supply the core-logic — instead you have to find an algorithm that when followed will compute something logical. There is a vast difference between the two. They require completely different method of thinking.

One of these days I will post a sample code written in PROLOG and perhaps to compare the same program in C#.

.NET Generics

I was watching this video last night. Out of many things one feature they will introduce in Whidbey is Generics. Although this sounds like a NEW technology and it’s obvious similar sounding word: Genetics; it is far from that.

Generics is nothing but templates which has been around in C++ for several years now. Templates allow you to create a class/function that can act on several data-types at the same time.

For instance, if you wrote a class called: Adder and you wanted this function to add integer, float and strings you have 1 of two options in .NET :

1) Write three different classes: AddInteger, AddDouble, AddStrings
2) Write several overloaded functions within the class Adder
for instance:

string Add(string, string), double Add(double, double), int Add(int, int)

With generics or templates you can write one body and bind the data-type at run-time. Meaning the compiler will not know what datatype Add is going to work on until at run-time.

I will write more on this subject soon!

Coffee and Weather

I found this strange article on LA times (in the outdoors section); I thought I share it with you.

It turns out you can tell the weather by looking at your early morning coffee! Isn’t that strange? For instance if it’s going to be a HIGH day and you look down on your coffee cup the center part of the coffee will be dipped down and the bubbles will be attracted in the center (due to the curvature); on the other hand if it’s a LOW day then the center of the coffee will rise up and the surroundings will be dipped down, logically the bubbles will be attracted around the corners. This assumes that the coffee is not near any influencing object like wind.

Why this happens I am not really sure. I am tempted to guess that it has something to do with the vector (force) fields around the center-of-mass of the cup; but cannot really argue that too strongly.

Next time you are at Starbucks be sure to try this nice little trick.

CISCO VPN

I have a network of two computers. One running RedHat Linux and the other running Windows 2000 server. All of a sudden I wasn’t able to ping from RedHat -> Windows (although Windows was able to ping RedHat). I spent hours trying to fix this problem. But nothing worked! Everything looked right, the subnet masked perfectly to the network address; and more interestingly both systems were ABLE to ping the gateway (which means the path from the system to the gateway was fine)! STRANGE!

Then out of no-where I had this thought that it could be some service that I am running on my Windows machine that is blocking requests from the Linux machine; and I was right I had installed a CISCO VPN dialer which apparently installs a service called: “Cisco system Inc. VPN Service” for no logical reason this service blocks all incoming requests EVEN FROM THE same subnet. This is stupid! Specially when I am not using the VPN this shoulnd’t happen. Anyways I stopped that service and everything is well.

Page 18 of 18« First...«1415161718