Ph: 277212429492

Every Flavour Beans

“The time has come…to talk of many [technologies].” –Lewis Carroll(’The Walrus and the Carpenter’)
Development Tools. Web Frameworks. GNU/Linux. Nokia N800. Video Encoding.

June 21, 2008

Don’t Let the “Smart” Way to Write the Swap Function in C++ Fool You

Filed under: C++, General — tabrez @ 11:42 am

Over the years many beginning programmers have shown me two different versions of C++ code for swapping the values of two variables. I am now starting to get tired of correcting the wrong version of the two, especially because of the disappointment that I get to see written on the face of those who thought theirs to be a very clever way of writing code. (Even if the version was correct, I don't know why they take such personal pride in the code. Obviously they have not invented the code; it has been circling the Internet or the general student community for many years, from where they have merely copied it.)

The first and the correct version for swapping values of two variables, in C++, is like this:

C++:
template<typename T>
void swap(T &a, T &b)
{
    T t = a;
    a = b;
    b = t;
}

Simple, straightforward, and correct.

The second, wrong version goes like this:

C++:
template<typename T>
void swap(T &a, T &b)
{
    a = a + b;
    b = a - b;
    a = a - b;
}

The openly asserted advantage of this version is that one variable is saved in this case. But the real, hidden attraction is that it appears to be smartly written code. If only the version was correct too. The problem with this version, of course, is that it is useless for any type that doesn't support arithmetic operations. It works fine for integer type for example, most of the times at least. It is not guaranteed to work even for floating point variables. But what if I want to swap the values of two Colour objects? Two Shape objects? The requirement that the type support addition and subtraction operations for its objects to be swappable is very absurd.

More problems with the second "smart" version:

The code fails utterly when you pass references to the same variable!
C++:
int a;
//read something into a
::swap(a, a)//BOOM! 'a' is now corrupted and beyond recovery.

The call to swap() in this case should have been a "no operation" like it will be with the first version.

The code fails even for the integers when the the sum of two passed variables exceeds the maximum integer limit on that machine. The code is also needlessly unnatural. Human beings don't swap things by indulging in addition and subtraction activities. Unless a person knows that the second version works for at least the integer type (some of the times), or verifies it manually by working through it logically or with a large number of example cases, it is difficult to just see and "get" that the code indeed does the swapping operation. The second version indeed uses one less variable than the first version but at the same time uses three additional arithmetic operations.

There are similar clever ways to swap two variables(using XOR operation, or macros for example), but all of them have one or more problems of similar kind. The best way to swap two variables is definitely the natural way to do it. By the way, parts of the above explanation apply to swapping variables in most other languages too(can't think one where it doesn't). Clever code is useless code if it isn't also correct at the same time.


If you want to receive future posts by email, enter your email address here:

Related Posts:

June 19, 2008

Wordpress Upgrading Experience With Automatic Upgrade Plugin

Filed under: Wordpress — tabrez @ 9:28 pm

Finally took the bite and tried out the upgrading process of my Wordpress blog using the Wordpress automatic upgrade plugin. Tried it directly with my main blog with not even a single test run before this.

Wordpress Automatic Upgrade allows a user to automatically upgrade the wordpress installation to the latest one provided by wordpress.org using the 5 steps provided in the wordpress upgrade instructions.
The whole process was relatively smooth and void of many surprises. I wish that the output messages from each test were accumulated together and displayed somewhere(a right sidebar?) so that it gives some structure to the upgrade progress and also keeps links to important downloads always visible.

The place the plugin broke-down was when re-activating all the plugins once the upgrade process was complete. I got the error message asking me if I am sure repeat the action. It never budged even an inch from there, so I quit it and activated all my plugins one by one manually for almost fifteen frustrating minutes. Reason? Wordpress doesn't only takes its own time but also refreshes the whole page for every plugin activation. Why not do the operation asynchronously? At least the location on the page where I am working will not be lost this way, saving me the constant scrolling.

My one complain with the Wordpress Automatic plugin is that it doesn't allow my own Wordpress archive to be uploaded and used instead of the "latest" version automatically downloaded from Wordpress.org. It's a good default value for those who can't be bothered but I would definitely like to use a Subversion Wordpress package for my blog. I have been using the svn install method for over an year and it makes the minor upgrades such a breeze that I got totally used to it. And in one fluid motion, automatic upgrade plugin replaced my svn install with the normal one before I could realise what I was going to lose.

I also realised that there is only "Deactivate All Plugins" link on the "Plugins" page where as I would like to see a "Remember Currently Active Plugins and Then Deactivate All Plugins" option so that we can restore the previously active ones with a single click.


If you want to receive future posts by email, enter your email address here:

Related Posts:

How To Create a Single-Click Link to View All Unread EMails in GMail

Filed under: General — tabrez @ 11:24 am

It was one of the requested features in my now almost two years old "GMail Wish List". I wanted one click access to all of my unread emails only. There is still no such ready-made solution in GMail but I am happy that I can at least create one now, thanks to GMail's advance search operators and the new "Quick Links" feature introduced as part of GMail Labs experimental features. Follow these five steps to create a custom link to fetch all unread emails from the GMail inbox.

Sign-in to your GMail account. Go to the Settings page and click on the Labs link.

If it is visible, you can also click the green funnel like icon sitting just to the left of "Settings" link(it is not visible when no GMail Labs feature is selected).

(If these links are not there, then you have to first enable the GMail Labs feature for your GMail account. Look for a link at the top-right corner of your GMail inbox page to do so.)

On the Labs settings page, enable the "Quick Links" feature, which should be the first feature listed on the page.
Now enter the text "in:inbox is:unread" into GMail search box as shown in the below screenshot, and click the "Search Mail" button(or press ENTER).
Find the "Quick Links" box in the left sidebar(should be somewhere below the "Contacts" and "Labels" boxes) and click the "Add Quick Link" link in it.
Enter some readable text for this custom link as shown in the screenshot below.
Your custom link is piping hot and ready now.

Just click on it whenever you want to load all the unread messages in your inbox. Alas there is no way to just drag this "Quick Links" block and place it at the top of the GMail page.

You can similarly create custom links using other search operators; refer to GMail help for more advanced search operators. These search operators can also be used to create filters and label the messages.


If you want to receive future posts by email, enter your email address here:

Related Posts:

June 18, 2008

Dreamhost Invitation Codes Give Four Times The Normal Bandwidth and Disk Space

Filed under: General — tabrez @ 1:42 pm

I got this email from th Dreamhost folks couple of weeks ago and they sent me five special invitation codes that can be used to sign-up for Dreamhost web hosting packages but get four times the normal bandwidth and hard disk space. Here is the complete text of the email:

You just got five DreamHost Invitations!

Hey Tabrez!

This email is to let you know that you, yes you, have just been given
five (5) oh so special DreamHost Invitations you can use to invite your
friends and colleagues to DreamHost!

Of course, they don't NEED an official invitation to sign up, but if you
email them and tell them to use one of these five invitation codes:





277212429492 670907616224 450568037263 134970060629600482886073

... they will get all these super special advantages not available any
other way:

* They will get four (4) times the normal disk and bandwidth!
* If they choose our five-year plan, they'll get $150 off!
* If they choose our ten-year plan, they'll get $200 off!
* YOU will still earn a full $97 for each person who signs up!

That means these five invitations are worth like, $485 in your pocket!
(Each code is good for only ONE sweet DreamHostering referral!)

Tell your invitees to use the 12-digit code you give them in the "Promo
Code" field when they sign up at:

https://signup.dreamhost.com/

So there you are. All the details are included in the quoted email text. Have a look at Dreamhost's normal shared hosting package feature set. It comes with 500GB of disk space and 5TB of bandwidth. Now multiply that with four!

The reason for not posting the invite codes here earlier is that, in my opinion, even the normal amount of bandwidth and disk space that comes with Dreamhost shared hosting package is way, way more than most users will ever need. I don't use even 2% of it. What then is the big attraction of four times of that bandwidth and disk space? If there is someone who hosts a lot of static content and this kind of huge disk space and bandwidth make sense to them, well, the invite codes are here for the grabbing:




Code 5 : 277212429492 Code 4 : 670907616224 Code 3 : 450568037263 Code 2 : 134970060629 Code 1 : 600482886073

There may be many such invite codes posted on Dreamhost forum too. Hosting service wise, Dreamhost is pretty good. Apart from the never going away ~30 minutes of downtime almost every week, I like their hosting service very much. For example, they have got very good wiki documentation. You can read reviews of it on the Internet.


If you want to receive future posts by email, enter your email address here:

Related Posts:

June 15, 2008

Creating “Hello World” Web Application Using the Grails Framework

Filed under: Groovy/Grails, Web — tabrez @ 4:04 pm

I have covered the installation procedure of Grails framework in my previous posts:
Setting Up Development Environment For Grails on Windows Vista/XP and GNU/Linux.

Just to make sure, check out again if the following environment variables are set properly on your operating system.

On Windows:
Set Grails environment variables in Windows
Set Java HOME environment variable in Windows
Set Groovy HOME environment variable in Windows
Set Grails HOME environment variable in Windows
Set Grails PATH environment variables in Windows

PATH should be set to something like(you can also check with 'echo %PATH%' on the command prompt):
c:\ruby\bin;C:\Program Files (x86)\MySQL\MySQL Server 5.0\bin;C:\Program Files (x86)\Subversion\bin;%JAVA_HOME%\bin;%GROOVY_HOME%\bin;%GRAILS_HOME%\bin

On GNU/Linux:

# echo $JAVA_HOME :: $GROOVY_HOME :: $GRAILS_HOME
/usr/lib/jvm/java-6-sun :: /home/tabrez/groovy :: /home/tabrez/grails
# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/tabrez/groovy/bin:/home/tabrez/grails/bin
Now you are all set to create Grails applications from the command line using your favourite text editor. IDE integration support for Groovy and Grails is not that great at this moment, I will write more about it in the next post. Let me just walk you through the creation of a sample Grails application from the command line.

Open the command prompt and run the following commands:

# grails create-app hello
[lot of text]
Created Grails Application at C:\Users\tabrez\grails_apps/hello
# cd hello && dir
.classpath
.project
application.properties
build.xml
grails-app
hello.launch
hello.tmproj
lib
scripts
src
test
web-app
# grails create-controller hello
[lot of text]
Created ControllerTests for Hello
Open hello/grails-app/controllers/HelloController.groovy using your favourite programming editor/IDE(e.g. SciTE, Emacs, Vim) and change its content to look like this:
Hello World program in Grails and Groovy
Run the web application and view it in the web browser.
# grails run-app
[lot of text]
Server running. Browse to http://localhost:8080/hello
2008-06-15 15:55:51.363:/hello:INFO: GSP servlet initialized

Now our application is running at the specified url. To invoke the controller that we had written earlier(Step 3), open the following url in a web browser:
http://localhost:8080/hello/hello/hello
And you should see the following output.
Grails Hello World program output in Web Browser
(If you are confused about the url: the first hello is the name of the application, the second hello is the name of the controller and the third hello is the name of the action to be invoked. In non-hello-world applications, these three will be different from each other!)

That's it. You have created and executed your first Grails web application. Next up is IDE integration.

Resources:
For more information, refer to the following resources:
1. Grails QuickStart Guide
2. Grails Tutorials and Screencasts
3. The Definitive Guide to Grails (Amazon)


If you want to receive future posts by email, enter your email address here:

Related Posts:

Next Page »

Copyright (c) 2006, 2007 Tabrez Iqbal.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. Verbatim copying and distribution of this entire article is permitted in any medium without royalty provided this notice is preserved. A copy of the license is included in the section entitled "GNU Free Documentation License".


Powered by WordPress
This website is hosted by Dreamhost


You are viewing a mobilized version of this site...
View original page here

Mobilized by Mowser Mowser
Mobilytics