I have two version of the same function and i am not sure which of the two i should pick. They do the same thing, but i wonder if there is a “right way” to do it or if it is only a question of aesthetics.
//both functions have to start at the second element
int multiFileHandler_1(char **files, int filenumber)
{
filenumber = filenumber - 1;
*files++;
while(filenumber--)
{
printf("%s\n", *files);
fileOpen(*files);
}
return EXIT_SUCCESS;
}
int multiFileHandler_2(char **files, int filenumber)
{
char **file;
for (file = &files[1]; *file != '\0'; file++)
{
printf("%s\n", *file);
fileOpen(*file);
}
return EXIT_SUCCESS;
}
I read have read a few texts about code readability and a few leaned towards using for since it places all the important information into one block. But the while version looks a tiny bit neater.
EDIT: Ah, stackoverflow is quite a learning experience. Shows me how much of a n00b i am. 😛
With the first loop you depend on a second argument,
filenumber, in order to iterate through the list. In the second example you use a null-terminator in order to identify the end of the list, therefore eliminating the need for a second parameter.I’d prefer the second example with the
forloop. To me it looks cleaner (although it can still be improved as others have pointed out.) Although this also means it depends on that null-terminator, and if it’s not present you have problems.