mIRC Home    About    Download    Register    News    Help

Print Thread
Page 2 of 2 1 2
#8160 26/01/03 06:06 PM
Joined: Dec 2002
Posts: 56
G
Babel fish
Offline
Babel fish
G
Joined: Dec 2002
Posts: 56
Sorry I can't agree with that.. Ive done a lot of tests on GOTO and WHILE loops. No matter how you construct the GOTO loop, it is not possible to make it faster than a WHILE loop. Please feel free to post some code and prove me wrong wink

#8161 26/01/03 06:12 PM
Joined: Dec 2002
Posts: 56
G
Babel fish
Offline
Babel fish
G
Joined: Dec 2002
Posts: 56
Quote:
using GOTO for anything I can think of is pretty bad coding.


Now come on, this isn't true. How else will you jump from part of a routine to another? This is what GOTO is for. I use it in events to Jump too $1 and execute the relevant code on $2-. It's got little to do with Looping. I believe GOTO looping was used because mIRC never had a dedicated looping function like WHILE.

Using GOTO jumps wisely can be far better and faster coding than a bunch of IF-ELSE checking. However, Using GOTO jumps for loops when we have a WHILE statement doesn't make much sense.

#8162 26/01/03 07:03 PM
Joined: Dec 2002
Posts: 2,809
C
Hoopy frood
Offline
Hoopy frood
C
Joined: Dec 2002
Posts: 2,809
Well for your example of error trapping, as I said with Sabby's example, switch() would be better, in your example error handling (try, except, finally, throw) would be a much better way to do error handling than a goto trap.

#8163 26/01/03 07:14 PM
Joined: Jan 2003
Posts: 1
S
Mostly harmless
Offline
Mostly harmless
S
Joined: Jan 2003
Posts: 1
Quote:

there is nothing a for loop can do what a "var %i = 1 | while (%i <= 10) { bla | inc %i }" can't do... just a little more code.


there is nothing a while loop can do what a "var %i = 1 | :l | if (%i <= 10) { bla | inc %i | goto l }" can't do...just a little more code.

I think the main problem presented by for loops is syntax. mIRC doesn't have any multi-argument looping/conditional commands. It would probably require changes to the parser and a new syntax addition.

I'm ok with inc/dec while loops, although I like for better, but the one I'd really like to see is do/while, specifically for the reason mentioned: sockread event. Or perhaps an identifier akin to $sockbr that tells if there is any data left to be read?

Code:
var %t
dowhile ($sockbr) {
  sockread %t 
  ;do stuff...
}


Code:
var %t
while ($sockdata) {
  sockread %t
  ;do stuff..
}


Either of those would be fine. A "dowhile" loop would definitely be more confusing if done that way, but it would keep closer to the "mIRC feel".

-myndzi

#8164 26/01/03 07:34 PM
Joined: Dec 2002
Posts: 1,321
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Dec 2002
Posts: 1,321
Code:
  var %t
  while (1) {
    sockread %t
    if (!$sockbr) break | ; or halt
 
    ;  The rest of the script
  }
  ;  Cleanup
}


DALnet: #HelpDesk and #m[color:#FF0000]IR[color:#EEEE00]C
#8165 26/01/03 07:53 PM
Joined: Dec 2002
Posts: 1,321
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Dec 2002
Posts: 1,321
Quote:

Well for your example of error trapping, as I said with Sabby's example, switch() would be better, in your example error handling (try, except, finally, throw) would be a much better way to do error handling than a goto trap.


...none of which are currently in mIRC scripting. Furthermore, the error condition is what you need the goto for, not how it's handled. If-elseif-elseif-else (aka switch (basically)) is how you'd decide just what to do about the error, if anything. You can bet that try/catch won't be in mIRC anytime soon; mIRC is not reentrant in any meaningful way, though you can fake it a bit. Seems to me I remember seeing on ERROR goto whatever .. in many high level languages.

I cannot honestly remember a time when I have written (not modified older code) a goto into a script. The ONLY place I can remember using it in the 3 years since while () was added is in on SOCKREAD, and that was probably more out of habit and follow the old example in the help file to get everything started. It's simple enough to put the code you want to jump to into an alias and simply call that alias (which will return or not).

All that being said, I STILL disagree that ANY language element is "bad." There are perhaps "better" ways of doing things, but GOTO is not inherently bad. What you are objecting to is the bad coding practices that its overuse or misuse lead to; THAT I quite agree with. That's like saying SET is bad because we have /writeini, /var, /hadd, etc. or it's bad because you can easily get your variables written over by another scripter's script that uses the same variable names as yours does (how rude!).

If you truly believe GOTO is bad, I strongly suggest that you stay the hell away from assembly language. laugh


DALnet: #HelpDesk and #m[color:#FF0000]IR[color:#EEEE00]C
#8166 27/01/03 06:01 AM
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
I agree. I don't use goto often in mirc but there are cases where goto does the job in the best possible way (at least for me). The most striking example is using it as another way of switch (in absense of the latter):
Code:
var %a = $1
goto %a
:foo | return 1
:bar | return 2
:%a | return Unknown

I've used goto in a couple of other cases too (but not for looping). Another example would be to break out of nested loops. Only goto can do that at once.

As for arguments of the type "goto makes spaghetti code", that's the programmer's fault. If a programmer doesn't realise he's making spaghetti code, he's hopeless. I don't think any language should remove tools that others might find useful in some cases just because some people would use them in a 'bad' way.


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
#8167 27/01/03 09:04 AM
Joined: Dec 2002
Posts: 25
S
Ameglian cow
OP Offline
Ameglian cow
S
Joined: Dec 2002
Posts: 25
Just that this is the only sane way to make up for things mIRC lacks doesn't make it right smile

Also note that I referred to 'self respecting higher level language', not some script kiddy language in a chat client :tongue:

#8168 27/01/03 01:20 PM
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
"Just that this is the only sane way to make up for things mIRC lacks doesn't make it right"

It makes it right enough if Khaled isn't planning on adding a switch (which is possible seeing that he's already very busy in other fields of this one-man project called mIRC).


"GOTO is the statement that is removed from every self respecting higher level language, or should be."

This statement sounded irrational to me all along. Should be? This is a feature. What entitles you to say which feature should or should not be in a language? If it's useless to you don't use it, but if there is even one person who thinks something is useful, you "should not" have anything to do with it.

Anyway, I'm beginning to repeat what Hammer and others said in this thread, so I'll stop :tongue:


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
#8169 27/01/03 04:34 PM
Joined: Dec 2002
Posts: 87
V
vcv Offline
Babel fish
Offline
Babel fish
V
Joined: Dec 2002
Posts: 87
Nicely put Hammer.

In the end, all while and for loops use GOTOs (jxx op codes) in every language... the only difference is that it is hidden from you.

GOTO should not ever be used by beginners. But for programmers that know what they are doing, I don't see ANYTHING wrong with using goto, as long as they do not abuse it.

But I repeat.. in the end, all our code is eventually translated to goto's.

#8170 27/01/03 07:00 PM
Joined: Dec 2002
Posts: 2,809
C
Hoopy frood
Offline
Hoopy frood
C
Joined: Dec 2002
Posts: 2,809
if (%a == foo) return 1
else if (%a == bar) return 2
else return Unknown

That would be a better way to do it than a goto. More easily readable, you can follow what is going on much more clearly.

#8171 28/01/03 04:01 AM
Joined: Dec 2002
Posts: 56
G
Babel fish
Offline
Babel fish
G
Joined: Dec 2002
Posts: 56
Ofcourse with your example nobody would use GOTO, That would be stupid. Readability isn't important in such a tiny alias, as you will never go back and debug it. For your example, Speed and Readabiity is not an issue. There are cases where GOTO jumps are far better than using a long stream of IF-ELSE though. Nobody suggested that GOTO was better than IF-ELSE in every possible circumstance. To use a GOTO jump in that instance would be unwarranted.

If that example is meant to illustrate that all uses of GOTO are unwarranted, then I have to disagree. If it's meant to bolster SubSpace's stance of "Goto should be removed from every self respecting language" I have to disagree with that too.


#8172 28/01/03 12:22 PM
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
Better? If by that you mean "more readable", this is obviously subjective, as I find both ways equally readable, with the goto way looking cleaner to me.

But "better" has many aspects, one of which is speed: the goto way is faster and this is not subjective. This has to do with the fact that the %a variable isn't evaluated over and over with each if statement: it's evaluated only once and then mirc looks for the literal label that matches the content of %a. Considering that this is scripting -not programming-, which means that variable evaluations cost, this way is "smarter", even if the actual speed gain isn't so important in most cases.


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
#8173 28/01/03 11:33 PM
Joined: Dec 2002
Posts: 2,809
C
Hoopy frood
Offline
Hoopy frood
C
Joined: Dec 2002
Posts: 2,809
Quote:

This has to do with the fact that the %a variable isn't evaluated over and over with each if statement


Hence the fact why switch() would be very useful as well, it is designed to do exactly what you are talking about.

#8174 29/01/03 12:40 PM
Joined: Dec 2002
Posts: 1,321
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Dec 2002
Posts: 1,321
And in fact, the closest approximation of switch/case, currently, would use exactly that: GOTO %switchlabel
Code:

  ;    switch(condition) {
  ;        case 1,2:
  ;                  code here
  ;                  break;
  ;        case 3:
  ;                  code here
  ;                  break;
  ;        case N:
  ;                  code here
  ;                  break;
  ;        else
  ;                  code here
  ;    }                         // End of switch(condition)
 
  if (condition) {
    goto $ifmatch
 
    :1
    :2
    ;  command(s)
    goto EndOf.Switch | ; break
 
    :3
    ;  command(s)
    goto EndOf.Switch | ; break
 
    :N
    ;  command(s)
    ;  Since this is the last section, goto EndOf.Switch is redundant
 
    :EndOf.Switch
  }
  else {
    ;  command(s)
  }

One pass through evaluating the condition and then go directly to the appropriate section of code (which is the benefit of using switch). Switch is a special case of IF-ELSEIF-ELSE where the exact same condition applies to each case, varying only in the value. This is, in fact, a good example of when you probably SHOULD use GOTO over IF-ELSEIF-ELSE, if speed is an issue.


DALnet: #HelpDesk and #m[color:#FF0000]IR[color:#EEEE00]C
#8175 31/01/03 01:56 AM
Joined: Jan 2003
Posts: 3,012
Hoopy frood
Offline
Hoopy frood
Joined: Jan 2003
Posts: 3,012
Fist off, about the for loop..
Quote:

For loops are different, they are used when initialization is needed and the iteration is standard:

for (%i = 0; %i != 4; inc %i)
{
echo %i
}

Untrue, the variable in the for lop can still be altered judt like any other. Teh differece again is there are less lines of code, a preset stop length, and much more legible.

Seconds, the gotos are not all bad, though I do find them a means of being a 'beginners loop' From my visuals and scripts I have seen, people who do not realize what is going on use goto's, where as more experienced tend to stick with while loops.

Not only the for though would be a good idea, but perhaps a switch statement. Rather then have several labels, provide yet again, another cleaner visually satisfying methos to parse out a variables contents..

Just my 2 cents.. laugh


-KingTomato
#8176 31/01/03 02:52 PM
Joined: Dec 2002
Posts: 29
N
Ameglian cow
Offline
Ameglian cow
N
Joined: Dec 2002
Posts: 29
i like the foreach token idea can come in useful also a for next loop would be nice in some cases
Code:
for %a = 1 to 255 | echo -at $chr(%a) | next %a

oh and also goto sux cant really see anywhere where it is better to use a goto....

#8177 13/02/03 07:09 AM
Joined: Feb 2003
Posts: 29
S
Ameglian cow
Offline
Ameglian cow
S
Joined: Feb 2003
Posts: 29
I would like to have all the loop types codemastr suggested. smile

Page 2 of 2 1 2

Link Copied to Clipboard