• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 582
  • Last Modified:

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):

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

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.

1 Solution
  While FChar <> '-' Do NextChar;
until FChar ='>';
nricoAuthor Commented:

/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.
Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

I think I would try something like this:


ThisStep := 0;
NextStep := 1;

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

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

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.


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...
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*

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.
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.
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.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now