Wednesday, April 27, 2005

 

Dumb Code - 1

Recently, I was helping a friend debug an application debugger. In one of the report/UI modules, he had to log the date execution started + more process meta info. For date conversion, instead of using one of the inbuilt CRT functions, with the true spirit of a good (!?) programmer, he decided to implement it himself. When I saw the code, I couldn't help asking him "How many years have you been programming?"!! Check out this so called production-quality (??) code...

---- [ Code segment ] -------------------------------------------------------

#define MONTHS_SIZE 12

typedef struct __monthNoPair
{
char *strNoMonth;
char *strMonthName;
} monthNoPair;

static monthNoPair months[] = {
("1", "January"),
("2", "February"),
("3", "March"),
("4", "April"),
("5", "May"),
("6", "June"),
("7", "July"),
("8", "August"),
("9", "September"),
("10", "October"),
("11", "November"),
("12", "December")

};

char *getMonthFromData(int noMonth)
{
int i;

for (i=0; i < MONTHS_SIZE; i++)
{
if (atoi(months[i].strNoMonth) == noMonth)
return strdup(months[i].strMonthName);
}
return NULL;
}
---- [ /Code segment ] -------------------------------------------------------
Comments: Post a Comment

<< Home
Indian Bloggers Ring
[ Join | Hub | ? | << | >> ]