tronkko / dirent

C/C++ library for retrieving information on files and directories

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reconsider the need for an error code variable in two functions

elfring opened this issue · comments

How do you think about an implementation of the functions “opendir” and “_wopendir” which can work without the local variable “error” if the source code would be adjusted in the way that the desired exception handling will be achieved by using goto statements?
Would you like to avoid an extra condition check at their ends?

Interesting question! The check could also be avoided if we added another exit point. For example, in _wopendir:

                if (dirent_first (dirp)) {
                    return dirp;
                }
            }
            dirent_set_errno (ENOENT);
        }
        _wclosedir (dirp);
    }
    return NULL;
}

However, I didn't do that as I find functions with single exit point to be easier to understand.

Check and goto could also be avoided if error handling code on lines 444 and 445 were copied to else statements on lines 422, 429 and 434. I didn't like that either.

Personally, if this was just for me, I could have used goto but I was afraid of using it as some people have strong opinnions about goto. The error variable and extra check seemed to be the best of the options :)

Would you find any information from the section “Centralized exiting of functions” of the document “Linux kernel coding style” also applicable for this source file?

Thanks, it is really good document :)