Hi,
I know the pitfalls of using the goto statement, and have just this second read a comment from Ion saying he would fail his students if they used the goto statement.
However, surely there are times when it may be useful? For instance, MS use it extensively in their MFC code. I wrote a program the other day in which i experimented with the goto statement, i used it in the following way:
int MyFunction(char* Param)
{
int iReturn = 0;
HANDLE hFile = NULL;
if (!OpenFile())
goto EndFunc;
if (!GetFileSize())
goto EndFunc;
if (!ReadFile())
goto EndFunc;
if (!WriteFile())
goto EndFunc;
EndFunc:
// Close file handles in one generic place
CloseHandle();
hFile = NULL;
return iReturn; // Only 1 exit point in this function, easy to debug
}
Instead of having 5 exit points and 5 close handles in that function, the use of the goto statement meant that i only had 1 exit point, which means it is quite alot easier tracking values that get returned from the function.
MS use it in exactly the same way with their MFC code.
So what do you think?
I know the pitfalls of using the goto statement, and have just this second read a comment from Ion saying he would fail his students if they used the goto statement.
However, surely there are times when it may be useful? For instance, MS use it extensively in their MFC code. I wrote a program the other day in which i experimented with the goto statement, i used it in the following way:
int MyFunction(char* Param)
{
int iReturn = 0;
HANDLE hFile = NULL;
if (!OpenFile())
goto EndFunc;
if (!GetFileSize())
goto EndFunc;
if (!ReadFile())
goto EndFunc;
if (!WriteFile())
goto EndFunc;
EndFunc:
// Close file handles in one generic place
CloseHandle();
hFile = NULL;
return iReturn; // Only 1 exit point in this function, easy to debug
}
Instead of having 5 exit points and 5 close handles in that function, the use of the goto statement meant that i only had 1 exit point, which means it is quite alot easier tracking values that get returned from the function.
MS use it in exactly the same way with their MFC code.
So what do you think?