Saturday, July 07, 2007

How Enumeration can Improves code readability

How Enumeration can Improves code readability

Advantage of Enumeration
Enumeration improves code readability.
It also helps in avoiding typing mistake.

Example of Poor Coding (Code without Enumeration)

int iQuizAnswer = 1;

switch(iQuizAnswer)
{
case 0:
Response.Write("Your Answer is NO");
break;
case 1:
Response.Write("Your Answer is YES");
break;
case 2:
Response.Write("You haven’t Attempt");
break;
}


Example of Enumeration (Improving Code Readability)

Declaring Enumeration
enum QuizAnswer
{
NO,
YES,
Unanswered
};


Your Code
//Creating Instance of Enumeration and Assigning QuizAnswer “Yes”
QuizAnswer myQuizAnswer = QuizAnswer.YES;

switch(myQuizAnswer)
{
case QuizAnswer.NO:
Response.Write("Your Answer is NO");
break;
case QuizAnswer.YES:
Response.Write("Your Answer is YES");
break;
case QuizAnswer.Unanswered:
Response.Write("You haven’t Attempt");
break;
}

No comments:

Most Recent Post

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape