7 ms·
One thing that stood out for me was the vehement "no" to goto. That's a little too harsh, one thinks?
by mangamadaiyan 14y ago
One thing that stood out for me was the vehement "no" to goto. That's a little too harsh, one thinks?
- jlgreco 14y agoThat is anything but an uncommon stance.
- andrewcooke 14y agofor C? it's very commonly used to jump to implement "catch" (jump to clean-up on error).
- _kst_ 14y agoYes, but there are other ways to implement error handling -- and a ban on gotos is a lot easier to check and enforce than a ban on just "bad" gotos.
- jlgreco 14y agogoto is commonly used that way in C... and it is just about as commonly straight up banned in C. I am not defending the bans, but find it strange that people would be surprised by them.
- andrewcooke 14y ago> I am not defending the bans, but find it strange that people would be surprised by them. yes, i think a lot of replies here are speaking past each other, making valid and not actually contradictory points, but not really replying to each other. if you read the entire doc it is clear that they are pushing c in a very safe, but somewhat unusual direction. there's no dynamic memory for example, so the main reason that most of my c code uses goto - to free memory on failure in a "catch" - is irrelevant. taken as a whole, it's not what i would call normal c use, and i don't think it's very useful for most other people as a guideline, but it is internally consistent and, for the specific use case, reasonable.
- revelation 14y agoAnd most of it seems to stem from the old "goto considered harmful" that has been spread from generations to generations. gotos have a pretty important niche in error handling blocks. You can see them all over the linux kernel, arguably one of the biggest (and most successful) C projects out there.
- mturmon 14y agoWe're talking about flight software, not software in general. Implying that the people who drafted this guideline were blindly accepting dogma is simply wrong. Look at the list of contributors (page 5) -- it includes Ritchie, Kernighan, and Doug McIlroy -- in addition to several others who have spent their careers writing flight software.
- jdpage 14y agoThese are guidelines for code which controls multi-million-dollar spacecraft. "No recursion" and "always predictable loop bounds" are also incredibly harsh, but they are helpful for verifying correctness.
- Zenst 14y agoActualy it is very wise. I started out doing COBOL with Jackson Structured Programming standards and in that when I started my first job I was totaly unaware of the goto verb. Even got told to use it. This was years ago and real-world standards have caught up with education standards, maybe not cutting edge but at least on a sharp surface if you know what I mean. With C I have never used a goto, sure you can compact your code, but is it managable later on by somebody else and by avoiding goto's you also tend (at least I have found it to be so) to get more structured, easier to follow code. Also smaller functions albiet more of them as well I'd say from what I have experienced. Also remember a goto may be fine for what the program is to do today, but what about down the line and changes. In that as much structure and in that control is the ideal. Some might say if you want to code goto's then code elsewere in assembler. It is harsh, but there again so is space (sorry had to say it).
- silentbicycle 14y agoDo you write parsers or state machines very often? goto has its uses. It shouldn't be used indiscriminately, but (especially since C does not have tail-call optimization) it is the best approach sometimes.
- stephencanon 14y agoIt's a bit strong to say that C doesn't have TCO. The language doesn't require TCO, but compilers are free to implement it (and indeed many of them do).
- silentbicycle 14y agoBut you can't depend on it. Since TCO changes the fundamental order of growth for space, it makes a huge difference. Embedded C compilers typically do not have TCO.
- Zenst 14y ago"Do you write parsers or state machines very often?" Nope, not at all. So I can't comment further on that area beyond saying for every rule there is an exception - that is the rule. As a rule goto's are bad in highlevel languages.
- jrockway 14y agoThe go to statement is considered harmful.