goto?

Started by
139 comments, last by GameDev.net 18 years, 10 months ago
Quote:Original post by aleksi1578
Well should i use goto or what to go to another place in the program?





Any professor or programming instructor who told you 'dont ever use GOTO
statements only bought into a fad.

For error processing (like jumping to a common exit point) its alot cleaner looking than the TRY/CATCH form.

For more complicated looping control the named lables can be much more understandable.

Advertisement
Quote:Original post by aleksi1578
Well I program in C++/C.. Please can you tell some other ways than goto? or any tutorials that has some of them/all of them?





If you havent already, you should become familiar with 'break' and 'continue'
statements as used withing the various loop forms.

Quote:Original post by Extrarius
Sorry but because its code for work I can't post the actual code, and I dont think I could really retype an example without violating all those contracts you have to sign to be a programmer for hire =-/


Gotchya. Think you can give a basic description of what it did? E.g. something like "resorted patient records by name"?

Quote:Anyways, like I said, coding standards require each class to have its own files, which means that no, I couldn't do as you've demonstrated.


Oh, I thought you were talking about the C++ standard (it's "the" standard in my mind), not work-specific standard. I tend to have each class in it's own header because it allows for quick reuse if that becomes desireable, but for foo_problem like positions I think it's a silly requirement to follow blindly. I'd fault the coding standard for that problem, not the code :-).

Quote:Thus, the vital part of a member function of class A has to be cut out and putt in a_loop_wrapper's file. Sure code division is a good thing, but those loops basically were the meat of the original function (so instead of func loops return, its now func a_loop_wrapper() return) AND returns are only allowed at the end of functions


Another very silly rule IMHO. Who am I to say? Better question: Who is the implementators (sp?) of the standard library on my linux box to say? Whoever they are, they seem to agree with me:

  template<typename _RandomAccessIter, typename _Predicate>    _RandomAccessIter    find_if(_RandomAccessIter __first, _RandomAccessIter __last,            _Predicate __pred,            random_access_iterator_tag)    {      typename iterator_traits<_RandomAccessIter>::difference_type __trip_count        = (__last - __first) >> 2;      for ( ; __trip_count > 0 ; --__trip_count) {        if (__pred(*__first)) return __first;        ++__first;        if (__pred(*__first)) return __first;        ++__first;        if (__pred(*__first)) return __first;        ++__first;        if (__pred(*__first)) return __first;        ++__first;      }      switch(__last - __first) {      case 3:        if (__pred(*__first)) return __first;        ++__first;      case 2:        if (__pred(*__first)) return __first;        ++__first;      case 1:        if (__pred(*__first)) return __first;        ++__first;      case 0:      default:        return __last;      }    }


Rules are made to be broken I allways say :-).
Basically the code involved comparing answers given by a person taking a test to the answers specified by a teacher, but there were many aspects to each answer (ie not simple multiple choice) and each had various tolerances depending on other aspects of the answers. It was a rather complicated grading system to put it simply (the company is a contractor, and it was required by the contract), and the function in question was the comparison part. The goto didn't actually break out of all the loops, just the innermost 3 iirc (it's been a while since I worked on this part of the project), but still goto was a much better solution than what was required by the company standards.

Personally, I find all of the standard rather silly (such as 80 chars max when the project requires us all to run at at least 1280 by something, only return at end of function, no continue/break/goto, custom hungarian-like naming convention, must refactor all functions that score a 'complexity rating' higher than a certain number with some code audit tool until they come out low enough {and it basically checks the number of branches, which have to be massively increased because of the 80 char line limit and no return rule functions are basically just if(!bfail){} if(!bfail){} etc over and over to avoid indentation), must use at least two characters in variable names because apparenly ide's can't search for identifiers only or something)
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Quote:Original post by MaulingMonkey
Quote:Original post by Extrarius
Sorry but because its code for work I can't post the actual code, and I dont think I could really retype an example without violating all those contracts you have to sign to be a programmer for hire =-/


Gotchya. Think you can give a basic description of what it did? E.g. something like "resorted patient records by name"?

Quote:Anyways, like I said, coding standards require each class to have its own files, which means that no, I couldn't do as you've demonstrated.


Oh, I thought you were talking about the C++ standard (it's "the" standard in my mind), not work-specific standard. I tend to have each class in it's own header because it allows for quick reuse if that becomes desireable, but for foo_problem like positions I think it's a silly requirement to follow blindly. I'd fault the coding standard for that problem, not the code :-).

Quote:Thus, the vital part of a member function of class A has to be cut out and putt in a_loop_wrapper's file. Sure code division is a good thing, but those loops basically were the meat of the original function (so instead of func loops return, its now func a_loop_wrapper() return) AND returns are only allowed at the end of functions


Another very silly rule IMHO. Who am I to say? Better question: Who is the implementators (sp?) of the standard library on my linux box to say? Whoever they are, they seem to agree with me:

*** Source Snippet Removed ***

Rules are made to be broken I allways say :-).



OMG!! You actually put the 'return' statements on the same line as their 'if'
statement!!!!! Truely EVIL!!!

Hasn't the programmers union told you yet that code line count must be maximized? How else will we continue to fool the suits into thinking that programming takes highly trained workers???

------------

Anyway I must not be the only one who thinks that having as much real code visible as possible on our pittifully small screens is a good thing.
(Oh for the old days with the fanfold paper printouts.)




Quote:Original post by Extrarius
Basically the code involved comparing answers given by a person taking a test to the answers specified by a teacher, but there were many aspects to each answer (ie not simple multiple choice) and each had various tolerances depending on other aspects of the answers. It was a rather complicated grading system to put it simply (the company is a contractor, and it was required by the contract), and the function in question was the comparison part. The goto didn't actually break out of all the loops, just the innermost 3 iirc (it's been a while since I worked on this part of the project), but still goto was a much better solution than what was required by the company standards.


That does sound messy :-).

Quote:Personally, I find all of the standard rather silly


Sounds like we're on the same page then. If you've got an enforced standard tying your arms behind your back, then gotos may indeed be the best solution.

Quote:Original post by Anonymous Poster
OMG!! You actually put the 'return' statements on the same line as their 'if'
statement!!!!! Truely EVIL!!!


Well, actually, that source snippet was out of /usr/include/c++/3.3/bits/stl_algo.h, which I did not write. But yes, I do place returns and throws on the same line as their if statement on occasion :-).

Quote:Anyway I must not be the only one who thinks that having as much real code visible as possible on our pittifully small screens is a good thing.
(Oh for the old days with the fanfold paper printouts.)


The last place I worked at had those old chain 132 width chain printers - and hugeass boxes of chain printer paper. Those were awesome :-).

Personally, I'm more resolution-limited. I have good eyesight, my monitor is like 19" or so and I've got it cranked up to 1600x1200 - the max. I've got an old ~17" screen which could do like 2048x1600... until windows correctly detects it, at which point it limited it to 1600x1200. I keep meaning to find a registry hack to allow the higher resolution after detection...

Also, I'd use "6pt Small Fonts" if it had a fixed character width. I've been leaning towards going back to it even though it dosn't... to make up for the lack of bold I used to set the normal "non-bold black" to a grey, and "bold black" to actual black :-).

[Edited by - MaulingMonkey on June 4, 2005 3:47:29 AM]
Quote:Original post by MaulingMonkey
[...]Sounds like we're on the same page then. If you've got an enforced standard tying your arms behind your back, then gotos may indeed be the best solution.
Except of course, that the gotos are the first thing ruled out by the standard. Personally, I think goto is cleanest of all solutions since you get to leave everything in a single coherent function.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Quote:Original post by Extrarius
Quote:Original post by MaulingMonkey
[...]Sounds like we're on the same page then. If you've got an enforced standard tying your arms behind your back, then gotos may indeed be the best solution.
Except of course, that the gotos are the first thing ruled out by the standard.


Doh! Oh well, I've still never run into a situation where I even wanted to use a goto... so I'll stand by my use of divide-and-conquer-replacing-goto-with-return stance :-p.

In other news, I'm trying out 6pt Terminal. It dosn't support bold, so I'll use my old trick of using grey for "not bold black" :-).
Quote:Original post by Extrarius
Personally, I find all of the standard rather silly (such as 80 chars max when the project requires us all to run at at least 1280 by something, only return at end of function, no continue/break/goto, custom hungarian-like naming convention, must refactor all functions that score a 'complexity rating' higher than a certain number with some code audit tool until they come out low enough {and it basically checks the number of branches, which have to be massively increased because of the 80 char line limit and no return rule functions are basically just if(!bfail){} if(!bfail){} etc over and over to avoid indentation), must use at least two characters in variable names because apparenly ide's can't search for identifiers only or something)
I don't care where you work, if I were you I would blatantly disregard those rules because they were written by someone with a case of 'rectal cranial inversion'. If I read a standard like that I would just laugh my head off and then just go and write some proper code as if I never saw it. I kid you not![lol]
I mean if they restricted you any more then you may as well be programming in BrainFuck.

They'll either never read your code anyway, or when they do they'll have no choice but to keep you on when they find out because they're too stupid to understand what continue does etc.[cool]
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Quote:Original post by Extrarius
Production code can get REALLY messy when you impose stupid limitations such as no goto/break/continue/returns(except for returns at the end of a function). It requires hideous code twice as long to avoid them, or code 10+ times as long(that takes 50 times as long to design) to make readable code that avoids them.
I bet you that it costs more too [wink].


Interesting tidbit: my multi-thousand line project uses goto once. Important to note that it dosn't use it excessivly (or even "much"), but it does still use it.

This topic is closed to new replies.

Advertisement