30 years of C
dadhacker.com
dadhacker.com
Couldn't agree more - every extra line makes the source code that much harder to wrap your brain around.
If I glance at a section in my code and can't instantly grasp what it is doing and how it fits into the bigger picture then I know I still have refactoring to do.
I can't even count the number of times I've heard "but this code will be useful if/when we add support for feature X" used to justify hundreds or even thousands of lines of ifdeffed-out cruft.
if (false) { ... large incomprehensible code block .. }
was common. Why comment out stuff when you can just disable it programmatically? > This avoids archeology on historical versions.
It's not really your main point, but the idea of revision control archeology is a really a great analogy.Version control is not so easy to search, particularly if you're looking for a utility that you think is live in the project.
I never knew this, interesting tidbit. Like many programmers I've read my share of Apple history books, but somehow Apple Pascal eluded me.
I still have a boxed copy of Symantec Think Pascal. I used it to make my first steps in window based/event driven applications programming on the Mac (in 1990).
I remember coding my first apps invoking the system calls directly to create windows, handling events (see where the user clicked) etc. Then I moved to Codeworrior which had a nice c++ framework.
An assignment for an exam was to make a simple library management program in pascal (cataloging books, leading, receiving them back etc.), mainly to test our understanding of pointers.
Even if we also had Macs at the university, we've been asked to make a command line app. But, since I was studying the Mac OS gui programming by myself, I made a graphical application instead.
The assistant professor that was evaluatings our assignment didn't believe that I had made it myself. She probably couldn't understand it herself (but, obviously, all the relevant functions were separated from the gui code). So she asked me to rewrite everything from scratch as a command line application in a couple of hours in front of her.
I managed to do it but I was really really pissed off. Shortly thereafter I started working and dropped out.
In a similar situation, I wrote the required command-line app, but it handled queries, basically parsing to an expression tree predicate.
Of course, it doesn't matter now.
The assignment was about implementing the functionalities of a simple library management app. And it was explicitly to test using pointers, linked lists ecc.
The requirements were fully implemented. And all the queries were handled by functions which were completely separated from the gui code.
Basically, instead of a textual menu (1 - borrow, 2 - return etc.) you had a Mac Os menu and the results were presented inside fields in a window instead of a textual output.
If I had to convert the program to a textual interface by editing my source (instead of doing everything from scratch) it would have took me 5 minutes (or less). IIRC the code was already there and simply commented out.
Keep also in mind that our lab was full of Macs (50-60 machines) with just a PC (which, btw, has been used by a guy who had made the app with his own advanced textual interface - something like ncurses - and who had a similar fate as mine). So I thought it was a plus to use the native gui of the OS we were using all the time.
> Of course, it doesn't matter now.
Of course. It happened 17 years ago :-)
This gets to interfaces though. I don't know about the gp, but if the assignment specifies a particular interface then that becomes a part of the spec. It doesn't matter that you made a more advanced interface, if it doesn't match the required interface it's wrong.
can’t help but think that Sun continued to blow its
opportunities here, for years.
Usually I jump on the bash-java bandwagon. However, I think the author's wrong about this. Java is good in a team setting, and Java is the best choice for the enterprise.I support a cross-platform, plugin-oriented Java application with a large install base and wouldn't want it written in anything else.
In C applications, it happens that applications will have segfaults, and threaded log less helpfully on the way down whereas in java there's a command that allows you to analyse threads of a running process. When there's a Java problem I have much better odds of getting my head around the source than I would for the sort of macro-affected C source that you'd need for an application run in this setting. If it were to run out of memory it would be clear about it. When you're stuck with libraries where the source and developers are long gone it's realistic to use a decompiler. With some preparation at dev time you can use the classpath and classloaders to hot-patch.
C is good for programmers, but not so good for the other technical workers. And I don't think C is as much of a big deal as it used to be due to the advances in scripting languages. I do a fair bit of programming oriented around unix system calls, all in python. Sometimes I find the documentation is better in C, and mock it up there, and then carry it back to python where I can do more with it. Also, if you're on Bigco's standard-issue Solaris host you're far more likely to find perl than a C compiler. Computers are faster than they used to be. Raw execution speed and memory footprint of a process (areas where C is stronger) are much less relevant to scale issues than they used to be.
Reply said:
The author is comparing Java to C#, not C.
Er - ah - so he did. Thanks for being polite about it.For Java-vs-C#, I hold my ground :-)
I tend to hack up Perl-like tools in C# these days, using a small regexp-centric framework that also handles command line parsing. It's not much more code, and I feel better about it in my current community.
Other than that, some food for thought on use of C in the real world is this article by Rob Pike, "Notes on Programming in C:"
There are several threads on HN recommending source to read. (Off the top of my head, I'd recommend the source for Lua and OpenBSD's userland utilities.)
- lmathlib.c, lstrlib.c: get familiar with the external C API. Don't bother with the pattern matcher though. Just the easy functions.
- lapi.c: Check how the API is implemented internally. Only skim this to get a feeling for the code. Cross-reference to lua.h and luaconf.h as needed.
- lobject.h: tagged values and object representation. skim through this first. you'll want to keep a window with this file open all the time.
- lstate.h: state objects. ditto.
- lopcodes.h: bytecode instruction format and opcode definitions. easy.
- lvm.c: scroll down to luaV_execute, the main interpreter loop. see how all of the instructions are implemented. skip the details for now. reread later.
- ldo.c: calls, stacks, exceptions, coroutines. tough read.
- lstring.c: string interning. cute, huh?
- ltable.c: hash tables and arrays. tricky code.
- ltm.c: metamethod handling, reread all of lvm.c now. You may want to reread lapi.c now.
- ldebug.c: surprise waiting for you. abstract interpretation is used to find object names for tracebacks. does bytecode verification, too.
- lparser.c, lcode.c: recursive descent parser, targetting a register-based VM. start from chunk() and work your way through. read the expression parser and the code generator parts last.
- lgc.c: incremental garbage collector. take your time.
Anyone actually follow this method want to chime in?
Anything where there's just one group using it lets too much stuff get through.
look at openssh, but the one in openbsd's tree and not the portable branch. for that matter, any of the openbsd-developed daemons (openbgpd, openntpd, etc.) at http://www.openbsd.org/cgi-bin/cvsweb/src/usr.sbin/ or usr.bin/ssh for openssh.
any of the openwall projects are also small and easy to work with - http://www.openwall.com/
For example qmail is probably the most secure smtp server around. But the code is not easy to read.
Pretty much all the stuff Dan Bernstein writes is secure and correct. But his coding style is just too weird for me and many others.
Still a good learning experience, but yeah, it would be preferable to choose something well known for quality code. I learnt a lot from programming an apache2 module. Supposedly the apache code isn't the best C project available, but there are some interesting real-world aspects of it.
Gnu Ed's source code is quite easy to read. Plan 9's utilities also have very clear, concise source code.
http://www.cs.brown.edu/courses/cs167/lect.old.08.shtml http://www.cs.brown.edu/courses/cs167/asgn.shtml