Use of Goto
-
I've just noticed this pattern (in 300 places) in some legacy code I've been inflicted with:
[code] Goto(0, Line2); printf("some text"); [/code]
This is plain old C for an 8051-type microcontroller.
-
And? I would guess that the Goto function sets the cursor position on the screen. I don't see problem with that. Please explain.
-
@SenTree said:
I've just noticed this pattern (in 300 places) in some legacy code I've been inflicted with:
Without seeing the definitions of Goto or Line2, it's pretty hard to know what's really going on here.<font face="Lucida Console" size="2"> Goto(0, Line2); printf("some text"); </font>
This is plain old C for an 8051-type microcontroller.
-
mol1111 is right, Goto sets the cursor position, Line2 is just a line number. My thought was that Goto (capitalised) was easily confused with goto (lower case). A different function name might have been helpful.
No big deal, I just found it mildly amusing and wrote the post as a diversion from trying to untangle the rest of the spaghetti.
-
I agree that the name can be confusing, especially for beginners.
-
@mol1111 said:
I agree that the name can be confusing, especially for beginners.
Beginners ? I would think that even more experienced people would prefer something like SetCursorPosition.
-
@SenTree said:
My thought was that Goto (capitalised) was easily confused with goto (lower case). A different function name might have been helpful.
Well, there ya go. I would have thought that the fact that it's capitalised stands out like a sore thumb. Even noobs learn pretty damn quick that C is case-sensitive and you can't go around capitialising the keywords. And also, that's very visibly a function call rather than a statement, and it has these two arguments; I can imagine someone doing a brief double-take when they see it, but I don't see how they could go for any amount of time under the misapprehension that that was a goto statement.Still like you say it's a slow Monday morning round here ... :-)
-
@mol1111 said:
I agree that the name can be confusing, especially for beginners.
Beginners? Do beginners even know about goto, these days? I first learnt to program in BASIC so I was familiar with it, but I don't remember much mention being made of gotos when I learnt C, either in the book I learnt from or the classes I sat through a year or so later. I'm not sure I even knew they existed in C at first, and I certainly haven't used one since the BASIC days.
-
@DaveK said:
I can imagine someone doing a brief double-take
Did that, posted in haste, regretted at leisure.
@DaveK said:Still like you say it's a slow Monday morning round here ... :-)
Exactly... :D
-
@NSCoder said:
@mol1111 said:
I agree that the name can be confusing, especially for beginners.
Beginners? Do beginners even know about goto, these days? I first learnt to program in BASIC so I was familiar with it, but I don't remember much mention being made of gotos when I learnt C, either in the book I learnt from or the classes I sat through a year or so later. I'm not sure I even knew they existed in C at first, and I certainly haven't used one since the BASIC days.
goto is used in c (see e.g. zlib library). There are at least two good reasons to use goto:
1) poor-mans exceptions:
r = f();
if (!r) goto fail;
r = g();
if (!r) goto fail;
...
return OK;
fail: /* handle errors */The idea is to separate normal code-flow and error handling.
2) if you have specs of some state machine written in this way ("... 3] if something than go to to 2] else go to 5] ...") you may want to code itusing gotos to be as near as possible to the specs.
-
@pitchingchris said:
@mol1111 said:
I agree that the name can be confusing, especially for beginners.
Beginners ? I would think that even more experienced people would prefer something like SetCursorPosition.
Yes, but they won't be confused just will think something bad about original coder.
BTW I grew up on Borland and I'm completely happy with gotoxy :-)
-
@mol1111 said:
You and me both.@pitchingchris said:
@mol1111 said:
I agree that the name can be confusing, especially for beginners.
Beginners ? I would think that even more experienced people would prefer something like SetCursorPosition.
Yes, but they won't be confused just will think something bad about original coder.
BTW I grew up on Borland and I'm completely happy with gotoxy :-)
-
@mol1111 said:
@pitchingchris said:
@mol1111 said:
I agree that the name can be confusing, especially for beginners.
Beginners ? I would think that even more experienced people would prefer something like SetCursorPosition.
Yes, but they won't be confused just will think something bad about original coder.
Which was exactly what I did, and it's actually one of his milder WTFs. This guy perpetrated much of our original code, which we're still using 10-15 years later (industrial sensors change very slowly). I get a sinking feeling whenever I have to maintain some of his early stuff.
BTW I grew up on Borland and I'm completely happy with gotoxy :-)
Funnily enough, that's exactly the function name he used on later projects. I too am happy with that, and in fairness it does show that he improved over time.
-
@mol1111 said:
goto is used in c (see e.g. zlib library). There are at least two good reasons to use goto:
I recall seeing a goto in the original Half-Life's source code. It fell mostly under the "poor-man's exceptions" category, I think, but I think it was used as an exit condition rather than an exception. I'd have to see it again to be sure.
-
It's also acceptable (IMO) to use goto as a "super-break" to break out of an inner and outer loop in one step. The alternative is to make a semiphore boolean and checking it each time through the outer loop.
Javascript added the concept of labels, and the ability to continue to a specific label, to handle this case without using the "goto" keyword.
-
@blakeyrat said:
It's also acceptable (IMO) to use goto as a "super-break" to break out of an inner and outer loop in one step. The alternative is to make a semaphore boolean and checking it each time through the outer loop.
Javascript added the concept of labels, and the ability to continue to a specific label, to handle this case without using the "goto" keyword.
Um, 'labels' are the term for what C uses to target gotos, and they're in a rather large portion of languages. Perl also can break or continue to a label (well, technically, last, next, or redo - Larry recognized the semantic confusion presented by break and continue, fixed it in Perl, and then added in his own semantic confusion for good measure.)
I suspect there are other languages which can perform similar loop flow operations. I think I've actually used some, because I remember thinking Perl's redo was nifty, but the ability to break to a specific label didn't seem new.
-
Sorry, I worded that funky. Labels obviously aren't the innovation; you're right, gobs of languages have those. The innovation is letting the "continue" keyword skip to a specific label optionally.
Considering the text editor on this stupid forum breaks my browser's spell-checking, *not* being a spelling-Nazi would be nice.
-
@mol1111 said:
BTW I grew up on Borland and I'm completely happy with gotoxy :-)
I too grew up on Borland, but I used this instead:
[code]push ds
lds si,Source
les di,Dest
mov cx,Count
mov al,TextAttribute
mov ah,al ; Attribute goes in ah
jcxz L2
L1: lodsb
stosw
loop L1
L2: pop ds[/code]Dest would be B800:0000 + (row*80 + col)*2 for color displays, B000:0000 + offset for monochrome.
I could draw an entire screen worth of text on a 4.77 MHz 8088 without visible flicker, something TP could not do, much less the BIOS routines...
(edit) How do you get whitespace in code snippets while avoiding visible "nbsp" tags??
-
@boh said:
(edit) How do you get whitespace in code snippets while avoiding visible "nbsp" tags??
With a browser that is (!Chrome && jsEnabled).
<font face="Lucida Console" size="2">push ds
lds si,Source
les di,Dest
mov cx,Count
mov al,TextAttribute
mov ah,al ; Attribute goes in ah
jcxz L2
L1: lodsb
stosw
loop L1
L2: pop ds</font>
-
@boh said:
(edit) How do you get whitespace in code snippets while avoiding visible "nbsp" tags??
Use the raw editor. And they're entity references, not tags.
-
@Spectre said:
Use the raw editor
It is interesting to note that my advice is opposite to yours, but mine is accompanied by a working example.