| Store | Cart

Python style guidelines

From: Andy Salnikov <a_sa...@yahoo.com>
Mon, 15 Mar 2004 13:34:58 -0800
"MetalOne" <jcb at iteris.com> wrote in message
news:92c59a2c.0403131328.54210c30 at posting.google.com...
> Good points.> With resource acquistion, I was mainly thinking along the lines of>> int file_compare(const char *filename1, const char *filename2)> {>     FILE *infile1 = fopen(filename1, "r");>     FILE *infile2 = fopen(filename2, "r");>     if (!infile1 || !infile2)>     {>         // error>         if (infile2)>             fclose(infile2);>         if (infile1)>             fclose(infile1);>         return FAILURE;>     }>>     // compare files>     return SUCCESS> }>> as opposed to>> int file_compare(const char *filename1, const char *filename2)> {>     int result = SUCCESS;>     FILE *infile1 = fopen(filename1, "r");>     if (infile1)>     {>         FILE *infile2 = fopen(filename2, "r");>         if (infile2)>         {>             // compare files>         }>         else>         {>             result = FAILURE;>         }>     }>     else>     {>         result = FAILURE;>     }>     if (infile2)>         fclose(infile2);>     if (infile1)>         fclose(infile1);>     return result;> }


  Latter version has an error - scope of infile2 is the first if _only_.
Write it like
this (and this is my preferred style too :)

int file_compare(const char *filename1, const char *filename2)
{
    int result = FAILURE;
    FILE *infile1 = fopen(filename1, "r");
    if (infile1) {
        FILE *infile2 = fopen(filename2, "r");
        if (infile2) {
            // compare files
            result = SUCCESS ; // maybe
            fclose(infile2);
        }
        fclose(infile1);
    }
    return result;
}

// Andy.

Recent Messages in this Thread
beli...@aol.com Mar 10, 2004 07:12 pm
Skip Montanaro Mar 10, 2004 10:09 pm
Will Berry Mar 11, 2004 10:39 pm
Richie Hindle Mar 12, 2004 09:36 am
Jorgen Grahn Mar 12, 2004 07:29 pm
MetalOne Mar 13, 2004 07:08 am
Heather Coppersmith Mar 13, 2004 01:22 pm
MetalOne Mar 13, 2004 09:28 pm
Andy Salnikov Mar 15, 2004 09:34 pm
Rocco Moretti Mar 17, 2004 07:22 pm
wmwd...@sneakemail.com ) (Greg Ewing (using news.cis.dfn.de) Mar 15, 2004 12:39 am
Josef Meile Mar 11, 2004 09:48 am
Peter Otten Mar 11, 2004 11:18 am
Heather Coppersmith Mar 11, 2004 11:34 am
Josef Meile Mar 11, 2004 03:39 pm
Peter Hansen Mar 11, 2004 03:41 pm
Donn Cave Mar 11, 2004 04:49 pm
John Roth Mar 11, 2004 05:16 pm
wmwd...@sneakemail.com ) (Greg Ewing (using news.cis.dfn.de) Mar 12, 2004 03:23 am
Mel Wilson Mar 12, 2004 03:57 pm
Aahz Mar 11, 2004 04:27 pm
beli...@aol.com Mar 11, 2004 12:52 pm
Michael Hudson Mar 11, 2004 01:41 pm
Messages in this thread