Live data from Hacker News

On becoming an expert C programmer

isthe.com

61–70 of 80 posts

Re: On becoming an expert C programmer

#61
post #41

Earlier quoted context omitted.

I've been programming (dabbling, to be honest) in LISP since about 1980, I've never shipped any products in it. I wrote a couple of toy interpreters in college (after reading Allan's Anatomy of LISP ) and some small projects, but nothing massive. I read SICP when it first came out and did most of the exercises. Yet Scheme was a terrible language to ship software in (commercial implementations were basically toys). I…

Aight, to each his own, a lot of Haskell mystery vanished when I saw it explained in terms of simpler languages (pattern matching, lazy evaluation, curryfication etc etc) but apparently that's not what's bothering you.

Hmmm, your answer was still helpful. Please don't get me wrong.

I guess I'm just used to functional style where data === code to a great degree.

I'll take another look at Haskell when I finish my current (yuck) Java based project :-)

Re: On becoming an expert C programmer

#62

Earlier quoted context omitted.

Those don't seem like "these days" things. In fact, some is much more "old days". Glib is just another library like any other, and POSIX is just a library plus specification of the details any platform would have to specify. And if you're not on something that tries hard to look like a *nix, POSIX mastery isn't going to help you. Autotools is a plague slowly being eradicated, the problems it ostensibly solved have be…

I wouldn't regard POSIX as "just another library". It is the interface between your program and the operating system. In order to gain a healthy understanding of things like memory mapping, signals and process control, you have to go beyond the corresponding man pages.

That has nothing to do with C. You would need to do the same thing in any language on any platform.

Re: On becoming an expert C programmer

#63
post #23

The article advises learning to play music because a lot of good programmers play music. A lot of programmers are also white. Please become white if you can.

Not a valid comparison at all. One is a learned skill, the other is an innate phenotype.

That's true, but the point is that this correlation could be pure coincidence, or at least not causative. (If it actually occurs to start with - it would be interesting to create a poll and verify whether most reputed programmers are indeed more musical than general population.)

Many programmers have stereotypical geek interests like RPG, Star Wars etc. but it doesn't mean that these contribute to their programming skills in any way. Being socially awkward doesn't make you Sheldon Cooper :)

Re: On becoming an expert C programmer

#64
post #54

Look at some of DBell's code: http://www.tip.net.au/~dbell/ http://www.isthe.com/chongo/tech/comp/calc/index.html Learn from it. DBell is one of the best programmers on the planet. I have nothing against Mr DBell, whom I don't know, but... don't learn from it, please. Weather 1.9 - Java application to plot weather observations from the Australian Government's Bureau of Meteorology (BOM) web site. I did look at it. Co…

>So, what's the point of closing the stream twice?

The first stream.close() does not get executed if an IOException occurs.

Re: On becoming an expert C programmer

#65
post #64
post #54

Look at some of DBell's code: http://www.tip.net.au/~dbell/ http://www.isthe.com/chongo/tech/comp/calc/index.html Learn from it. DBell is one of the best programmers on the planet. I have nothing against Mr DBell, whom I don't know, but... don't learn from it, please. Weather 1.9 - Java application to plot weather observations from the Australian Government's Bureau of Meteorology (BOM) web site. I did look at it. Co…

>So, what's the point of closing the stream twice? The first stream.close() does not get executed if an IOException occurs.

Of course, but the second one DOES execute whether the exception occurs or not. So the first call is redundant.

If no exception occurs (which is most of the time, probably), the code will call stream.close(); stream = null; and after that it will pass that null stream to Utils.safeClose, which can't do anything to it at this point, as the stream is closed by now and we've dropped the reference to it anyway (by setting it to null in meantime) :)

The code reads as if its author was under false impression that finally only executes if catch executes. But that's not true, and it would make no logical sense (why would the language distinguish between "catch" and "finally" at all in such case?)

Re: On becoming an expert C programmer

#66
post #12

There's nothing magic about learning C versus any other language. - Read an authoritative source (K&R is good; there are better ones) - Read a bunch of good code (I mostly read tools and kernel sources) - Write crappy code and get better Generally I want to write 10K lines of code in a new language before I probably don't suck at it. Varies on the language and paradigm, going to C++ from C took like five years (figur…

10K lines is very fast to make it to proficiency. It takes me a lot longer to 'not suck' at a new language, I'm not at all sure what a reasonable value here is but I don't usually consider myself slow. Probably I'll have to adjust that self image now ;).

Re: On becoming an expert C programmer

#67
post #12

There's nothing magic about learning C versus any other language. - Read an authoritative source (K&R is good; there are better ones) - Read a bunch of good code (I mostly read tools and kernel sources) - Write crappy code and get better Generally I want to write 10K lines of code in a new language before I probably don't suck at it. Varies on the language and paradigm, going to C++ from C took like five years (figur…

I really think C is much easier to get good at than other languages. There is no mystery with what's going on in the computer when you learn C, the builtin functions, how big something is, how things work, etc. When you learn higher level languages there is significant mystery in everything. It means you can write a lot of code having no idea what's going on under the hood, which is good. That's the point of a higher…

> It means you can write a lot of code having no idea what's going on under the hood, which is good.

I'm not so sure that's good. The more stuff there is under the hood that you have no idea about the more places bugs can hide in.

Re: On becoming an expert C programmer

#68
post #64
post #54

Look at some of DBell's code: http://www.tip.net.au/~dbell/ http://www.isthe.com/chongo/tech/comp/calc/index.html Learn from it. DBell is one of the best programmers on the planet. I have nothing against Mr DBell, whom I don't know, but... don't learn from it, please. Weather 1.9 - Java application to plot weather observations from the Australian Government's Bureau of Meteorology (BOM) web site. I did look at it. Co…

>So, what's the point of closing the stream twice? The first stream.close() does not get executed if an IOException occurs.

The whole point of a finally block is to have the cleanup code in it that should be called regardless of whether or not an exception occurred in the first place.

So the first stream.close should not be there if the 'finally' block already contains one, and it will be executed twice if an exception does not occur.

Re: On becoming an expert C programmer

#69

Earlier quoted context omitted.

> I can't understand what undefined behaviors C has, because it is the most simple and defined language I do know of. C has lots of undefined behaviors because the language was designed to be easy for compiler writers to implement. As such, a lot of decisions were left to the compiler writers, which is what "undefined" means. Here is a (probably partial) list of undefined behaviors in C. (For a full list you'd have t…

Sure, there is a lot of code you could write that results in undefined behaviour. However, none of it is code that you should write, nor is it code that an expert would write.

Undefined behavior is defined as 'it does what it apparently does without guarantees to future behavior'. So that code that you should write and that an 'expert' would write that implements something that depends in a non-obvious way on undefined behavior (and it is surprisingly easy to do that) will possibly break in hard to detect ways (if it is detected at all, which is more dangerous) at some point in the future when said undefined behavior is changed.

The only way to work around that is not just by becoming an expert at the language but also by becoming an expert at the implementation details of the language and that's a domain that not many programmers are comfortable in. That way you can make sure you stay away from the undefined behaviors as much as possible.

Bad car analogy: not only do you have to learn how to drive the car, you also need to know that the combination of wind across the left front+rain+loud music on the stereo will sometimes cause a wheel to fall off. This is not usually considered a useful level of knowledge and so C programmers (experts too) all find that their knowledge of the limits of C has a partial overlap with the real limits of C as implemented by their particular toolchain. And in between the cracks lots of nasty stuff can happen.

Dead code elimination, different sequences of optimization phases (changing from one compiler to the next) can introduce very subtle bugs in your code and I highly doubt 'most experts' would know how to distinguish between some very innocent code that will almost certainly bork on some standards compliant compiler with optimization 'on' or 'max' versus similar looking code that will work just fine.

C is a beautiful but somewhat tricky mistress.

Re: On becoming an expert C programmer

#70

Earlier quoted context omitted.

I wouldn't regard POSIX as "just another library". It is the interface between your program and the operating system. In order to gain a healthy understanding of things like memory mapping, signals and process control, you have to go beyond the corresponding man pages.

That has nothing to do with C. You would need to do the same thing in any language on any platform.

Most higher level languages do have wrappers around these calls, but you rarely have to worry about things such as async-signal-safety or EINTR, for example. Knowing why such things are problematic isn't of much interest to the average Python programmer, while essential for writing correct and safe C code.
Post reply on HN