Solved

Goto dilemma

Posted on 2002-05-14
10
534 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
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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now