1

Just started this problem set about a day or two ago and while implementing lookup() I was wondering if there was an alternative to using multiple if statements to compare the path and return the MIME type.

Just curious in an effort to not be copy and pasting lines of code to avoid bad design!

Currently I have the following as my lookup() implementation.

const char* lookup(const char* path)
{
    // sets a pointer to the first index in path where '.' is found
    char* dot = strchr(path, '.');
    if (dot == NULL)
    {
        return NULL;
    }

    // compares dot with file type returning MIME type
    if (strcasecmp(dot, ".html") == 0)
    {
        return "text/html";
    }
    if (strcasecmp(dot, ".css") == 0)
    {
        return "text/css";
    }
    if (strcasecmp(dot, ".gif") == 0)
    {
        return "image/gif";
    }
    if (strcasecmp(dot, ".ico") == 0)
    {
        return "image/x-icon";
    }
    if (strcasecmp(dot, ".jpg") == 0)
    {
        return "image/jpeg";
    }
    if (strcasecmp(dot, ".js") == 0)
    {
        return "text/javascript";
    }
    if (strcasecmp(dot, ".php") == 0)
    {
        return "text/x-php";
    }
    if (strcasecmp(dot, ".png") == 0)
    {
        return "image/png";
    }    
    else
    {
        return NULL;
    }
}

Any help is greatly appreciated!

2 Answers 2

2

You can refactor your code to use only one if statement!

Think about the number of MIME types you have to check for, and the number of matching file extensions.

Since there are the same number of each, you can store them in a pair of matching numerically-indexed arrays, so that the same array index will return a matching MIME type and extension.

In your array, you should store the extensions only in lowercase. Then, you can iterate through the extension array and compare your string to extension[i]. You are already using strcasecmp() for a case-insensitive string comparison. If it matches, return mimetype[i].

Using this approach you should be able to get this function down to less than 20 lines of code, including comments and opening/closing curly braces on a line all by themselves.

Hope this helps!

2
  • Now this is something I hadn't thought about. Very good! Commented Nov 3, 2016 at 1:56
  • Awesome! Will be trying to implement this after I get through the rest of the functions. Thank you for the help and response!
    – Invysti
    Commented Nov 3, 2016 at 3:29
0

It makes me uneasy too. I understand your concern. But, the truth is that when you have a lot of specific cases, you have a lot of code in general. This part is hard to factorize because the cases do not have many things in common. Specially because they don't translate in an exact way. For example, jpg ends up as jpeg, js as javascript and so on.

What you could do though is, to make them one case only. This will improve performance if only a little bit. Instead of making them a bunch of if statements make an if else if block of conditions. That way, as soon as one of them is true it won't check the other ones. Along with this technique you could also put first the most common ones. These are good practices but I don't know how necessary they are for this program in particular.

if (strcasecmp(dot, ".html") == 0)
{
    return "text/html";
}
else if (strcasecmp(dot, ".css") == 0)
{
    return "text/css";
}
else if (strcasecmp(dot, ".gif") == 0)
{
    return "image/gif";
}
else if (strcasecmp(dot, ".ico") == 0)
{
    return "image/x-icon";
}
else if (strcasecmp(dot, ".jpg") == 0)
{
    return "image/jpeg";
}
else if (strcasecmp(dot, ".js") == 0)
{
    return "text/javascript";
}
else if (strcasecmp(dot, ".php") == 0)
{
    return "text/x-php";
}
else if (strcasecmp(dot, ".png") == 0)
{
    return "image/png";
}    
else
{
    return NULL;
}

This way one, and only one, condition can evaluate to true. Which is indeed our case.

1
  • Actually, even without the else if statements, the function will exit as soon as one of the conditional tests returns true. That's how return works. So your code does not really improve anything. See my answer for a better way to refactor this. Commented Nov 2, 2016 at 20:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .