24 ms·
How can C Programs be so Reliable? (2008)
- macmac 13y agoAdjusted for effort I don't know of any evidence that they are...
- dguaraglia 13y agoHow does effort affect reliability? Even if you consider one as consequence of the other ("it requires more effort to make a reliable C program than the equivalent program in $language") that doesn't make the final product any less reliable.
- Anderkent 13y agoIt does when you only have a set amount of effort that you can dedicate on a project. Which is usually the case.
- wonderzombie 13y agoI am not sure that's a useful criterion in practice, though. People are fond of saying that languages will not make you orders of magnitude more productive, and I think the corollary is that on average they will not make you orders of magnitude less productive either. (Controlling of course for characteristics of problems and such.) The problem is that there's not really a great way to reason about this, either. If you're talking about how long it takes to write a program in C vs Java, how do you measure time spent writing code vs debugging vs bug fixing, et al? All in all it's certainly possible it takes longer to write code in C, but for all anyone knows, writing more reliable code up front means fewer bugs later on. Code which is more performant by default may lead to fewer performance regressions when someone induces pathological GC behavior. Contrariwise, there's stuff like memory corruption as the OP describes, which is probably way more difficult in a native environment instead of a hosted one. It's hard not to reach the conclusion that we're all just talking out of our hats anyway (myself included).
- trapnii 13y agoit is more about the attitude of a C programmer versus the attitude of a, say, JavaScript or Ruby programmer. Whilest the latter merely assumes "nah, the VM will catch all null pointers as soft exceptions for me, and all exceptions I don't catch, my caller should." the first kind of programmer has learned (the hard way) to respect as much of the error codes a function call can return. "The hard way" is the malicious smiled SIGSEGV dynamic-programming language programmers may laughter about, because it hardly crashes your program, one may say. However, I do believe, that this attitude to think first (how to program right) rather than trying to remember (what could have caused that many 500's on my HTTP server) which I think is the better approach to write more reliable software. Dynamic languages are said to be more convenient for web sites, for example, I'm not denying that one, but those languages just shift the problems back into the future, where, when time has come, you may or may not be willing to attempt to fix the bug you introduced days/weeks/months ago, depending on the urgency. On programming environments (such as C), where types are more statically typed, errors have to be handled manually and with caution, memory has to be managed (more or less) always with an ownership in mind, those programs, that think about these topics from the very beginning, and iron out those remaining bugs over time, are - from my point of view - the more reliable ones. So I can out of my distance second this blog post. It was interesting to read.
- nkarpov 13y agoThis really does depend on how "reliable" is defined. At the code level, reliable is dependent on the actual programmer. This code must then be compiled/interpreted in order to to actually function. At this stage the programmer has lost control unless they are also in direct control of the interpreter/compiler - which is, of course, impractical and extremely unlikely. Inevitably writing in high level languages sacrifices control for how the program looks at the machine level simply because you must depend on existing structures. The higher up you go, the less control you have of the final product. Writing in Python vs. C means that you have to think less about lower level issues and consequently are subject to problems that arise outside the scope of what you could have originally controlled - simply because you didn't have to think about a subclass of issues that you assumed were inherently solved for you. So yes - if you take a Python program and rewrite it in C, you can get a more reliable program IF you properly solved the set of things that Python took care of for you in relation to your specific problem. But this relies on the fact that you succeeded in rewriting what Python took care of for you. So we're back to where we started - it all depends on the programmer. The article implies this in the final sentence: All that said, I don't recommend using C unless much thought has been given to the decision - the resulting software might be reliable, but it will have taken a significant human effort to produce it. And this basically negates that C is any more reliable than whatever other programming language. Despite the fact that I think a discussion of reliability of a language is effectively useless (at least the way I've framed it here, I'm open to other interpretations), I enjoyed the article - thanks for sharing!
- sillysaurus2 13y agoTarsnap is the classic example of a C program whose reliability almost defies belief. Its codebase is large; it solves a very hard problem; and it manages to handle almost every cornercase flawlessly. (As of the latest version, it seems to handle every cornercase flawlessly, as far as I know.) But I've always wondered: if Colin were as experienced with Python as he is with C, would Tarsnap be better served by writing it in Python? It seems like the codebase would be half as large. In other words, is C the reason why Tarsnap is good, or is it because of Colin? Or is it both? Example: Lisp, along with PG, is the reason why HN is good. Many of HN's features simply wouldn't be easily accomplished without it, so they wouldn't ever have been implemented. But I'm not sure the same could be said of Tarsnap. But let's be honest - in how many languages can one retrospectively add a garbage collector? Many. Lisp in particular. HN's links like /x?fnid=KxPuWC6qnkl4nxcUTDh4xs are an example of this.
- stinkypete 13y ago/x?fnid=KxPuWC6qnkl4nxcUTDh4xs resolves to "unknown or expired link". Do you have a full URL?
- zb 13y agoIt got garbage-collected. That's the point.
- stinkypete 13y agoGotcha. The parent comment made it seem like that was a link to information about retrospectively adding GC to other languages, especially given that Lisp already has GC as nearly an assumption at the language level. But I can see what he was trying to get at.
- elwell 13y agohaha, exactly
- nilkn 13y ago> E.g. Lisp, along with PG, is the reason why HN is good. Many of HN's features simply wouldn't be easily accomplished without it Could you expand/elaborate on this? I've never viewed HN as particularly complex, just tastefully designed with a good community.
- notacoward 13y agoThe author makes a good point about the discipline imposed by not having exceptions. Programmers tend to write code in one of two modes: the quick-and-dirty mode where you consistently don't check return values, or the built-to-last mode where you consistently do. If you start in the first mode and have to fix a bug, you often have to add error checking all up and down the call chain from where it occurs to where it's handle, so you very quickly migrate toward the second mode. By contrast, programmers in exception-based languages often get to a state where they catch some exceptions in some places where their tests have found bugs, but large sections remain in quick-and-dirty mode. The code tends to keep running longer after the actual error, leaving latent bugs that are harder to debug when they actually surface. The kinds of errors that are characteristic to C, such as segfaults from bad pointer handling, have obvious effects and more often have obvious causes as well. I'm not totally anti-exception, and I think manual memory management is only appropriate in an increasingly small set of situations. I'd rather program in Python than C when I have the choice, but I acknowledge that the Python code I write is probably a bit less robust than the C code I write. I'm probably not alone. Theory aside, in practice the greater prevalence of sloppy error handling in languages other than C seems to leave the field pretty level when it comes to robustness.
- dllthomas 13y agoI wonder if that's a general "water seeks its level" thing. We only care so much about robustness, and start to trade off in favor of it at some threshold regardless of language...
- adamnemecek 13y agoYeah, I think that people are starting to realize this. E.g. Go and Rust don't have exceptions (or Java/C++ style exceptions).
- dllthomas 13y agoI've been thinking this might be well ameliorated by using checked exceptions, but even languages that have them don't get sophisticated with them, so they wind up being painful enough they're not really used (in my experience). For instance, I don't know of a language that lets you say "This function accepts a function (f) and returns a function which throws anything f throws except FooException."
- dllthomas 13y ago"[Pointers are] arguably the trickiest concept in low-level languages, having no simple real-world analogy[.]" I'm not sure how "address" is not a good, "simple real-world analogy" for pointers. It may be that I've just internalized enough of the behavior of pointers that I'm glossing over important differences, though... I struggled with pointers long enough ago that I don't remember struggling with pointers.
- breadbox 13y agoFor a lot of people I think it takes a while to really internalize the ramification of the fact that you can have a pointer to memory you're not allowed to use. (That, plus the way they're used in C as a cheap answer to templates/generics.) I understood that a pointer was an address from the start, but it was still some time before I figured out how to use them correctly.
- mannykannot 13y agoC implements a fairly low-level but general model of how a computer works, and a programmer's understanding of pointers is something of a proxy for their understanding of how that model works. Therefore, C has a sort of built-in test for a level of competence in a programmer. Unfortunately, it is by no means perfect, and you can still find people writing in C who have no idea why they should not return a pointer to an automatic variable.
- dllthomas 13y agoSure... but I don't really see that that's a problem with the analogy. I'm limited in what I can do to someone else's house, even if I have the address; an address might persist after the house has been bulldozed and replaced; &c...
- breadbox 13y agoIt's not really a problem with the analogy; I'm just saying that a good analogy is no cure for a lack of experience. My first non-toy C program was a disaster because I knew what a pointer was but I didn't yet understand how to manage them.
- akkartik 13y agoThe author's point is weakened by the fact that the Converge VM is now (5 years after this article) written in python: https://github.com/ltratt/converge/search?l=c https://github.com/ltratt/converge/search?l=c (as of https://github.com/ltratt/converge/tree/08dadda29b/vm https://github.com/ltratt/converge/tree/08dadda29b/vm). Compare https://github.com/ltratt/converge/tree/converge-1.x/vm https://github.com/ltratt/converge/tree/converge-1.x/vm. "there are some obvious reasons as to why it might be so reliable: it's used by (relatively) large numbers of people, who help shake out bugs; the software has been developed over a long period of time, so previous generations bore the brunt of the bugs; and, if we're being brutally honest, only fairly competent programmers tend to use C in the first place. But still, the fundamental question remained: why is so much of the software I use in C so reliable?" Why could the fundamental question not be explained by all the points he just mentioned? I'm a C programmer, and this guy is hitting all my biases, but this isn't very well thought-out.
- gabriel 13y agoI believe he explains this switch here: http://tratt.net/laurie/blog/entries/fast_enough_vms_in_fast_enough_time http://tratt.net/laurie/blog/entries/fast_enough_vms_in_fast... It's a very good read. IIRC, the reasons aren't really rooted in the language that is used. It's more about the tooling provided by PyPy for constructing a compiler.
- masklinn 13y ago> The author's point is weakened by the fact that the Converge VM is now (5 years after this article) written in python Kinda, it's written in RPython, a restricted and statically inferable subset of python built specifically to write VMs.
- eliteraspberrie 13y agoThere are idioms and methods for writing reliable C programs, which you learn with experience and reading better code than yours. If you learned languages in the order higher-to-lower, you eventually understand that you cannot write a program in C at the same pace as in a higher level language. What has worked for me is: always have a second terminal open for the manual pages and the standards; and to regularly read quality C source code. Opengroup.org has thorough references for the standard C library, including its relationship to POSIX and the SUS; and the various kernels and core Unix utilities are always a good read.
- rzimmerman 13y agoAlso, compile-time checks and the tendency of lower-level languages to fail catastrophically rather than subtly or silently plays a part. The author also has a lot of experience to draw on, which might just make his work more solid and error free in the first place.
- plg 13y ago"Once one has understood a concept such as pointers (arguably the trickiest concept in low-level languages, having no simple real-world analogy) " huh? So when you write a letter to someone, you actually attach their house onto the outside of the envelope? No of course you don't. You write their ADDRESS on the envelope. All this lore about how difficult pointers are, is bunk. The syntax of pointers vs addresses vs dereferenced pointer values, can be difficult at first ... and can be very difficult if you read and/or write unecessarily obfuscated code ... but there is no need.
- ProAm 13y agoSometimes I just write the contents of their house on the outside of the envelope. You have to admit it is one of the trickier concepts people learn when programming. Once you understand it, it's definitely a "Duh, how else would it work?" moment but getting to that point can take some time.
- Scramblejams 13y agoI agree. The concept may be simple, but when you're just starting out as a C++ newbie and you've got pointers-to-pointers-to-pointers flying all over the place in this ugly syntax trying to satisfy some contrived homework assignment on a short deadline, things can get confusing rather quickly.
- avifreedman 13y agoAgreed, in theory - in practice, I find that explaining mov r1, r2 vs mov (r1), r2 vs mov (r1)+, r2 followed by an hour of writing assembler programs to do basic things with null-terminated strings is usually needed to help people learning C to quickly internalize what that means. Something about * and + and - around the var name seems to become easier when they can say "oh now I see; *c++ is like (r1)+".
- mhenr18 13y agoYeah, I've never gotten why people consider pointers to be hard. If you consider memory to be a massive array, pointers are just the indices that you use to get at different elements in that array.
- wonderzombie 13y agoThese pieces are the reason why I still read HN today. Thanks for submitting it and thanks for upvoting it, everyone.
- lmm 13y agoWalking across a tightrope is a lot easier than you would think. When you're walking on a tightrope you really concentrate on your walking; it's stressful, but not actually hard. Still, I don't think many people would choose to walk across a tightrope when an easier option was available. Exceptions, meanwhile, allow a counterintuitive but effective strategy for greater reliability. My boss likes to say that the cloud is great because machines fail more often - the point being that rather than the impossible task of making individual machines failure-proof, you instead design systems that can handle the failure of any given machine. Often the best way to make a resilient system is to break the problem into quite coarse-grained blocks, and then define appropriate responses (such as failing a single message and processing others, or retrying the whole block) whenever a given block fails.
- peterashford 13y agoSpeaking as someone who has been a long time coder in C and Java, I think the point he makes about checking errors is BS. You have to handle exception in Java and you're perfectly free to consider all the possible failure paths if you want though typically you'll handle the ones you can sensibly do something about and ignore the rest (i.e.: log or just fail outright depending on context). I do NOT miss the C world where you have to handle the results of every single function call if you want to be safe. The exception handling approach IMO is generally superior - the normal code executes simply and the exception handling code gathers all the exception cases together. Much better than interspersing both and especially better than handling the same error 10 times in a row rather than just doing it once. That said, I love C. It's a very simple language and that's a virtue.
- strlen 13y agoThere are multiple factors that contribute, but here's a few that I haven't (to the best of my recollection) seen mentioned so far: tooling (crucial), "do the simplest thing that could possibly work" attitude brought about by (lack of) a built-in collections library and simple syntax, and lack of rapidly changing requirements during development. Tools for C are powerful, mature, and available on near every platform. Every single C programmer that I know uses multiple memory debuggers, for example: e.g., valgrind for leak checks and more on smaller code segments, LLVM address sanitizer on unit test runs, jemalloc or tcmalloc/google-perftools, and more. "Do the simplest thing that could possibly work" is somewhat close to the author's point: simply put, while there are widely used cross-platform hash tables in C, it takes more effort to use one; hence, they aren't used e.g., when N is known to be 100 (or if maximum size is bound, there's likewise no pressure to use the built in hash table/red-black tree and one can use perfect hashing instead...). In another big distinction I see between code I wrote in C vs. higher-level languages is that one can simply use a stack allocated variable length array -- or (if random access doesn't happen) a linked list in place of a heap allocated resizable array. These exammples, I think demonstrated how C discourages you from interpreting "the simplest thing that could possible work" as "the thing that is fastest to code"; that's not to say development speed is not an issue -- it's a _huge_ issue! -- but it often leads to code where the unneeded complexity (such as using a red-black tree or a skip list instead of a sorted stack allocated array!) is hidden from the programmer but is nonetheless present. Onto the last point, I agree sentiment expressed by others questions the premise. I think the examples of C programmers where we explicitly know of something as being a C program are heavily biased towards well known and open source software; open source software, language runtimes, operating systems, browsers (usually in a clean and limited subset of C++ like, e.g., Chromium and likely Firefox as well -- I only mention Chromium as I've re-used base libraries from its codebase), and so on. We don't, however, think of parking meters, vending machines, and many other usually embedded (but not life critical) systems that -- unlike open source systems software -- are built from unclear, confused, and ever changing business requirements ("well, this city introduced a new red-zone, so now we have to make sure to charge extra 0.33 cents between hours 8 and 13 except on all Holidays, but not July 4th"). Yet when people bring up Java, it's easy to forget about gmail or Minecraft, but instead to think of the insurance claim management system that you had to use after your rental car got rear-ended near 4th and Harrison, that crashes with a user-visible stack trace when you click the wrong button and (even in 2013) doesn't use Ajax and requires opening a new page (slowly, as they don't use Java's built in thread-pools but spawn new threads, because the system has to run at a customer site that uses Enron's custom implementation of Java 1.3 on an AS/400) to make an changes.
- azov 13y agoPopular C software is so reliable because of enormous amount of effort spent developing, testing, and polishing it over many years - not because error handling in C is superior to that in higher-level languages. If your python script sticks around for 30 years constantly being used by millions of people - I bet it will be rock solid as well. All other things [1] being equal [2], a program written in a higher-level language will likely be more reliable - simply because it will likely be shorter. C has many properties conductive to great longevity of software written in it, but error handling is not one of them. Now, to the rant about not knowing the exact number of exceptions a function can throw. Most sane languages [3] give you exception hierarchy. You know that every function can throw an Exception (or Throwable, or whatever the root of all things evil is called in your language), and you either handle it or let in propagate. The important bit is you catch exceptions based on what you can handle in a current context, not on what the functions you call can throw [4]. As long as you realize that every call can potentially throw, it is not important what exactly is thrown - letting you abstract away the exact nature of an error is a feature, not a bug, that's what exceptions are there for! Let's say some function deep below changed and now throws a new exception type. So what? It didn't change what you can handle, so there's no reason to modify calling function - just let the exception propagate. What if nothing in your program can handle that exception? You let it crash (hopefully to the debugger, and no, it's not ironic that I suggest it in a topic about reliable programs - crashing is a reliability feature invented to expose problems that would be hidden and hard to debug otherwise). Of course you may also put a few catch-all clauses in a very limited number of strategic places. If you approach exceptions this way they are vastly superior to error codes. [1] Scope of the problem, amount of effort spent on development, programmer skills [2] Or at least on the same order of magnitude [3] With a notable exception of C++ - I'm not sure what they were smoking when they decided that throwing arbitrary values is a good idea [4] And this, by the way, is what's wrong with checked exceptions in Java PS. Didn't mean that having no exceptions is insane - but that if a language has exceptions, the sane thing to do is to organize them in a hierarchy with a single root.
- deleted 13y ago[deleted]
- ante_annum 13y ago
- ChuckMcM 13y agoIt is an interesting observation. I have found that scripted languages are unreliable as a function of loadable modules (which is to say they depend on some module that gets loaded, that at some point changed, and then broke the script). C has that problem too with shared libraries but there are generally fail stop policies in place (if you can't get the version you were linked against exit rather than try to valiantly proceed) What is perhaps most important is that software does not "rot" in the sense of building materials becoming weaker after exposure to the elements. But it does fair poorly against changes in things it assumes to be true. Some languages, like Ada or Mesa, which have very detailed configuration specifications, achieve high reliability at the cost of high configuration specificity. That makes them brittle against even modest changes in their environment. The art is striking the balance. Take something like thttpd, 20 year old code still compiles, still does exactly what it always did (simple HTTP web server) very reliable. But the barest minimum of reliance on the ambient environment it runs in.
- rmrfrmrf 13y ago> I have found that scripted languages are unreliable as a function of loadable modules (which is to say they depend on some module that gets loaded, that at some point changed, and then broke the script). That's why I'm thankful for a lot of these newfangled virtual environments and machines along with their respective package managers (and why more people should use them!).
- codex 13y agoI should note that less experienced programmers tend to start with higher level languages, as a rule, so the average experience of a C programmer is likely higher than that of the HLL programmer. This general experience level alone gives the resulting code a reliability boost.
- jnbiche 13y agoWith notable exceptions, I think this is true. I've been coding seriously for 2 years and am starting to move down the stack. I see others in my cohort doing the same thing.
- discreteevent 13y agoSome good points in the essay but I have to admit that for me the reason I like C is that it's the first language I ever really did any serious programming in and the first language that made me think hard. Because of that it just feels like home when I come back to it and I can never really evaluate it without that bias. Its the simplest model of the machine that you can get without going down to assembly. Its the same reason I like riding and fixing bicycles. In both cases sparse resources have kept the design to the bare minimum.
- Nursie 13y agoBecause those of us who have been doing it for 15 years or more know we're tangling with tigers and have figured out how to use their strength against them...
- wfunction 13y ago> "Surprisingly few people can really program in C and yet many of us have quite strong opinions about it." Maybe they have strong opinions precisely because they don't find C to be a language which is easy to "really" program in?
- puppetmaster3 13y agoC programmers have better culture. Than say Java programmers. If I was a swordsman and had a rusty sword and you had a new one, the sword does not decide the fight.
- burstmode 13y agoYes, mainly because new sword has an auto-grinder welded to it that might start running at any given moment, even if you are in a deathly fight :-)
- blahbl4hblahtoo 13y agoSince everyone has access to decent c toolchain these days you tend to see this over and over again. The "wisdom tradition" of UNIX? There is some kind of need for this kind of counter factual "the ancients had powers we can't believe" thing... the thing is that languages and frameworks surface sets of techniques for solving problems that if not present end up needing to be reinvented... this goes for C and UNIX, mainframes, os400, erlang...whatever. Maybe I'm just being contrary, but it seems to me that the lessons I learn in one language apply to all the others at least in some facet. I worked my way through SICP last year and it really changed the way that I think about code...while also not making me a lisp nut. It seemed to me that lisp could have been anything else and the techniques would still be there.
- sytelus 13y agoC also has "better" I'd argue that average person programming in C is more matured, and understand nitty gritty of programming, computer science, performance and how things work at low level far better than, say, Visual Basic or even Java programmer. I think we have been putting too much faith in to languages. After certain level of sophistication, the weight of quality shifts to person programming in it instead of language itself. However most language designers would tell you that their holly grail is to shift this threshold so programmers can be as lower in skills and ignorant as possible while quality of program as high as possible. Lot of these things can be also said for JavaScript. I've now coded fair bit in plain JS constantly avoiding temptation to use some other language that compiles to JS (and even avoid TypeScript for that matter). Just 100% pure JavaScript. And I can tell you that my habits and flow is very different when I code in JS. Back then I though coding in pure JS would blow everything up just because of lack of types or silly thing like missed semicolons or as simple stuff like forgetting to pass 10 in parseInt. The fact is that none of these bothers me at all anymore. I rarely find bugs that can be attributed to these deficiency in JavaScript. Strongly typed languages now feels like some kind of over hyped hoax.
- deleted 13y ago[deleted]
- drdaeman 13y agoC programs are so damn reliable I spent last three weeks mostly trying to debug others' code (from multiple software projects - I was lucky enough they decided they'd all start hitting obscure bugs at approximately same time) and waiting for it to crash again. Just imagine my feelings when a critical server that ran just fine for 3 years gets a bit more load and starts getting assertion failures. That was a known and already fixed bug, but after version bump to next stable those had changed to SIGSEGVs - and it's not trivial NULL pointer dereferencing but a weird concurrency issue. C has neither [relatively] strong guarantees of OCaml and Haskell (so I could be at least sure there're no trivial bugs with that weird macros doing crazy pointer juggling), nor dynamic flexibility of Python and Erlang (so I could hack working runtime live, letting it fail and hot-swapping code at will).
- zvrba 13y ago> it's reliable in the sense that it doesn't crash, and also reliable in the sense that it handles minor failures gracefully. This is an incomplete definition of reliability. It seems that the author is only considering programs which work as advertised when used as intended. Most software is written optimistically, i.e., the "good path" is tested most. As soon as you start feeding the program with invalid input, it is no longer reliable, as witnessed by the myriad of low-level security bugs (stemming from the nature of C) in virtually every nontrivial C program ever written. Now, would you rather have your program crash (be "unreliable" by the author's definition) or end up running arbitrary code injected by an attacker? (Security exploits are the most prominent example; many programs will do something weird when fed unexpected input.)
- yason 13y agoI've never in my life written as careful and premeditated code as when I was writing C on my Amiga when I was young. One miss and you would tilt the whole system. There was no memory protection and everything was system wide. If stuff failed, you had to deal with it because if you didn't you probably left something unlocked or left a resource open which hogged it and prevented other applications from using it. And anything left open would leak memory as well. And there was only system memory so other programs would eventually fail memory allocations because your program didn't close some handle somewhere. Surprisingly, most of the programs I wrote worked correctly early from the beginning. There was no "I'll just code this thing up and fix bugs later" phase because there were absolutely no safety nets. And fixing bugs in retrospect usually meant rebooting your machine, starting up the editor and compilers again for another round. I'm pretty pedantic on Unix too but I'm still a lazy bastard compared to those times. And my C programs on Unix do take a whole lot more iterations to "stabilize" than those I wrote on my Amiga. In some perverse masochistic way I somehow miss that.
- chalst 13y agoKent Pitman's idea of "languages as political parties" [1] seems to me to offer the best perspective on the surprising reliability of C. The core community shaping C over the years has been: 1. Unix-centric, so the community has a coherent view on the preferred sort of semantics offered to users of code; 2. Applies Kernighan--Ritchie--Pike coding style that gives a clear "code smell" [2] for C code; and 3. Is the natural home of "Worse is Better", which favours source-code simplicity over specification simplicity. For example, the "C party" favours procedures "succeeding" with non-zero exit codes on failure where other language parties have language constructs to represent failure. This can be better from the point of view of source-code simplicity, both in code and compiler, than exceptions, and worse in terms of semantics. The LK coding style, among many other C coding styles, prefers the use of goto (or sometimes setjmp/longjmp) to handle exit status [3], which is frowned upon in nearly all non-C programming language communities; the restriction of goto/longjmp to handling errors in this way avoids the problems Dijkstra pointed out [4,5], and correct usage of goto/longjmp in properly checking exit status has good code smell if generally accepted coding conventions for the use of goto to handle failure are followed. There's a nice fragment of code illustrating the idea on SO [6]. [1]: Pitman 1994, Lambda: The ultimate political party, http://www.nhplace.com/kent/PS/Lambda.html http://www.nhplace.com/kent/PS/Lambda.html [2]: https://en.wikipedia.org/wiki/Code_smell https://en.wikipedia.org/wiki/Code_smell [3]: From http://www.tux.org/lkml/ http://www.tux.org/lkml/: So now we come to the suggestion for replacing the goto's with C exception handlers. There are two main problems with this. The first is that C exceptions, like any other powerful abstraction, hide the costs of what is being done. They may save lines of source code, but can easily generate much more object code. Object code size is the true measure of bloat. A second problem is the difficulty in implementing C exceptions in kernel-space. This is convered in more detail below. [4]: Dijkstra 1968, A case against the goto statement, http://www.cs.utexas.edu/users/EWD/transcriptions/EWD02xx/EWD215.html http://www.cs.utexas.edu/users/EWD/transcriptions/EWD02xx/EW... [5]: Knuth, Structured programming with go-to statements, http://pic.plover.com/knuth-GOTO.pdf http://pic.plover.com/knuth-GOTO.pdf [6]: http://stackoverflow.com/a/741517/222815 http://stackoverflow.com/a/741517/222815
- Davidarch 13y agoGood Resource!