Sunday, August 22, 2010

Reading cross domain RSS feeds (AJAX)

If you want to read an RSS feed from a different domain, let us say you want to read an RSS feed from http://digg.com, then here is a simple example using Google AJAX Feed API.

http://digg.com RSS feed link is http://feeds.digg.com/digg/popular.rss.

Javascript example
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script>
var RSSURL="http://feeds.digg.com/digg/popular.rss";
var DIGGURL="http://digg.com/";
function getFeed()
{
var strHTML='';
var feed = new google.feeds.Feed(RSSURL);
feed.load(function(result) {
if (!result.error) {    
    var length=result.feed.entries.length;
    for (var i = 0; i < length; i++) {
        var entry = result.feed.entries[i];
        strHTML+='';
        strHTML+='<a href="'+entry.link+'" target="_blank">'+entry.title+'</a><br/>';
        strHTML+=entry.publishedDate+'<br/>';
        strHTML+='<div>';
        strHTML+=entry.contentSnippet+'<br/>';
        strHTML+='</div>';
        strHTML+='<br/>';
    }
    strHTML+='<div align="right"><a href="'+DIGGURL+'" target="_blank">more...</a></div>';
    document.getElementById("digg").innerHTML=strHTML;
}
});
}
</script>
<div id="digg"></div>
<script>
    google.load("feeds", "1");
    google.setOnLoadCallback(getFeed);
</script>

Note: As the Google AJAX Feed API uses Feedfetcher, feed data from the AJAX Feed API may not always be up to date. The Google feed crawler ("Feedfetcher") retrieves feeds from most sites less than once every hour. Some frequently updated sites may be refreshed more often.

Wednesday, August 18, 2010

Naming convention

There are no hard and fast rules as far as naming conventions are concerned, but it has to be consistent at the individual, team and organization level.

The name of the folder, project, file, namespace, class, and function have to be meaningful. The variable (data members, local members or function arguments) must have the following information as part of their name
  • modifier (private, public, etc.) and scope of the variable (member to the class (data member), member to the function (local variable), constant, static)
  • datatype of the variable
  • meaningful name
Along with the above mentioned points you can also add comments where ever necessary. Also indentation of the code would improve the readability.

C# 2.0 example- Below is the Employee.cs file.
using System;
using System.Collections.Generic;
using System.Text;
namespace Sample_v2
{
    class Employee
    {
        private string m_strName=String.Empty;
        private int m_iEmpId=-1;
        public Employee(string strName, int iEmpId)
        {
            m_strName = strName;
            m_iEmpId = iEmpId;
        }
        public string Name
        {
            get
            {
                return m_strName;
            }
            private set { m_strName = value; }
        }
        public int EmpId
        {
            get
            {
                return m_iEmpId;
            }
            private set { m_iEmpId = value; }

        }
        //should not allow empty string or null string
        public bool SetNameWithValidation(string strName)
        {
            bool blnStatus = false;
            if (strName != string.Empty && strName != null)
            {
                m_strName = strName;
                blnStatus = true;
            }
            return blnStatus;
        }
        public override string ToString()
        {
            return string.Format("Name:{0},EmpId:{1}",
                m_strName, m_iEmpId);
        }

    }

}

C# 3.0 example- Below is the Employee.cs file.
using System;
using System.Collections.Generic;
using System.Text;
namespace Sample_v3
{
    class Employee
    {
        public string Name { get; set; }
        public int EmpId { get; set; }
        public Employee()
        {

        }
        //should not allow empty string or null string
        public bool SetName(string strName)
        {
            bool blnStatus = false;
            if (strName != string.Empty && strName != null)
            {
                Name = strName;
                blnStatus = true;
            }
            return blnStatus;
        }
        public override string ToString()
        {
            return string.Format("Name:{0},EmpId:{1}",
                Name,
                EmpId);
        }
    }
}

Note: Alternatively you can follow the naming standard followed by the respective technology (.Net, J2SE, JavasScript, etc). Intellisense feature of the respective integrated development environment (IDE) could help you on this.

Rule: Follow consistent naming convention.

Rule: If you not clear on the final name of the product, module, etc consider using a meaning less code name (e.g. "code123").

OpenID Authentication

If you are developing a web application for internet and your web hosting plan does not support SSL and want to support user Sign Up and Sign In, then consider using OpenID authentication mechanism.

Check this URL Federated Login for Google Account Users.

Using Janrain Engage you can build OpenID Sing In module with in few minutes.

There is another quick and easy OpenID library is Dope OpenID.