Sunday, April 29, 2007

Firefox is recommended browser

Videohybrid displays this message when you visit them on a browser other than Firefox. I have come across numerous websites that are the other way around. Never have I seen anyone put up a banner like this in support of Firefox. Amazing!

If you have ever read my previous posts, you know I am a big Firefox fanatic. There is so much value added stuff in Firefox that is glaringly absent in IE7. But If you are using IE7, this Find As You Type extension is a MUST HAVE and takes a lot of the pain off your surfing experience.

My browser right now is Grand Paradiso Alpha 1. Contributing my 2 cents to the Firefox community with user testing and crash reporting.

If you are wondering what Videohybrid is, it is one of those next generation video aggregators that make it easy to find you favorite shows, movies. It is illegal though as it streams videos from other websites that house illegal content like dailymotion. A post on techcrunch made it highly visible and brought with it unprecedented load and traffic that has seen the site go down more than once.

The point here is, I am happy to see such a banner, but would truly love to see a day where all browsers follow and implement common standards. Well, Thats still a long way away, made even harder by IE's majority share and Microsoft's own way of doing things.

Have you tried Coke Zero yet?


I am one of those guys who needs a constant supply of coke to keep going. I used to gulp down as many as 8-9 a day. I ABSOLUTELY love the taste !. But over time, all those calories and carbonated water started making me feel stomach heavy.

I unsuccessfully tried switching to diet coke or diet pepsi to at least cut down on the calorie intake. Ho diet coke is horrible and diet pepsi I can do with, but not for long. So I found myself coming back to coke again and again. Something in it.. the taste .. may be the caffeine.

With some restraint, I can now get away with 2 a day. Trident has really been helpful in this regard. I realized chewing Trident brings down my urge to grab myself a coke. So I find myself chewing Trident all day, spitting it out only to have lunch.

One of these days I watched the sue-coke-zero-for-taste-infringement commercials. They are really funny. Given the diet coke experience, I wasn't really looking forward to a rich taste...but to my surprise it was real close to coke classic. Well, you can't make it taste exactly like coke classic when it has only zero calories. It was close enough that I decided to give it a chance. So I have been training myself to Coke Zero and am officially switching from Coke Classic to Coke Zero, AT LAST.

Eventually, I would love to get away from the urge to drink coke anymore. So If you are looking for Coke-ness without the calories, Coke Zero is worth a shot.

Off to another Coke Zero and wilfing !

Saturday, April 07, 2007

SQL Short-circuit

This post looks at short-circuiting in SQL Server 2000/2005 and its caveats.

Consider a simple product search scenario. If the user enters something in the search box, you want to retrieve product results pertaining to the search text, otherwise you want to bring in all the products, effectively ignoring the parameter.Ideally you will page the results.

This is a typical example of Optional Parameters, requiring a Conditional Where Clause in your translation to SQL.

A simple SQL statement fails to capture this essential part of the problem domain and there seems to be no easy way to accomplish this. Most of the developers resort to using dynamic SQL, table variables with joins, If expressions or CASE Statements to accomplish this. Short-circuiting can come in handy in these situations and gives a performance boost that is worth investigating.

To illustrate that SQL does indeed support this feature, execute the statement below as indicated by Mark Cohen on his blog.

Select 1 Where 1=1 or 1/0=0

We indeed don't get a divide by zero exception reinforcing our claim that SQL Server does have short-circuiting support.

As Jeff points out, many of the CASE expressions can be converted into boolean logic and hence take advantage of short-circuiting.

Assume @CustomerID = -1 is the default value, indicating that nothing was passed in. Optional Parameters would generally be coded as one of these

Exec sp_executesql @YourDynamicStatement

(Or)
If @CustomerID = -1
Select * from Sales.Customer
Else
Select * from Sales.Customer Where CustomerID = @CustomerID

(Or)
Select * from Customer
Where
Case @CustomerID
When -1 Then 1
Else
Case When @CustomerID = CustomerID Then 1 Else 0 End
End = 1

(Or)
Select * from Customer
Where CustomerID =
Case
When @CustomerID = -1 Then CustomerID
Else @CustomerID
End

The equivalent boolean logic(with short-circuit) would be as below Where (@CustomerID = -1 or CustomerID = @CustomerID)

When @CustomerID = -1 , indicating that nothing is passed in, the right side expression is never evaluated.

As you can see, this is easily readable as well as maintainable.

Now if you look at the comments in Jeff's blog, a user complains that his short circuit doesn't work.
Select * from Northwind..Orders
Where CustomerID = CustomerID and OrderID > 1/(0*year(getdate()))

You would expect this statement to not generate a Divide By Zero exception, but it does. So Whats wrong here.

We found out, the short circuit works only if the expression is DETERMINISTIC. That is, if the engine can look at the expression and determine its truth value without having to run the query, then the engine short-circuits the statement, effectively ignoring the entire expression.

Eg.; The truth value of @CustomerId = -1 can be determined before hand and hence is deterministic.Similary are
Select 1 Where getdate()=getdate() and 1/0=0

Select 1 Where 1=1 or 1/0=0

Select 1 Where 1=1/0 or 1=1

So as long as the expression is deterministic (truth value can be determined), you can take advantage of short-circuiting.

Though CustomerID = CustomerID seems deterministic in a fleeting glance, it is NOT DETERMINISTIC because in SQL by default null IS NOT EQUAL to null. So the engine cannot determine before hand the value of the left side expression and hence cannot short-circuit and fails.

So the next time you are doing Conditional Where clauses, convert the condition into boolean logic and take advantage of short-circuiting built into SQL Server.

Layman Web 2.0 Video

Micheal Wesch at Kanas State University created this amazing layman Web 2.0 video The Machine is Us/ing Us.



It illustrates the concepts that are shaping today's web in a surprisingly simple way . And to think that he is a cultural anthropology professor amazes me. XML, RSS, Content sharing, Tagging, Social Bookmarking, the idea that the machine is learning a new idea with every click and the creation of a database-backed web are all showcased.

My favorite part is when content from disparate sources seamless blends into the sections of the page where dropped. Drives home the powerful concept of form and content separation.

There is nothing geeky about the video. Must watch for everybody. Repeat after me : We are the Web