Click here to Skip to main content
15,911,646 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: MeasureString and MeasureCharacterRanges (Page Faults) Pin
Nemanja Trifunovic1-Dec-08 9:50
Nemanja Trifunovic1-Dec-08 9:50 
GeneralRe: MeasureString and MeasureCharacterRanges (Page Faults) Pin
bob169721-Dec-08 10:30
bob169721-Dec-08 10:30 
QuestionRecive TCP packets without TX [modified] Pin
mehdir211-Dec-08 5:00
mehdir211-Dec-08 5:00 
AnswerRe: Recive TCP packets without TX Pin
Randor 1-Dec-08 6:30
professional Randor 1-Dec-08 6:30 
QuestionInstead of using nested if-else Pin
krishnakumartm1-Dec-08 4:30
krishnakumartm1-Dec-08 4:30 
AnswerRe: Instead of using nested if-else Pin
Hamid_RT1-Dec-08 4:34
Hamid_RT1-Dec-08 4:34 
GeneralRe: Instead of using nested if-else Pin
Code-o-mat1-Dec-08 5:30
Code-o-mat1-Dec-08 5:30 
GeneralRe: Instead of using nested if-else Pin
ssclaire1-Dec-08 7:55
ssclaire1-Dec-08 7:55 
The argument is "are guard clauses in methods okay?" And how do you handle resource cleanup if one of the guard clauses fail? I haven't seen a convincing consensus either way.

A specific example: Let's say you have 10 resources to allocate (like opening files, networks, etc) -- for brevity here, let's just use four resources. All resources have to be allocated for the method to continue.

So what we are trying to avoid is something like this (yuck):
isMethodSuccessful = false;        // assume this isn't going to work
resource1 = OpenMyFile(myFile);
if (resource1 == SUCCESS)
{
    resource2 = OpenSomeNetworkConnection(networkID)
    if (resource2 == OKAY)
    {
        resource3 = CreateNewFile(outputFile);
        if (resource3 == SUCCESS)
        {
            resource4 = ConnectToSomething(mySomething);
            if (resource4)
            {
                //
                // Do all my logic here
                //
                isMethodSuccessful = true;      // return success
            }
            else
            {
                 DeleteFile(resource3);    // do cleanup
                 CloseNetwork(resource2);
                 CloseFile(resource1);
             }
         }
         else
         {
              CloseNetwork(resource2);   // do cleanup
              CloseFile(resource1);
         }
     }
     else
     {
         CloseFile(resource1);    // do cleanup
     }

     return (isMethodSuccessful);
}


Guard clauses will convert that mess into something like this:
resource1 = OpenMyFile(myFile);
if (resource1 != SUCCESS)
{
    return failure;
}

resource2 = OpenSomeNetworkConnection(networkID)
if (resource2 != OKAY)
{
    CloseFile(resource1);  // do cleanup
    return failure;
}

resource3 = CreateNewFile(outputFile);
if (resource3 != SUCCESS)
{
    CloseNetwork(resource2);   // do cleanup
    CloseFile(resource1);
    return failure;
}

resource4 = ConnectToSomething(mySomething);
if (!resource4)
{
    DeleteFile(resource3);    // do cleanup
    CloseNetwork(resource2);
    CloseFile(resource1);
    return failure;
}

// 
// Put all your logic here
//

return (success);

...which is far from ideal (i.e. more yuck). So what's the best code logic in this situation?
GeneralRe: Instead of using nested if-else Pin
CPallini1-Dec-08 8:09
mveCPallini1-Dec-08 8:09 
GeneralRe: Instead of using nested if-else Pin
Nemanja Trifunovic1-Dec-08 9:18
Nemanja Trifunovic1-Dec-08 9:18 
GeneralRe: Instead of using nested if-else Pin
Timothy Baldwin2-Dec-08 12:47
Timothy Baldwin2-Dec-08 12:47 
AnswerRe: Instead of using nested if-else Pin
CPallini1-Dec-08 5:47
mveCPallini1-Dec-08 5:47 
AnswerRe: Instead of using nested if-else Pin
David Crow1-Dec-08 10:17
David Crow1-Dec-08 10:17 
AnswerRe: Instead of using nested if-else Pin
bob169721-Dec-08 16:29
bob169721-Dec-08 16:29 
AnswerRe: Instead of using nested if-else Pin
sheshidar1-Dec-08 18:15
sheshidar1-Dec-08 18:15 
QuestionUsing "extern" Pin
krishnakumartm1-Dec-08 4:05
krishnakumartm1-Dec-08 4:05 
AnswerRe: Using "extern" Pin
Code-o-mat1-Dec-08 4:08
Code-o-mat1-Dec-08 4:08 
GeneralRe: Using "extern" Pin
krishnakumartm1-Dec-08 4:17
krishnakumartm1-Dec-08 4:17 
GeneralRe: Using "extern" Pin
Code-o-mat1-Dec-08 4:53
Code-o-mat1-Dec-08 4:53 
AnswerRe: Using "extern" Pin
CPallini1-Dec-08 5:41
mveCPallini1-Dec-08 5:41 
QuestionFocus rectangle on CDialogBox button Pin
J. Landsheer1-Dec-08 3:54
J. Landsheer1-Dec-08 3:54 
QuestionRe: Focus rectangle on CDialogBox button Pin
David Crow1-Dec-08 10:22
David Crow1-Dec-08 10:22 
AnswerRe: Focus rectangle on CDialogBox button Pin
J. Landsheer2-Dec-08 3:32
J. Landsheer2-Dec-08 3:32 
AnswerRe: Focus rectangle on CDialogBox button Pin
J. Landsheer9-Dec-08 2:34
J. Landsheer9-Dec-08 2:34 
Questionhow to give gray color Pin
Member 46556851-Dec-08 2:13
Member 46556851-Dec-08 2:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.