7 ms·
If you’ve ever actually written more than toy C, you’ll know gotos are essential for resource cleanup and error handling. Even the famous goto fail was not a go
by dcow 4y ago
If you’ve ever actually written more than toy C, you’ll know gotos are essential for resource cleanup and error handling. Even the famous goto fail was not a goto error, it was a block error (named because a cleanup statement `goto fail;` always executed because of a brace-less if). goto can be abused like any other language construct, but it’s uniquely useful and makes code more simple and easy to reason about when used correctly. You just shouldn’t be using C any more unless under duress.
- preseinger 4y agoof course nobody should be writing C any more, for exactly the reason you're demonstrating in this comment
- raldi 4y agoWhat language do you think Linux and Arduino should be programmed in?
- pjmlp 4y agoArduino uses C++, thankfully.
- raldi 4y agoEverything in the top-level comment applies to C++, too.
- pjmlp 4y agoMy "thankfully" remark shows my point of view. The only sin of C++ is the same that plagues evolutionary languages like Typescript, Kotlin and so forth. No matter how many tools they provide to write better code than the language they have grown from, their compatibility with them is like hearding cats while trying to have everyone adopt best practices. Otherwise in regards to Arduino, I would suggest a couple of nice BASIC and Pascal compilers like those sold by Mikroe.
- saagarjha 4y agoIf it was created today? I’d go for Rust and Python respectively
- deleted 4y ago[deleted]
- eric__cartman 4y agoThe ATmega 328P an Arduino Uno or Nano uses has 2KB of memory and 32KB of flash storage for the program. Having some sort of micropython interpreter there would be impossible, and even if it could be achieved, somone still has to write the low level C/ASM code to make it all work. For many embedded systems, low level languages like C or C++ (perhaps Rust for a lot of ARM micros) is the only sensible choice. If it's not a multi user system that's connected to a network, people trying to abuse memory unsafe code isn't a concern.
- deckard1 4y ago> has to write the low level C/ASM code to make it all work shhh. Let's not disturb those that believe in the GC fairy that sprinkles their code with safety magic late at night while they soundly sleep. Firmware doesn't exist. I can't hear you. Na na na na na na na
- pjmlp 4y agoToo late, here is Firmware being written in Go. https://www.withsecure.com/us-en/solutions/innovative-security-hardware/usb-armory https://www.withsecure.com/us-en/solutions/innovative-securi...
- SOLAR_FIELDS 4y agoI hope Rust takes off for embedded programming. I've been working with ESP32 and C is really the only choice if you are doing anything remotely fancy or resource constrained
- 4y ago
- JKCalhoun 4y agoIf you're steeped in C and its quirks, have good coding patterns that allow for it, I say knock yourself out. I'm absolutely coding in C again — writing some old-school games using SDL. After working for decades with various retain/release, garbage-collected, magic-memory™ languages, going back to C feels like programming again. I like it.
- lmm 4y ago> If you're steeped in C and its quirks, have good coding patterns that allow for it, I say knock yourself out. Somewhere between 95% and 100% of people who think they're "steeped in C and its quirks, have good coding patterns that allow for it" write code that invokes undefined behaviour.
- otabdeveloper4 4y agoThere's nothing wrong with undefined behaviour.
- preseinger 4y agoUndefined behavior means the language provides no guarantees about the outcome. Whatever happens is up to the compiler, or rather the specific implementation of the compiler, and the target architecture on which the program is run. These things are not constrained by any spec or guarantee, so they are allowed to change, for any reason, to anything. These changes may be triggered by not only different execution environments, but also changes to properties of a given execution environment. The behavior of a program with undefined behavior is non-deterministic, and cannot be predicted, modeled, or effectively maintained.
- otabdeveloper4 4y ago> Undefined behavior means the language provides no guarantees about the outcome. No, not "the language", but "the standard". Languages like Python or Rust that don't have standards are 100 percent undefined behavior. If you're really worried about this, then just code against x86_64 clang and not ISO C++ and forget about this non-problem forever.
- oconnor663 4y agoI don't think it's essential. I think it can be a reasonable choice, but there are other patterns that can accomplish the same thing, with some upsides and some downsides: https://gist.github.com/oconnor663/b0b32f25301ed5a1af4d07053edf7036 https://gist.github.com/oconnor663/b0b32f25301ed5a1af4d07053...
- pjmlp 4y agoNot really, I have deployed plenty of C code into production and never used gotos, other than special flavoured gotos like UNIX signals. My problems have always been in how memory corruption friendly C happens to be, nothing to do with how to use structured programming practices for resource cleanup.
- jstimpfle 4y agoI don't see any similarity between gotos and signals at all. Gotos are just explicit jumps, while the "normal" structured way of doing jumps is to have them inferred from nested regions (that double as scopes and lifetime guards for automatic variables). The structuredness of such inferred jumps usually is both sufficient and convenient, but sometimes being extremely structured leads to boilerplate and inefficiencies. That shows already with early returns from nested scopes, which aren't widely frowned upon, while they are very similar to common usage of goto. I would say I haven't encountered a goto that isn't basically an early return from some nested scope to after some parent or grand-parent scope. In that sense, a goto can save from having to extract a nested scope as a stand-alone function just to write it as an early return. I would say that many gotos in the wild could be rewritten as early returns after making a standalone function, but maybe sometimes this is too much of a hassle, or, subjectively, puts a toll on readability.
- pjmlp 4y agoSignals are implicit GOTOs in the sense of INTERCAL's COMEFROM. As for structured handling, instead of gotos all over the place, do inverted conditions for early returns similar to what Swift has done with guard statement, embrace functions for resource cleanup, if the cost of a jump brings cold sweat, have them inline and called alongside a return, most compilers will replace calls with jmp opcodes.
- jstimpfle 4y ago> Signals are implicit GOTOs in the sense of INTERCAL's COMEFROM. What? > guard How does it help to have an inverted if statement? Not sure what is the point, can do without. Goto isn't necessarily for resource cleanup. The common usage is as an early break from the currently executed block, to what comes after the block. Which is often cleanup code, but not necessarily.