Live data from Hacker News

How can C Programs be so Reliable?

tratt.net

71–80 of 105 posts

Re: How can C Programs be so Reliable?

#71

> What I realised is that neither exception-based approach is appropriate when one wishes to make software as robust as possible. The author misses the main point of exceptions: they let us separate data processing code from error handling code. This is why we are more productive in languages that have exceptions and our code is easier to maintain. C requires us to handle errors throughout our program, tightly coupli…

I have to disagree. From my perspective it is an error to think that error handling can be handled separately from the main code path. The classic example that I use to demonstrate this is to point to all of those tedious discussions that programmers have as to whether something is an exception or just normal behaviour of a system. Straight away for me that's a red flag that a non-real distinction is being made.

I honestly don't see any advantage to exceptions over C-style return codes, with one important ...euh... exception: the boiler plate for exception handlers can be well handled by modern IDEs. All the other supposed advantages seem to be just waffling to me. Take the whole 'Oh, but exceptions make handling errors the default!' kind of argument (several examples on this thread already). Yes, sure, you do have to write exception handlers for all errors in a language such as Java. But my experience is that if I'm writing use-once-and-throw-away in C, I'll just not use the return code. In Java I'll just stick a whopping great big try/catch around the whole app, and be done with it. If I'm trying to write stable code that's going to be around for a while in C, I check the error codes returned by a function every single time, which gives me around about as much work as when I am using Java, and actually handling different exceptions correctly.

All of which means, for me at least, that exceptions don't add anything to a language, but they do make the language just a little bit harder to learn (remembering exactly how any given language has implemented exceptions, and which resources can still be safely used when is a pain, as each language tends to have subtle differences that can bite you).

Re: How can C Programs be so Reliable?

#72
post #64

Earlier quoted context omitted.

You seem to forget that in embedded systems, C is still the most used language. So most of the new systems developed these days have new C code. And embedded systems are everywhere. Also a big part of embedded systems have to be reliable for years in hostile environement without external interventions. so I wouldn't say that this is easily predictable subsystems.

Hmmm, I've worked in the embedded world for the last 10 years, and I can tell you that the bulk of embedded code is far from hardened. It appalls me the number of times that mobile phones, set top boxes, etc crash. Typically, reliability is low because it is hard to run decent unit tests in an embedded environment. You're obliged to write a type of simulator that you can run on a PC, but the simulator never has the e…

Yes, this is true: A lot of embedded code is poorly tested. Even more is poorly designed and written by poor programmers (like most software.)

Some people have to deal with the machine, and C is a good model of the machine. Even reliable systems have some core in C (or an equivalent) that provides an abstraction where higher level abstractions can be expressed.

Testing isn't the ultimate solution; some people are just able to use the machine better and produce better code. Testing crappy code doesn't really help. A subset of developers produce tools where others can produce systems with less risk by providing appropriate abstractions.

Re: How can C Programs be so Reliable?

#73
post #53

This misses two points I consider important: - A whole lot of C code is old. Old, actively maintained code, tends to be more reliable than new code. - C tends to be employed in relatively predictable sub-systems. Something like a device driver has a relatively predictable set of states relative to a GUI application. There aren't that many paths. C code for GUIs, in my experience, tends to be at least a buggy as code…

I disagree with both these points:

In some cases old, actively maintained code is more reliable. However, I have seen many cases where old, actively maintained code (depending highly on the quality of the maintainers) has lost reliability because the original principles of the codebase have been lost.

Device drivers (especially in multiprocessor systems) have very many unpredictable paths. In my experiences in writing devices drivers and GUI code code (as well as lots of code in the middle), device drivers are more likely to have the unforeseen codepath. With GUI code, you can constrain concurrency so that possible codepaths are also reduced.

Re: How can C Programs be so Reliable?

#74

Earlier quoted context omitted.

But oftentimes I want a correct result or an obvious failure, without having to handle tons of errors I don't even want to account for.

Returning -1 is an obvious failure. What you're saying is that you want your programs to blow up when a function returns a known error (such as "disk full"). One of the points of the article is that C makes you think about all the possible, well-documented, errors that each function can encounter. You know when you write C precisely what can go wrong at each stage, and decide the right way to handle it. Seeing a dial…

The difference between letting an exception bubble up and seeing a system call fail and aborting is pretty small. Either way, you need a higher level way of dealing with it.

One of the most interesting papers I read was "Why computers fail and what we can do about it" by Jim Gray while he was at Tandem. Transactions are essential, and most failures are transient. This can be applied at many levels, and can deal with many failures without the developer of the mainline code having to decide what to do for each given failure.

Re: How can C Programs be so Reliable?

#75
post #46

Earlier quoted context omitted.

But oftentimes I want a correct result or an obvious failure, without having to handle tons of errors I don't even want to account for.

Exactly. In properly written C, about half your code is checking for errors and handling them explicitly. If you don't do that, at some point it will blow your foot off; you have to account for those errors. C programmers have generally accepted this and thus spend an excessive amount of coding time thinking about and writing recovery mechanisms for external errors -- and that's why so many C programs are incredibly…

Yes.

Re: How can C Programs be so Reliable?

#76

Earlier quoted context omitted.

"As the author alludes to as well - in C you're made more aware of the error conditions you can handle and the ones you can't." You mean like when a function silently returns -1 to indicate failure and then you wonder why your program returned a wrong result (if you're lucky enough to even notice)? In the bigger part of most of my programs I want a big, flashy, loud, total failure by default if anything goes wrong.

You mean like when a function silently returns -1 to indicate failure and then you wonder why your program returned a wrong result (if you're lucky enough to even notice)? This is the fault of the programmer who wrote the function, not of the language.

False. In languages with better type systems (e.g. ML and Haskell), you generally use the "Option" (OCaml) or "Maybe" (Haskell) type in situations like these.

(Example code is OCaml)

  type 'a checked = Pass of 'a | Fail
  
  (* very much a toy example *)
  let even x = (x mod 2 == 0)
  let can_fail x = match x with
    | 0 -> Fail
    | x when (even x) -> Pass (x * 10)
    | x -> Pass (x)
  
  # can_fail 2;;
  - : int checked = Pass 20
  # can_fail 3;;
  - : int checked = Pass 3
  # can_fail 0;;
  - : int checked = Fail
  # 
The languages infers where you're passing around option types (such as "int checked" above) and forces the calling function to account for any potential failures. Rather than just Pass 'a and Fail, it could be Pass, File_Not_Found, Access_Denied, etc., and (Unknown_Error string). OCaml also has exceptions, but the combination of union types and required exhaustive matching covers most cases.

Re: How can C Programs be so Reliable?

#77
post #5

People often write in higher level languages because they want lots of bad code fast. Almost all business applications are CRUD apps (create/retrieve/update/destroy) with some business logic, and they're generally written in C#. The app may crash when you click the wrong button, but the app is cheap to develop and the programmers are easily replaceable. Of course I'm generalizing, and a lot of C# programmers write gr…

People often write in higher level languages because they want lots of bad code fast. I assume you simply meant programmers who want to churn out something quick prefer high level languages. (Majority of them would never like to write 'bad' code intentionally) Your second assertion is spot on. If one needs to develop a high performing solution optimized for speed and memory consumption, plus high reliability and if i…

Just do what the gaming industry has been doing for awhile (in various ways).... core libraries / engine in C/C++ with higher-level logic in a higher-level scripting language.

This gives you the advantages of a high level language where the extra overhead and consequences for coding a little poorly aren't as detrimental.

Re: How can C Programs be so Reliable?

#78

Earlier quoted context omitted.

People often write in higher level languages because they want lots of bad code fast. I assume you simply meant programmers who want to churn out something quick prefer high level languages. (Majority of them would never like to write 'bad' code intentionally) Your second assertion is spot on. If one needs to develop a high performing solution optimized for speed and memory consumption, plus high reliability and if i…

Just do what the gaming industry has been doing for awhile (in various ways).... core libraries / engine in C/C++ with higher-level logic in a higher-level scripting language. This gives you the advantages of a high level language where the extra overhead and consequences for coding a little poorly aren't as detrimental.

Yes, we are doing something similar currently. A combination of C & Ruby is working out quite well. (The product is for the Ruby domain so the high level language choice was very straightforward.)

Re: How can C Programs be so Reliable?

#79
post #5

People often write in higher level languages because they want lots of bad code fast. Almost all business applications are CRUD apps (create/retrieve/update/destroy) with some business logic, and they're generally written in C#. The app may crash when you click the wrong button, but the app is cheap to develop and the programmers are easily replaceable. Of course I'm generalizing, and a lot of C# programmers write gr…

Yes, lots of bad code fast is half the reason I use Scheme almost exclusively now after 12 years of C. The other half is converting bad code to good code fast.

Re: How can C Programs be so Reliable?

#80
post #16
post #5

People often write in higher level languages because they want lots of bad code fast. Almost all business applications are CRUD apps (create/retrieve/update/destroy) with some business logic, and they're generally written in C#. The app may crash when you click the wrong button, but the app is cheap to develop and the programmers are easily replaceable. Of course I'm generalizing, and a lot of C# programmers write gr…

I come from the systems research world. I've seen people choose C not for the reasons you mention, but just because it's what they know best. This is not always the best thing to do. One instance is a colleague who needed to do data post-processing, and just did it in C because it was most familiar. A language like Perl or Python would have been a better choice, and saved him time in the long run, since string manipu…

Your colleague may be interested in pcre (perl compatible regular expressions) see http://www.pcre.org

But I agree, C isn't the best choice for that task.

Post reply on HN