Link to home
Start Free TrialLog in
Avatar of nrico
nrico

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of MBo
MBo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nrico
nrico

ASKER

Duh...

/me slaps himself silly
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.
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

Avatar of nrico

ASKER

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...
Sure.I know that. Thats my opinion or should I say that would be my way out of this.

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.
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
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.
Avatar of nrico

ASKER

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.