Replacement For GoTo lazarus / 21-General ---------------------------------------------------------------------- Weitentaaal · 2021-02-03 12:13 Hello Guys Got this Code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure Suche_Gegentauscher(InZlAl: Integer);Var....Begin....   GeraetGefunden:...//Some IF's and a Loop and in it there is a Call like :GoTo GeraetGefunden;...//Some If's and Loop's later next CallGoTo GeraetGefunden; ...GoTo GeraetGefunden; //End of Code is an If and else... in the else block is next CallGoTo GeraetGefunden;SQLTrans.CommitRetaining;   and now i'm asking myself how to eliminate the Goto, bc i don't want to use it. I thought about a recursive Function bc the goto is only at the beginning. Are there any other, better Ways ? ---------------------------------------------------------------------- Bart · 2021-02-03 12:18 That rather depends on how spaghettified the code is. If you do a Goto inside one of the loops, it may get very complicated. When rewriting things like this, you better rethink (and may writ out) the actual algorithm that you are trying to implement. Bart ---------------------------------------------------------------------- lucamar · 2021-02-03 12:22 Depends quite a lot on what your code is like and what it does but rethinking the code with a judicious use of while and/or repeat loops coupled with break/continue if needed usually works wonders. ---------------------------------------------------------------------- Weitentaaal · 2021-02-03 12:29 Ok Thanks Guys ---------------------------------------------------------------------- Kays · 2021-02-03 12:37 Quote from: Weitentaaal on February 03, 2021, 01:13:58 pm[…] now i'm asking myself how to eliminate the Goto, bc i don't want to use it. […]Well, like the previous posters, there’s just no snakeoil recipe. Just spitballing here: You can use exceptions, throw an exception when the device’s been found. It’s just really funky, cos exceptions are usually used to convey error conditions, not intentional messages, but it still serves the purpose (i.e. avoiding evil goto). This is just a minimal effort solution, I’d rather stick to clean/traditional programming. EDIT: I’m sorry, I just see you’re jumping up, not down. My “idea” is only feasible if you can arrange your code the other way round. ---------------------------------------------------------------------- MarkMLl · 2021-02-03 13:00 I've seen break/continue derided as "goto in disguise". It's worth remembering that in his later years Dijkstra- who was responsible for the "considered harmful" meme- was slightly dismayed that some people enforced it with religious fervour. Goto has its uses as a strictly-local construct. SetJmp()/LongJmp() has its uses, but has to a very large extent been eclipsed by exceptions. MarkMLl ---------------------------------------------------------------------- Sieben · 2021-02-03 13:19 I guess that below the label 'GeraetGefunden' there is some code doing sth with that device. Put that code into a proc or function and call that instead of GoTo. Just for a start. ---------------------------------------------------------------------- lucamar · 2021-02-03 13:23 Quote from: MarkMLl on February 03, 2021, 02:00:24 pmI've seen break/continue derided as "goto in disguise". I say it frequently (though not in a deriding way), whenever the "never ever use goto" gang starts preaching. It's worth noting it: they are goto-replacements for very specific conditions, which just happen to be close to the (IIRC) 90% legitimate use of goto*. * I read somewhere (in some book, I think) that about the "90% (legitimate) gotos are for ..." but I don't remember where. ---------------------------------------------------------------------- mangakissa · 2021-02-03 14:58 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure Suche_Gegentauscher(InZlAl: Integer);Var.... procedure GeraetGefunden;begin end;  Begin....   GeraetGefunden:...//Some IF's and a Loop and in it there is a Call like :GeraetGefunden;...//Some If's and Loop's later next CallGeraetGefunden; ...GeraetGefunden; //End of Code is an If and else... in the else block is next CallGeraetGefunden;SQLTrans.CommitRetaining; ---------------------------------------------------------------------- rvk · 2021-02-03 15:17 Quote from: mangakissa on February 03, 2021, 03:58:47 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  Not exactly what was asked for. In your code only the code in GeraetGefunden is executed multiple times. In the code of TS, ALL the "Some IF's and a Loop" are again evaluated after the GoTo. In your code the first "Some IF's and a Loop" is only evaluated once. It really depends on how the IF's and loops look as to how you would need to re-code this. ---------------------------------------------------------------------- BrunoK · 2021-02-03 16:32 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure Suche_Gegentauscher(InZlAl: integer);{Var....}begin  {....}  repeat  {....}    //Some IF's and a Loop and in it there is a Call like :    continue;    {....}    //Some If's and Loop's later next Call    continue;    {....}    continue;     //End of Code is an If and else{....} in the else block is next Call    continue;    SQLTrans.CommitRetaining;    break;  until False;end; ---------------------------------------------------------------------- MarkMLl · 2021-02-03 17:37 @BrunoK I generally agree with your approach. Apart from anything else, having a simple Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if something then begin  do_something;  continue // or break as appropriateend;  as a functional unit eliminates what can become a ridiculously deep if-then-else structure. of course, another approach is to put the decision in a function returning an enumeration, and then apply that enumeration to a case statement. But sometimes it's easier to keep everything together. MarkMLl ---------------------------------------------------------------------- PascalDragon · 2021-02-04 08:08 Quote from: MarkMLl on February 03, 2021, 02:00:24 pmGoto has its uses as a strictly-local construct. SetJmp()/LongJmp() has its uses, but has to a very large extent been eclipsed by exceptions. The exception mechanism inside FPC uses SetJmp/LongJmp if no platform specific mechanism like SEH or POSIX EH is used. ---------------------------------------------------------------------- dseligo · 2021-02-04 09:31 I would make another function inside procedure Suche_Gegentauscher and call it repeatedly until it's finished. Something like that: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure Suche_Gegentauscher(InZlAl: Integer);Var  bDone:Boolean;....   function Suche_Gegentauscher_work:Boolean;  Begin    Result:=False;  ....       GeraetGefunden:    ...    //Some IF's and a Loop and in it there is a Call like :    Exit; // GoTo GeraetGefunden;    ...    //Some If's and Loop's later next Call    Exit; // GoTo GeraetGefunden;     ...    Exit; // GoTo GeraetGefunden;     //End of Code is an If and else... in the else block is next Call    Exit; // GoTo GeraetGefunden;    SQLTrans.CommitRetaining;        ...    Result:=True;  end;  Begin  repeat    bDone:=Suche_Gegentauscher_work;  until bDone;End; ---------------------------------------------------------------------- Mr.Madguy · 2021-02-04 10:47 My first language was Basic. Pure Basic, not QB. And it couldn't be used without gotos. But since that moment, when I switched to Pascal, I've never used goto. It can always be avoided. ---------------------------------------------------------------------- lucamar · 2021-02-04 12:45 Quote from: Mr.Madguy on February 04, 2021, 11:47:41 am[...] But since that moment, when I switched to Pascal, I've never used goto. It can always be avoided. Yes, maybe; but there are times when a judiciously used "goto" can help simplify an algorithm enormously. Granted, it happens very, very few times (I might have used about three gotos in Pascal in all my life ), but it happens. And let's not forget what was said above: each time you use break, continue, exit and alike you're really using a "goto", albeit a specialized one. ---------------------------------------------------------------------- Peter H · 2021-02-04 13:26 Gotos are a good replacement for exceptions if "LastError" and this stuff has to be used a lot. In "C" I had the habit, my procedures had three sections: Prepare dowork and cleanup. Prepare:Open Files and so on Dowork: Process stuff Cleanup: Close files and so on. So when there was an error, e.g. unexpected EOF,  I handled it and jumped to "cleanup". This habit was broken when I had to use Turbo Pascal. ---------------------------------------------------------------------- Zvoni · 2021-02-04 13:58 I remember translating C-Code as an excersize, and i came across stuff like this Aircode Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i //A While LoopWhile ACondition{//DoSomethingIf AnotherCondition Then Goto CleanUp;}  While SomethingElse{//DoSomethingIf SomeCondition Then Goto Cleanup} CleanUp:DoCleanup;  I "solved" it with a nested Function which always returns True The If-condition above read something like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i If SomeCondition Then If CleanUp Then Exit; ---------------------------------------------------------------------- Peter H · 2021-02-04 15:22 I think, when my program detects an error, then it has necessaryly the best information about the error. So the error should be handled at the location where it was detected, e.g. if gotbaddata then begin   Showmessage('Bad file:'+Filepath + Usefulinformation);   Goto cleanup;  // Or call Nestedproc cleanup(); exit;  // However this costs performance if the compiler optimization doesnt do it and doesnt convert the call into a goto  end; This is selfdocumenting and avoids duplicated code. ---------------------------------------------------------------------- Weitentaaal · 2021-02-04 16:20 Thanks For all those reply's, definitely learned a lot And i'm pretty sure i can Solve it now, thanks again! . ---------------------------------------------------------------------- MarkMLl · 2021-02-04 16:48 Quote from: Peter H on February 04, 2021, 04:22:30 pmI think, when my program detects an error, then it has necessaryly the best information about the error. So the error should be handled at the location where it was detected, e.g. if gotbaddata then begin   Showmessage('Bad file:'+Filepath + Usefulinformation);   Goto cleanup;  // Or call Nestedproc cleanup(); exit;  // However this costs performance if the compiler optimization doesnt do it and doesnt convert the call into a goto  end; This is selfdocumenting and avoids duplicated code. /Reporting/ the error where it happens is one thing, but in the general case it can only be /handled/ at the point where normal operation can be resumed. The Delphi/FPC exception mechanism is a good fit for that since at the point of error an exception can be raised with (among other properties) an embedded message saying what the problem is, which can be displayed by the exception handler before it- for example- makes sure that a faulty file is closed and asks if the user wants to try another one. In the general case, everybody agrees that goto can almost always be replaced by something more specialised, for example by continue or break in the case of an Object Pascal loop. But that doesn't necessarily mean that the replacement is more elegant, intelligible or maintainable. MarkMLl ---------------------------------------------------------------------- Peter H · 2021-02-04 17:12 Just this this provocative thougt: Bjarne Stroustrup wrote the first versions of C++ as a preprocessor for C. He has programmed in Pascal before, so why didnt he write a preprocessor Pascal++ for Pascal? Its too difficult, the versions of Pascal in 1980 had too much limitations and dogmas and probably where incompatible together.