True-life confessions of an NT-driver programmer:
Windows DDK for NT doesn''t support C++, only C. Say you need to allocate two or three objects using malloc in a function, then you need to perform 10 or 15 checks using those objects. If any check fails, all objects need to be free''d. You see this every time:
if (!some_test)
{
free (obj1);
free (obj2);
free (obj3);
return EINVAL;
}
..or, well, you suck it down and you goto freeall.
BTW, we didn''t do this. We really wanted to, but we couldn''t bring ourselves to use a goto statement in our code. So our code is (5 * 15 = ) 75 lines longer than it really needs to be, but we leave it that way.
However, it''s interesting to note that the example driver given by the DDK reference uses gotos.
(if microsoft does it, it must be ok, right?)
The EVIL goto...
You know, goto could be ok, if people would use it right. The problem is, most of the time when someone would want to use it, they''d use it WAY too much and it would screw things over a lot. But goto isn''t all that bad, if used sparingly. In fact, it can even be a little useful, in some VERY isolated circumstances. I never use it though
If you code it, they will come...
Commander M
(a.k.a. Crazy Yank)
http://commanderm.8m.com
cmndrm@commanderm.8m.com
If you code it, they will come...
Commander M
(a.k.a. Crazy Yank)
http://commanderm.8m.com
cmndrm@commanderm.8m.com
Just out of curiosity, why doesn''t VC++ allow you to skip variable initializations when you use goto?
for that guy who posted the InitializeSoundsError code example:
You could''ve made InitializeSoundsError a function. notice that as your program executes, even if no test fails, it will go right through your label and run that SoundsError portion even when there are no errors. You could easily combat that with a break or return call, but hey, doesn''t that make it more... unreadable?
hmm...
~Queasy.
You could''ve made InitializeSoundsError a function. notice that as your program executes, even if no test fails, it will go right through your label and run that SoundsError portion even when there are no errors. You could easily combat that with a break or return call, but hey, doesn''t that make it more... unreadable?
hmm...
~Queasy.
Jonathan Makqueasy gamesgate 88[email=jon.mak@utoronto.ca]email[/email]
Here''s how I''d do it, leveraging C++ to accomplish the attempted cleanly -> modifying the anonymous poster''s code.
void InitializeSounds( void ) { try { LoadSound( kSoundID1 ); LoadSound( kSoundID2 ); .. LoadSound( kSoundID23 ); } catch( OSException e ) { SoundCleanup( ); ErrorMessage( e.ToString() ); QuitClean( ); }}
This is using exception handling to avoid goto - so I guess the argument about them being similar is valid. However, try, catch and throw are much safer to use.
Give me one more medicated peaceful moment.
~ (V)^|) |<é!t|-| ~
ERROR: Your beta-version of Life1.0 has expired. Please upgrade to the full version. All important social functions will be disabled from now on.
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
Goto is an unconditional jump to anywhere it finds a label. Anyone here read Inside DirectX? Who wanted to smack the author for using goto?
Besides, goto is like marajuana, you use it once and you want to use it again, and then you want to do many other bad stylistic things to maybe save time.
-----------------------------
A wise man once said "A person with half a clue is more dangerous than a person with or without one."
Besides, goto is like marajuana, you use it once and you want to use it again, and then you want to do many other bad stylistic things to maybe save time.
-----------------------------
A wise man once said "A person with half a clue is more dangerous than a person with or without one."
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
Even though Ive never used goto, I think it''s not evil at all. It''s basically a more, LOW-LEVEL instruction that you can use to go to a certain place (like JMP in Assembly). If I were to use the goto, here''s how I''d use it...
bool LoadFile(char *filename)
{
if (!Open(filename)) goto Error;
if (!this) goto Error;
if (!that) goto Error;
return TRUE;
Error:
// ... Do unitialization stuff
return FALSE;
}
This way the function is separated in two parts. One of them is the initializing (and testing part) and the other is unitializing. This is basically what Kovach, the author of the book "The Secret Powers of Direct3D" or something used in his code. That''s more of an old-style C programming, but sometimes it''s better than all those
if (!create object 1)
{
return FALSE;
}
if (!create object 2)
{
destroy object 1;
return FALSE;
}
if (!create object 3)
{
destroy object 1;
destroy object 2;
return FALSE;
}
etc....
Well, C++ came to be, and it added the feature of exceptions, as MadKeithV showed, which basically replaces all of the above (nasty) syntax... So I don''t think goto has to be used at all in order to type less and have a good look''n code #=o) that still doesn''t make it EVIL.. it serverd its purpose as everything else in our everyday life, and now it''s obsolete... if you dont like it dont use it.. but dont tell the fellow programmer that uses it, that his/her code sucks, just because YOU think it shouldn''t be used... everyone has their own coding style and it''s always the easiest to read for them... even though you might find it messy, it''s easy to read for them and that''s why they use it...
-------------------------------
That's just my 200 bucks' worth!
..-=gLaDiAtOr=-..
bool LoadFile(char *filename)
{
if (!Open(filename)) goto Error;
if (!this) goto Error;
if (!that) goto Error;
return TRUE;
Error:
// ... Do unitialization stuff
return FALSE;
}
This way the function is separated in two parts. One of them is the initializing (and testing part) and the other is unitializing. This is basically what Kovach, the author of the book "The Secret Powers of Direct3D" or something used in his code. That''s more of an old-style C programming, but sometimes it''s better than all those
if (!create object 1)
{
return FALSE;
}
if (!create object 2)
{
destroy object 1;
return FALSE;
}
if (!create object 3)
{
destroy object 1;
destroy object 2;
return FALSE;
}
etc....
Well, C++ came to be, and it added the feature of exceptions, as MadKeithV showed, which basically replaces all of the above (nasty) syntax... So I don''t think goto has to be used at all in order to type less and have a good look''n code #=o) that still doesn''t make it EVIL.. it serverd its purpose as everything else in our everyday life, and now it''s obsolete... if you dont like it dont use it.. but dont tell the fellow programmer that uses it, that his/her code sucks, just because YOU think it shouldn''t be used... everyone has their own coding style and it''s always the easiest to read for them... even though you might find it messy, it''s easy to read for them and that''s why they use it...
-------------------------------
That's just my 200 bucks' worth!
..-=gLaDiAtOr=-..
I've never used a goto since I am a new school programmer(last 3 years) and was taught not to. I notice though reading this thread you could do a loop with a goto and a if, you can do exception handling,simulated methods and probably more. I was thinking you could make a damn functional program with just if's and goto's, but it would be hell to read. goto doesnt look so bad in the above example but if the label part is not on the page and you have to scroll up and down to find it this could be painful. Also if you use this style of coding the people you work with would probably hate you, and at my school you would most likly get a F if you turned in a program with all goto's. One of my teachers is a nice guy, he'd probably give you a C for trying though.
P.S. if goto's anything like marijuana I better not start
Edited by - Icculus on July 26, 2000 2:31:38 PM
P.S. if goto's anything like marijuana I better not start
Edited by - Icculus on July 26, 2000 2:31:38 PM
author of the Helping Phriendly Book
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement