I really think you intend to use *pad in your code and not simply pad.
I would not recommend setting errno. It's not necessary.
Here is how I would refactor it, no guarantees. In the end you (or
your coworkers) should feel comfortable with what you choose.
int setupDirectoryPad(WINDOW ** pad, const char * dirName)
{
DIR * dir = NULL;
struct dirent * entry = NULL;
size_t width = 0;
size_t height = 0;
*pad = NULL;
if (NULL != (dir = opendir(dirName)))
{
while (NULL != (entry = readdir(dir)))
{
size_t temp = strlen(entry->d_name);
if (temp > width)
width = temp;
++height;
}
if (errno == 0)
{
rewinddir(dir);
if (NULL != (*pad = newpad()))
{
while (NULL != (entry = readdir(dir)))
;
if (errno != 0)
perror("Error iterating directory");
}
else
perror("Error creating directory pad");
}
else
perror("Error iterating directory");
if (0 != closedir(dir))
perror("Failed to close directory");
}
else
perror("Failed to open directory");
return errno;
}
On Wed, Jan 20, 2010 at 10:51 AM, Alberto Treviño <[email protected]> wrote:
> On Wednesday 20 January 2010 11:40:38 am Daniel Dilts wrote:
>> The problem is that this requires the cleanup to be done in each if
>> block. Avoiding that is one of the main purposes of the
>> do{}while(false) block. Note that the DIR * has to be released if it
>> has been created successfully.
>
> Are we talking about readability or reducing the number of lines of code and
> minimizing bugs? Yes, all three are related, but there is always give and
> take.
>
> --
> Alberto Treviño
> BYU Testing Center
> Brigham Young University
> --------------------
> BYU Unix Users Group
> http://uug.byu.edu/
>
> The opinions expressed in this message are the responsibility of their
> author. They are not endorsed by BYU, the BYU CS Department or BYU-UUG.
> ___________________________________________________________________
> List Info (unsubscribe here): http://uug.byu.edu/mailman/listinfo/uug-list
>
--------------------
BYU Unix Users Group
http://uug.byu.edu/
The opinions expressed in this message are the responsibility of their
author. They are not endorsed by BYU, the BYU CS Department or BYU-UUG.
___________________________________________________________________
List Info (unsubscribe here): http://uug.byu.edu/mailman/listinfo/uug-list