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:
Post a Comment