Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : The GOTO comand
Author | Message | ||||
herbnz Senior Member Joined: 18/02/2007 Location: New ZealandPosts: 258 |
When I was in to programming in, to many years ago to count, I remember the critics of BASIC said it was unstructured. This was mainly due to the GOTO command that allowed jumping all over the place. It was refered to as spaghetti programming. It makes a program hard to follow for others looking over it and in fact when one looks back at ones own efforts. Impossible to draw flow charts for. As we now have a lot of people starting out I would like to hear some of the experienced programmers comments and at least making us aware of other alternatives, such as GOSUB,FOR,DO, etc. A little training now will maybe reduce the unstructered programs. I must say I like the GOTO command its usually the easiest way when developing but avoiding it will make for better structures long term Herb |
||||
vasi Guru Joined: 23/03/2007 Location: RomaniaPosts: 1697 |
When the program is public, I am for a "structured" representation of the code. If not, then I have nothing against a "spaghetti" program as long as is functional(is possible to be more efficient than the other one). As for me, is always much easy to write a GWBasic program from scratch than trying to understand others programs, specially when is a long list (even if is written in a "structured" manner). Generally, every subroutine must be visibly separated in the listing(probably using REM's and a descriptive comment), all must be after or bellow the main loop(don't know what is the rule in MMBasic but I think it doesn't matter). I think a BASIC for PIC32 can be implemented in a structured manner and an example of a structured interpreted language inside a microcontroller is Pic32Lua (Lua for Pic32): [code]-- Just a stupid "SOS" LED toggle for the -- chipKIT-Max32. function cksos() local i,n local stim pic.PortDir(pic.port.A,0b1000,0b1000) pic.PortState(pic.port.A,0,0b1000) n = 0.15 stim=pic.TimerRead() while(1) do for i=1,6 do stim=stim+n/pic.TIMERTICK pic.TimerMatch(stim) pic.PortPinsInv(pic.port.A,0b1000) end if n==0.15 then n=0.3 else n=0.15 end if pic.ConsoleReadChar() == 27 then break end end end[/code] Hobbit name: Togo Toadfoot of Frogmorton Elvish name: Mablung Miriel Beyound Arduino Lang |
||||
wizard Newbie Joined: 29/07/2011 Location: United StatesPosts: 38 |
Hi, Goto less programming. I remember years ago some 'lost' software engineer carefully explain that 'structured programming' meant no goto-s were used!! The poor fellow had no clue how to make things clear and easy to understand :), LOL. We could probably also program without the plus sign. We could compliment invert and subtract. That would be simpler.... right. Are we having fun yet? Keep smiling, Wiz |
||||
Pr0f Newbie Joined: 09/12/2011 Location: United KingdomPosts: 10 |
I don't think there is anything inherently evil about "GOTO", but because it's argument is normally just a number, it doesn't lend itself to documenting the code. I program a lot in PIC 8 bit assembler (just because I like to), and comment almost every line so that I can understand what my code does - let alone someone else :) ! And so it should be with GOTO or even GOSUB statements - a comment costs nothing but a few bytes in your source code - and can instantly explain what's going on GOSUB 400 GOSUB 400 ' Get next character in the buffer So with the above it's clear that the 2nd example is much clearer - but both are the same as far as the source is concerned. And for those purists - by the time the code is running at machine level, any change in program flow will be effected by some form of branch or other - you can hide that fact all you like in procedures, functions, case structures, if then elses, do while untils, but there's still branching (goto'ing) going on ;) Just be thankful we aren't teaching people to do calculated GOTO's - nightmare they are ! |
||||
wizard Newbie Joined: 29/07/2011 Location: United StatesPosts: 38 |
Hi, This issue IS clarity. Consider all the language hacks to accomplish the 'abort' function the user expects. Abort = get me out of here and back to the main line. Lots of fun! Wiz |
||||
cmichaelcouch Newbie Joined: 23/10/2011 Location: United StatesPosts: 16 |
I learned to write "Spagetti Code" day one, can't live without a Goto statement. I found the Arduino confusing because of the looping constructs, that I never really did learn how to deal with. They just don't seem to do what I expect them to do. Calculated Gotos are very useful if you don't have to debug them, no, lol? I had a knack for debugging dbase and basic code but I found C, Pascal, and Forth code impossible to comprehend. Michael Couch |
||||
graynomad Senior Member Joined: 21/07/2010 Location: AustraliaPosts: 122 |
Apart from a few exceptional cases like error handling I can think of NO good reason to use GOTOs. They are a social pariah for good reason, mostly because you can never know if the program ever comes back to the same place. The code I sometimes see on the Picaxe forum makes my hair curl :) Now if you're just tinkering with a small program fair enough, but any large program and certainly anything written for a serious application should not use them. ______ Rob Rob Gray, AKA the Graynomad, www.robgray.com |
||||
Print this page |