Solved

Goto dilemma

Posted on 2002-05-14
10
561 Views
Last Modified: 2010-04-04

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
0
Comment
Question by:nrico
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 1

Accepted Solution

by:
MBo earned 50 total points
ID: 7008491
repeat
  While FChar <> '-' Do NextChar;
  NextChar;
until FChar ='>';
NextChar;
0
 
LVL 1

Author Comment

by:nrico
ID: 7008525
Duh...

/me slaps himself silly
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 7008534
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:rarigo
ID: 7009228
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
 
LVL 1

Author Comment

by:nrico
ID: 7009581
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
 

Expert Comment

by:rarigo
ID: 7009591
Sure.I know that. Thats my opinion or should I say that would be my way out of this.

0
 
LVL 16

Expert Comment

by:AlexNek
ID: 7009689
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
 
LVL 6

Expert Comment

by:Holger101497
ID: 7012009
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
 
LVL 14

Expert Comment

by:AvonWyss
ID: 7013937
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
 
LVL 1

Author Comment

by:nrico
ID: 7013962
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question