PDA

View Full Version : Discussion: GOTO as a bad choice?



Bob Phillips
07-13-2008, 08:27 AM
Ok, i admit there was a typo in my code but i believe that this does exactly the same as yours Bob only shorter!

Sorry Simon, I didn't see yours, it was a response to MD.

As for shorter, maybe, but you use Goto, and you know what the Goto fascists will do if they find out!

Simon Lloyd
07-13-2008, 08:32 AM
Sorry Simon, I didn't see yours, it was a response to MD.

As for shorter, maybe, but you use Goto, and you know what the Goto fascists will do if they find out!Lol!, why is it frowned upon? in my limited knowledge i find it works perfect! you make a defined statement of where you want the procedure to be upon a defined condition, for me no guessing where in a loop the procedure is or is faulting!

But i hold my hands up if it has bigger implications!

Norie
07-13-2008, 09:03 AM
Simon

Have a look here spaghetti code (http://en.wikipedia.org/wiki/Spaghetti_code).

Using Goto isn't forbidden per se and if it's what's needed use it.

Simon Lloyd
07-13-2008, 09:14 AM
Thanks Norie but i cannot subscribe to that train of thought because as i said for me i state "GOTO 'Label' " the 'Label' is clearly visible in the code and therefore easier to know where the code is being directed. I have seen many an example (and used myself) where "For...If..Then....Goto...Next" has been used in a far more complexed way than that but very effectively and still with a high degree of tracibility within the code......with my limited knowledge on the subject i would still argue that people that advocate the minimising the use of GOTO are endeavouring to get everyone to comply with a unified structure of coding that they themselves prefer!

Norie
07-13-2008, 09:19 AM
Simon

Nobody is trying to force anyone to use a unified structure for coding.

But then again programming is occasionaly referred to as structured programming.

You say that the label is clearly visible in the code?

What if there are multiple labels, similarly named and sprinkled liberally throughout the code.:eek:

Simon Lloyd
07-13-2008, 09:25 AM
I understand what you are saying Norie but i think the active words there in your post is "Similarly Named" so still traceable.....i guess it just my opinion on it though, so i guess my code will always be a mix of "spaghetti and pasta"

Norie
07-13-2008, 09:38 AM
Simon

Similarly named is only one problem.

The major problem is trying to follow the flow of the code.

Now that may be clear to whoever created it originally but somebody else might find it hard to follow.

Bob Phillips
07-13-2008, 10:08 AM
As I keep saying, Gotos and spaghetti code maketh do not, spaghetti programmers do. Blaming poor code on Goto's is indolence on commentators who have a mantra and chant it (and I say that even though I personally avoid Gotos where I can).

Norie
07-13-2008, 10:15 AM
How is it being indolent to comment that using Gotos isn't that good an idea?:confused:

Generally when I see code using Goto I'll try and interpret it and post alternative code.

But it can be difficult to do that because of the reason I said - the flow of the code is hard to follow.

This is also a problem when code is posted that uses ActiveCell/Activate/Select/Selection.:)

Simon Lloyd
07-13-2008, 10:30 AM
....This is also a problem when code is posted that uses ActiveCell/Activate/Select/Selection.:)Although i do advocate not using Select or Activate i feel that Activecell and Selection are definate locations just like the 'Label' from a GOTO allbeit that activecell and selection are changeable by their definition but as reffered to in code as a definite target - they don't create an interruption in the code, if they did then the more ambiguos use of a variable to store an incremental number is a lot worse as without stepping through the code or having a msgbox (or value to a cell) you can't possibly tell where that count stands, so would that not impede the "flow" of the code from the point of view of another trying to follow the creators code? :)

Norie
07-13-2008, 10:34 AM
Simon

What you regard as the active cell when using ActiveCell might not be the same thing as VBA regards as the active cell.

Also using Selection you aren't even guaranteed that the selection is a cell or range.

I've lost count of how may times I've seen code posted that doesn't work because the Selection is either an object like a command button or not the range the user expected.

Simon Lloyd
07-13-2008, 11:53 AM
I MOVED THIS DISCUSSION AS IT WILL ONLY CLOUD THE OP's ORIGINAL REQUEST!

My understanding of VBA not having the same view as the user with regards to Activecell/Selection is that the expected operation is the same (to the user) everytime it runs regardless of which cell/object/range/sheet the user selected, other than that i can't see why it would be different. I mean to say that the user may have last clicked a command button which still has the focus when the code is run or selected a cell but the code runs with a specific sheet other than the one the user currently has active. If i haven't quite grasped what you have intimated can you explain further?, but getting back to the thread of this discussion, why would using the generic terms "activecell" or "selection" impede the flow or continuity of reading or following the code?

Bob Phillips
07-13-2008, 11:53 AM
How is it being indolent to comment that using Gotos isn't that good an idea?:confused:

IF you are going to pull me up on what I say, it might be a good idea to read it. What I said was, and you can check back on this, is that ... Blaming poor code on Goto's is indolence ...


But it can be difficult to do that because of the reason I said - the flow of the code is hard to follow.

And as I keep saying, it is not GOtos that make the flow hard to follow. I can knock up an example in 15 minutes that would not use a single goto and would be incredibly difficult to follow, and another where Gotos are used extensively that any fool can follow.

I keep hearing the mantra that Goto's are bad, they break the flow of code. Well so does Exit For, Exit Sub, Exit Do, but I don't hear the same people arguing againts these. Whilst I am not suggesting that anyone uses Gotos unless necessary, I tire of this lazy statement that I have never heard a convincing argument to back up.

Simon Lloyd
07-13-2008, 11:58 AM
Bob, i'm just a junior by the standards of both of you but you made perfect sense in your last argument, if folk are to use "throw away" statements then the same should be employed for the whole genre. I tried to imply that earlier but failed miserably!

Norie
07-13-2008, 10:06 PM
IF you are going to pull me up on what I say, it might be a good idea to read it. What I said was, and you can check back on this, is that ... Blaming poor code on Goto's is indolence ...



And as I keep saying, it is not GOtos that make the flow hard to follow. I can knock up an example in 15 minutes that would not use a single goto and would be incredibly difficult to follow, and another where Gotos are used extensively that any fool can follow.


I'm in no way 'pulling' anybody up and I must admit language like this is a little offensive.

what the Goto fascists will do if they find out:dunno

Simon Lloyd
07-14-2008, 01:07 AM
Norie, i rarely find Bob to be offensive, i would guess (as he's well read) that he checked his useage and terminology first:

Facist, Said of a computer system with excessive or annoying security barriers, usage limits, or access policies. The implication is that said policies are preventing hackers from getting interesting work done. The variant "fascistic" seems to have been preferred at MIT (http://dictionary.reference.com/browse/MIT).
In the design of languages and other software tools, "the fascist alternative" is the most restrictive and structured way of capturing a particular function; the implication is that this may be desirable in order to simplify the implementation or provide tighter error checking. Compare bondage-and-discipline language (http://dictionary.reference.com/browse/bondage-and-discipline%20language), although that term is global rather than local.
[The Jargon File (http://dictionary.reference.com/go/http://www.catb.org/~esr/jargon/)]
(2003-07-29)

Bob Phillips
07-14-2008, 01:26 AM
I'm in no way 'pulling' anybody up and I must admit language like this is a little offensive.
:dunno

A definition of fascism can be read as

A system of government marked by centralization of authority under a dictator, stringent socioeconomic controls, suppression of the opposition through terror and censorship, and typically a policy of belligerent nationalism and racism.

The way that those who object to Gotos raise the assumed asupices of OO, structured programming, 'real' coders and so on is comparable to authoritarian centralization;
the immediate grouping of those who hold those views rings of stringent social controls;
the jumping on anyone who dares to argue against is attempts at suppression of the opposition;
and the ridiculing and insulting of those who may argue that Gotos are not bad, or not the real problem, sounds like belligerent nationalism and racism to me.

In other words, you are not allowed to have an opinion different to theirs otherwise you face the wrath of the 'enlightened few'.

So you may find it offensive, I think it is an apt description of an intolerant attitude without even the buttress of a reasoned argument.

Norie
07-14-2008, 06:23 AM
Using Goto isn't forbidden per se and if it's what's needed use it.
And.

Nobody is trying to force anyone to use a unified structure for coding.

Bob Phillips
07-14-2008, 01:44 PM
Has anyone yet said that you are? You jumped in and decided that the comments, some of which were made before you joined in, were aimed at you. Not much we can do about that.

Norie
07-14-2008, 03:12 PM
i would still argue that people that advocate the minimising the use of GOTO are endeavouring to get everyone to comply with a unified structure of coding that they themselves prefer!

Eh?:huh:

Simon Lloyd
07-15-2008, 02:11 AM
Eh?:huh:Norie you replied to that in post 5!, i mean to say that in all walks of life not just programming you always come across people that want you to conform to their way of thinking or doing things, sometimes you can't avoid it as it's law, others you take it under advisement but still do your own thing, providing you don't bow to peer pressure, there's nothing wrong with breaking the mould, anyway who was it that determined the mould was correct in the first place?, i think you will find that it was a programming convention that someone developed, sold the idea to a few of his lesser willed buddies who then advocated the use of it and hey presto! everyone uses that as the norm, it doesn't make it right!.

If you really look at the issue you will notice that it's a personal preference that makes you code the way you do, until you decide that something else you tried works better then that becomes your new programming convention, it may not suit anyone else but works for you so you would defend that you are right and they are wrong - nothing wrong with that, but you have to allow others the same privilidge as you.

I'm afraid to me your argument is as valid as the world is flat argument


"Columbus believed that the world was round. Everyone else thought that it was flat. In 1492, Columbus finally overcame the objections of the bigoted church leaders of Spain, who believed the world was flat because the Bible said it was. Columbus got the backing of Queen Isabella on the sly, behind King Ferdinand's back. Perhaps there was some romantic infatuation or dalliance. In any event, Queen Isabella pawned her jewels to raise the funds for Columbus' three ships and off he went. His epic voyage of discovery was one of the crucial steps in mankind's throwing off the superstitions of the dark ages."
with you playing the part of the spanish church (because the Bible said so!) and Bob and I and many other are Columbus.

That passage illustrates why you shouldn't be a sheep all your life, it's good to be different! ;)

Norie
07-15-2008, 06:08 AM
Simon

I really don't know where you are coming from.

At no point in this discussion or elsewhere have I insisted that a particular programming approach should be used.

If it works, it works.:)

Simon Lloyd
07-15-2008, 06:15 AM
I agree you didn't insist, and i agree with the if it works.....! it's just in response to my asking why is GOTO frowned upon you posted a link to spaghetti code, i took the time to read it and then posted a disagreement to its reasoning why GOTO is not used in favour of a loop, from there on in you felt that you had to defend the position of those demeaning the validity of GOTO, hence me (and probably Bob) defending our take on the use and validity of it! :)

Tommy
07-15-2008, 10:12 AM
I use goto only as a last resort. Not because I don't think it has it's value, or sombody says not to, but because I find that something is wrong with my logic/coding when ever I use it.:dunno

As Norie said though...
If it works, it works.:)

Ya'll know how I love to throw pennies on the pile :rofl: