Goto dilemma


I'm in sort of a dilemma, and I would like to hear your opinions, as experienced developers, on this matter. It's sort of an ethical issue, concerning the use of GOTOs.

In the old days, I used BASIC to program the Commodore 64, or later GWBASIC on the PC (We're talking 8088 here folks :). BASIC wasn't exactly known for its structured programming, and while some dialects included some rudimentary form of procedures (SUBroutines, called with GOSUB), programs mostly consisted of GOTOs to chain parts of the program together. I know mine did.

Later on, when I wised up and got access to a 286 (imagine that :), I ventured more into structured programming and got started with the language Pascal. From that point on, GOTOs became a no-no, the forbidden command that was only allowed for newbies and in batch scripts. I actually recoiled when I was once exposed to a Pascal program that used GOTOs exclusively to handle its flow control. When asked about this, I would always strongly advocate against the use of GOTOs and claim that anything using GOTOs could be rewritten to use only structured flow control statements, and become more readable at that.

Here is where my dilemma comes in... my brothers, I have sinned! Yes, I have done the unthinkable and actually used a GOTO in one of my programs... It seemed the most appriopiate construction for the problem at hand, and while I am proud that I have overcome my preconceptions and actually used a GOTO in a position where it was useful, still... part of me is in doubt whether or not I should have used a different construction, maybe something that I have overlooked.

I will explain the problem to you (it's not a very big one, just some pattern checking), and I hope you can help alleviate my guilt or tell me what a dumbass I am for overlooking an obvious structured way.

The point of the program is to check the syntax of HTML files. The matter of interest lies in a routine that ignores comments. Once it is determined that a comment has started (by the encounter of the character sequence <! ), the program keeps on reading characters from the input string until a sequential -> is found. That is, a dash followed by a "greater than" sign. If just a dash is encountered, followed by something else than a greater than sign, the comment has not yet ended and the program should keep checking. Also, if a greater than sign is encountered that is not preceded by a dash, the check should go on.

This is the code that I eventually used (NextChar is a routine that reads the next character from the input string and places it in FChar; I used some more constants here that have been omitted for clarity):


checkend:
  While FChar <> '-' Do NextChar;
  NextChar;
  If FChar <> '>' Then Goto checkend;
  NextChar;


Quite simple, yes? My question is, is there a way that I could have written this with structured statements such as while and if, and if so, would it have been clearer? Or is my use of the goto justified?

I'd appreciate any input you might have.

Greetz,
nrico
LVL 1
nricoAsked:
Who is Participating?
 
MBoConnect With a Mentor Commented:
repeat
  While FChar <> '-' Do NextChar;
  NextChar;
until FChar ='>';
NextChar;
0
 
nricoAuthor Commented:
Duh...

/me slaps himself silly
0
 
robert_marquardtCommented:
There is no question. I think your comment testing is not completely correct anyway. At least if you take XML comments into account.
I would solve the whole problem with an HTML or XML parser.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
rarigoCommented:
I think I would try something like this:

Global

ThisStep := 0;
NextStep := 1;


function ThisChar : Char;
begin
    Result := Buffer[ ThisStep ];
    Inc( ThisStep );
end;

function NextChar : Char;
begin
    if NextChar < Length( Buffer ) then
        Result := Buffer[ NextChar ];
    else
       Result := '';
    Inc( NextChar );
end;

ThisChar is a routine that reads this char and
NextChar is a routine that reads the Next One

while (( ThisChar <> '-' ) and ( NextChar <> '>' ))  or ( NextChar = '' )  do ExecuteRoutines;



Or use a GOTO after all I don't have feel that guilty using this.




Reginaldo

0
 
nricoAuthor Commented:
I think you are misunderstanding me... this is not a real question as I have already solved the problem. I just want to hear your opinions on the matter...
0
 
rarigoCommented:
Sure.I know that. Thats my opinion or should I say that would be my way out of this.

0
 
AlexNekCommented:
2 nrico
What answer you expected? I can only write that you right: program without goto is better as with goto.
When I learn programming, I used goto only for exit by error. The point of view: if you write program for other people than never use Goto, if you write for machine then you can use any construction.
0
 
Holger101497Commented:
So you're looking for comments?
Mine is different: I think a comment starts with <!-- and ends with -->
Just to make your checking more complicated ;-)))
Also, in XML-files, the sequence "--" (two dashes) is not permitted within comments. For HTML, I don't know. Probably depends on the browser - as almost anything with HTML *g*

Holger
0
 
AvonWyssCommented:
As for GOTOs, I agree that they should be avoided whenever possible. Besides the "evil" GOTO, there are three other statements which can stay in the position of GOTOs for flow control. These are EXIT (for procedures and functions), and BREAK and CONTINUE (for loops). In my opinion, using EXIT and BREAK doesn't make code hard to understand. However, I always avoid GOTO and also CONTINUE whenever possible, since they do make code less understandable.

As for your HTML parser, I think that other pattern matching approaches would be better. There are efficient generic ways to do exact pattern matching, which yould then allow you to just apply them to your patterns:

EndOfComment:=FindNextPattern('-->', YourHTMLString, StartOfComment);

Since you'll encounter many patterns to match, using such an approach will not only make your code less errorprone, smaller and probably faster, but it will also make it MUCH easier to understand. Compare my one line of code with your code block; you'll quickly see that for someone who reads this code for the first time, it will be much easier to understand what is supposed to happen in my suggested code.
0
 
nricoAuthor Commented:
Agreed... but it was part of a recursive descent parser... first the scanner reads in characters and translates them into symbols, then the parsers combines the symbols to check whether constructs are valid.

I'll give the points to MBo since he gave the structured solution which I obviously overlooked, and I appreciate all of your input.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.