Tuesday, April 6, 2010

A Short History of Nearly Everything

A short history of nearly everything is Bill Bryson's guide to scientific history which any layman could understand but that doesn't mean that the book is just too simple. It in fact goes on to discuss topics like Big Bang, General and special relativity, E = mc square, brownian motion, fossils, methods for finding the age of Earth etc. etc. but it's more about how masterfully Bryson has covered and explained all this. From scientific laws to personal details of scientists, it tells everything in an interesting style. And it managed to did what none of my Physics teachers could do, it made me understand Einestine's General law of relativity!

When I was reading the book, I ended up making all these charts and hierarchies of how different scientist over a certain period managed to come across a discovery and it was just too much fun to not to share it here. So here's what I have decided, using this book, I'll chart out that how a certain discovery was made and some details about the scientist involved.

I see three advantages here,

1. It will make me remember all the information.

2. You or I can always refer back to these posts whenever in a hurry.

3. It would save you the time to do all this work, you'll get to the details directly!

It will be a slow and tedious process but I know I'll enjoy it and I hope you would too.

Saying all this, if you have to read just one book in your life then make sure that it's 'A short history of nearly everything'

Sunday, March 28, 2010

Twitter OAuth Login - Part 3

1. Part 1
2. Part 2

Note : As mentioned in part 2 , we will be using Elliot Haughin's twitter library for this article. Please note that library uses codeigniter framework.

So the actual coding integration is actually consist of totally three functions or actions.

1. Create a Twitter login Button.

All you need to do is this,

<a href="index.php/login/twitterLogin" >
<img style="border: 0px none ;" src="images/connenct-twitter.jpg" />

2. Create a function to handle Twitter button click

In our above code, that function or page is, 'index.php/login/twitterLogin'. So when the user will click the link, this function will be called.

This function will redirect the user to Twitter along with sending the consumer key and consumer secret key information. Now from where to get these keys ? If you remember in the last part we registered an application with Twitter, so when Twitter registers an application, it assigns the application a consumer and consumer secret . You can get this information from the application page.


Our function will look like this

function twitterLogin()
{
try
{

$consumer_key = $this->config->item('twitter_consumer_key');
$consumer_key_secret = $this->config->item('twitter_consumer_secret');

$this->load->library('twitter');
$this->twitter->oauth($consumer_key,$consumer_key_secret);
}
catch(Exception $ex)
{
log_message('error', $ex->getMessage());
$error = 'Unable to connect to Twitter, please try after a while.';
}
}
That's all! User will be redirected to Twitter where he will be asked to authorize your application. Once the user allows or denies the access, he will be redirected back to your website. This url will be the 'callback' url you provided at the time of application registration.

3. Create a function to handle User redirection from Twitter

In this function, you will know if the user allowed the access or denied. If allowed then you will talk back to Twitter to send you access token for user. You may want to store this access token in database in order to to use it for future actions like updating his twitter status from your application.

function twitterCallBack()
{
try
{

if(isset($_GET['denied']))
{
header('Location: ' . $this->config->item('base_url'));
return;
}

$consumer_key = $this->config->item('twitter_consumer_key');
$consumer_key_secret = $this->config->item('twitter_consumer_secret');

$this->load->library('session');

$tokens['access_token'] = NULL;
$tokens['access_token_secret'] = NULL;

// GET THE ACCESS TOKENS

$oauth_tokens = $this->session->userdata('twitter_oauth_tokens');

if ( $oauth_tokens !== FALSE ) $tokens = $oauth_tokens;

$this->load->library('twitter');

$auth = $this->twitter->oauth($consumer_key, $consumer_key_secret, $tokens['access_token'], $tokens['access_token_secret']);

if ( isset($auth['access_token']) && isset($auth['access_token_secret']) )
{
// SAVE THE ACCESS TOKENS

$this->session->set_userdata('twitter_oauth_tokens', $auth);

if ( isset($_GET['oauth_token']) )
{
$uri = $_SERVER['REQUEST_URI'];
$parts = explode('?', $uri);

// Now we redirect the user since we've saved their stuff!

header('Location: '.$parts[0]);
return;
}
}

header('Location: ' . $this->config->item('base_url').'index.php/dashboard');
}
catch(Exception $ex)
{
log_message('error', $ex->getMessage());

}

}
...And you are done!

Friday, March 26, 2010

Twitter OAuth Login - Part 2

The part 1 of this post can be checked here

1. First Step : Get a Twitter Account


If you already have a twitter account then you can ignore it else you need to create one at twitter.

2. Second Step : Register your application with Twitter

a. Log in to Twitter.
b. On the top menu, click 'settings'
c. On the settings page, you will see another menu, click 'connections.'
d. On the right hand, you will see a heading 'Developers' with a link to manage your application, click it.

e. You will be redirected to a page with a link to 'register new application,' click it.

f. Fill in the application registration form, it asks for some general information about your website like name and website url. Some important fields you need to fill in correctly are,
  • Call back URL - That's the page user will be redirected to once he will provide twitter his authentication details. This url will eventually be talking back to Twitter in order to get the access token for the user.
  • Application Type : Select 'browser.' 'Client' in other words desktop applications are out of the scope of this document.
  • Default Access Type : If you want to allow user to update his Twitter status right from your application then select 'Read & Write' else just 'Read.'
  • Use Twitter for login : Select it.



g. Once the form has been filled, submit it. If the application has been registered successfully, it will start appearing on Manage Applications page.

3. Third Step : Integrate it into your website

In order to integrate Twitter, you need a Twitter library with OAuth support. You may decide to write everything from scratch but hopefully you wouldn't want to reinvent the wheel. The one that I used is written by 'Elliot Haughin' and you can download it from here : http://www.haughin.com/code/twitter/

He developed it mainly for codeigniter framework but it would work just fine if used otherwise, you may just have to replace some codeigniter keywords. If you want to use a non codeigniter library then check out this one : http://github.com/abraham/twitteroauth

I would be using Elliot's library in rest of the article.

The next post will be the last in the series and would be discussing the code we need to write.

1. Part 1
2. Part 2
3. Part 3

Thursday, March 25, 2010

Twitter OAuth Login - Part 1

You must have noticed that lately a lot of sites have started to use login via Twitter and you migt already have used a screen like the following,



So that's what we will be discussing today, that's how to integrate Twitter login.

Why should we go for login via Twitter (or for that matter any other service like FaceBook)?

Login using a popular third party website is a good option both for the site and the user. User wouldn't have to go through the pain of entering all the sign up details and thus site wouldn't loose a user just because he was too lazy to fill in the sign up information. But offcourse no user wouldn't want to give their Twitter login credentials to just about any site who may end up storing your details in their database and misuse it or their database get hacked. Twitter understands it and therefore supports OAuth.

So what's OAuth and how does this address this issue?
If you're storing protected data on your users' behalf, they shouldn't be spreading their passwords around the web to get access to it. Use OAuth to give your users access to their data while protecting their account credentials.
If you want to understand the A-Z of how OAuth works, go here : http://www.oauth.net/core/1.0/

Let me give you a short summary in plain English. In order to understand how OAuth works, let's define two terminologies in terms of integrating Twitter login,

Consumer - This is the site which wants to integrate Twitter login.
Service provider - Twitter.

Here's the OAuth cycle.

1. Consumer sends a request token to Service provider.

2. Service provider grants a request token. This token is mainly to authorize the consumer using an id and secret key combination. It would be use to get Access token for user.

3. End user gets redirected to service provider's website.

4. User authorizes and gets redirected back to the consumer site

4. Consumer site requests service provider for a access token.

5. Service provider grants the token.

6. User is redirected to protected site pages.

So how could we integrate Twitter login to our website ?

Twitter is a good service but when it comes to API it's quite a pain mainly because it doesn't really have an official library, so you are left with two options: either to build one yourself or try to find a good an reliable library on the net. Twitter suggests some libraries but they aren't official. Secondly Twitter OAuth login is still a ...... beta! And beta services mean only one thing : they can change anytime! But as everybody is using it these days so I hope Twitter will not make any ground breaking changes.

So this post was to give you all the introduction. In next post we will be discussing the actual integration.

1. Part 2
2. Part 3

Codeigniter

I have some experience with cakePHP and I eventually get to play with code igniter - another php framework and I think it's just too brilliant!

It's faster, flexible and much easier than cakePHP but the best part is their detailed guide, just about everything is in there. A codeigniter site is installed as soon as you copy and paste it and writing it's hello world program hardly takes 30 seconds. Learning it is not really a dedicated job, you could learn it on the go thanks to it's extremely well written guide.

Most of the helper functions like emails engine is already there and you don't really have to install any pear library. Unlike cakePHP it is much optimized, eg. it supports persistent db connection by default while cakePHP's default option is non persistent.

So if you are into php coding, this is one framework which you must check : http://codeigniter.com/downloads/

Monday, December 28, 2009

How to be a better programmer

Most of us think that we are very good developers but honestly speaking, it's quite a bit other way round. It's very common to see following piece of code,

bool myFlag;
if(MyFunction() == true)
{
myFlag = true;
}
else
{
myFlag = false;
}
If you write this kind of code then.....you obviously need help some hard work. If you don't and you are actually a decent developer, even then there is always room for improvement. So here's a guide for all those who believe in improving themselves.

Just like any other craft, there is no shortcut to becoming a better programmer, it's all good old classic hard work. Here are some tips which I have gathered over the years and which many other top coders recommend,

1. Read the Code

Read , read and read the code you write. Just like a normal essay writing, code gets better when you review it over and over. Reading the code helps in understanding the overall structure and catching errors which may come during run time. It infact directly affects your unit testing time as chances are that you will pass the testing in first attempt. My personal experience says that reading the code makes me catch errors which would otherwise be very tough to be produced by normal QA testing, mostly because of complex scenarios.

2. Read the Code....that's others Code

Going everyday to your job and writing code all day long doesn't make you a better developer. You would have to see what others are doing. Take up others code, that's of people who have a good reputation as a programmer and read their code. Internet is full of open sourced projects, many companies have opened the source code of many of their products, pick any product that you might have enjoyed using or about which you might have wondered that exactly how it was done and just read the code and play with it. This will teach what smart techniques people are using.

Apart from that, reading others code is an art which isn't that common. Some developers say that it's a gift. If you disagree with me then recall the last time when your manager tried to give you an existing code base and asked to change one of the major functions, chances are that it wasn't your favorite assignment. I believe, the fear of reading others code is more to do with the fear of unknown than anything else. So kill that fear of yours by reading codes of other developers including your colleagues, but just for the sake of keeping your social ranking intact, don't be too emphatic in terms of pointing their mistakes or rather don't point them at all unless you think that it's critical to the project.

3. Join an open source project

By joining an open source project you can get to experience many things very early in your career such as participating in more than one phase of SDLC, direct interaction with the client that's end user in this case, and a coding approach of your choice that's you don't really have to follow the red tapped process of the company. You could also see the results of your efforts much more quickly than normal long term projects which usually come software houses way. Overall open source project not only gives you first hand experience in terms of working with a team with a more active role than a normal junior or mid level developer and it also helps you in getting your communication skills fine tuned and understand end users requirements better.

You may say that it's not important to get to know end user in order to sharpen coding skills, but it in fact gives another perspective to your work because now you also have an idea of how the users are using your applications and what other future expansions are expected.

4. Learn more than one language

I can't emphasize more that how important it is to know more than one language. And if you are a computer science graduate then learning a new language should be second to your nature. Knowing more than one language gives you a new perspective in terms of approaching a problem and figuring out a solution. Every language has it's own constructs but it's always possible that a concept of Java can be used in some form in Dot Net. Similarly, your ASP.Net will definitely see a boast if you know how the underlying response/request process works.

So don't make the mistake of sticking to one language, try to keep your language profile diverse.

5. Basics first

If the only data structure you use is ArrayList or Hashtable then shame on you (seriously!). Keep your knowledge of basic data structures always updated, using a B+ tree instead of a binary tree might make all the difference in the performance of your algorithm but in order to do that you need to know what exactly B+ tree is. Similarly it's also good to know basic algorithms.

With the ultra fast machines we have these days, it's not really critical to have highly optimized code and any efforts in that regard would eventually make it difficult for you to maintain the code. But you still would have to keep a check e.g. O(N square) is definitely something to avoid. So you should be able to do some basic code profiling.

Enjoy programming!

Visual Studio 2010 and .NET Framework 4 Beta period extended

Dr. Somasegar, senior vice president of Microsoft recently mentioned on his blog that the release of VS 2010 has been moved back by a few weeks, as they are focusing on including user feedback after Beta 2 release. The fact is that VS IDE consumes alot of memory and has a habit of getting slow which kills the whole purpose of the IDE that's making programmers productive. And this is pretty much the problem users faced with VS 2010. The virtual memory usage has a lot of problems (IDE crashes are an evidence in that direction) and that's what Microsoft wants to look at and fix. I think it's better to delay the release and get it right than meet the deadline and push over the shit to the poor programmers.

Here's the note,
At the same time, you have also given us feedback around performance issues, specifically in a few key scenarios including virtual memory usage. As you may have seen, we significantly improved performance between Beta 1 and Beta 2. Based on what we’ve heard, we clearly needed to do more work. Over the last couple of months, our engineering team has been doing a push to improve performance. We have made significant progress in this space since Beta 2.

With these improvements in the product, we do want to make sure that they truly address the performance issues while continuing to maintain a high quality bar. As a result, we are going to extend the beta period by adding another interim checkpoint release, a Release Candidate with a broad “go live” license, which will be publicly available in the February 2010 timeframe.

Since the goal of the Release Candidate is to get more feedback from you, the team will need some time to react to that feedback before creating the final release build. We are therefore moving the launch of Visual Studio 2010 and .NET Framework 4 back a few weeks.
Link to Post : Visual Studio 2010 and .NET Framework 4 Beta period extended