13 ms·
C Craft: C is the desert island language.
- gcv 16y agoI love the link to OTCC (the Obfuscated Tiny C Compiler, http://bellard.org/otcc/ http://bellard.org/otcc/). It frankly blows my mind.
- sliverstorm 16y agoI don't think you'd need infinite monkeys to come up with that. It closely resembles what my dog produces when laying on my keyboard.
- roel_v 16y agoI was quite disappointed though that most of the obfuscation comes from the use of the preprocessor. I can make any program in any language look hard if I can rewrite it with an m4 macro before running it.
- Groxx 16y ago>In my Eiffel days, I was encouraged to write "integer", not "int", "character", not "char", and so on. I believe Java encourages this practice too. Supposedly clarity is maximized. But how can this be if we do the opposite when we speak? Do you say “taxi cab”, or “taximeter cabriolet”? Redundant utterances are tiresome and worse still, obscure the idea being expressed. I see this argument a lot, and they strike me as people complaining because they don't use tools their language provides. Typedefs are the answer to excessive name length, and they're nearly everywhere. Just create a couple typedef files, and import them as needed - future programmers get the full names easily, while you can program in your pseudo-K version of C for maximum keyboard efficiency. I have a handful of such files, they're endlessly useful - why write `Pop_froM_lIsT_WhIch_COntaiNs_speCiFik_type_X`, doing battle with naming-scheme-X that only employee-Y uses (and their associated spelling errors) when you can do so once, and write `pop` from then on, unambiguously? The upside of typedefs for this comparison is that they're precisely what we do with spoken language - nobody knew what a "taxi cab" was until someone told them it the shorter version of "taximeter cabriolet", or until the full phrase was well enough known that it could be inferred accurately by the average person.
- chittis 16y agoGiven that Java doesn't have typedefs how would you reduce the verbosity of : ConcurrentHashMap<String, Integer> foo = new ConcurrentHashMap<String, Integer>();
- jarin 16y agoI would argue that you don't really need to, since auto-completing code editors and displays with enough resolution to support more than 80 columns are pretty ubiquitous.
- Groxx 16y agoThere you're largely screwed, but Java is well-known for being extremely verbose, and of course there will be languages that don't have basic features others have had for decades. The closest you can get in Java is to wrap it in a private, internal class. Which also lets you rename some verbose or frequently-combined operations, say: foo.someoneCouldntComeUpWithAShorterName() -> wrapped.succinct() -- or -- foo.store(val); foo.store(val); foo.store(val); // I hate void functions... -> wrapped.store(val,val,val); But yes, it's a pain. Java's largely a pain.
- jedsmith 16y agoI see your Java, and raise you Objective-C: NSString *thing = [NSString stringWithFormat:@"Result: %@", [foo someoneCouldntComeUpWithAShorterNameWithZipcode:08205 name:@"larry" parcels:5]]; NSDictionary *request = [NSDictionary dictionaryWithObjectsAndKeys:@"iphoneapp/0.1", @"User-Agent", @"news.ycombinator.com", @"Host", nil]; This isn't to kick off pet language wars, by the way, just to point out that my OCD tic to wrap to 80 characters has absolutely zero place in Xcode. Or Java.
- Groxx 16y agodisclaimer: I have not tried these. Totally pulling these from a quick googled-refresher of macros. #define withZip someoneCouldntComeUpWithAShorterNameWithZipcode #define dictWith dictionaryWithObjectsAndKeys or #define dictWith NSDictionary dictionaryWithObjectsAndKeys so you can [dictWith: key, value, key, value] :) I forget, does XCode intelligently handle macros? Or does using a macro destroy its helpful features, like code completion? I actually have a "hashWith" utility function for my .NET code. Saves me a ton of ugh-induced headaches.
- granite_scones 16y agoThe "Power" section made me think of Blub. http://www.paulgraham.com/avg.html http://www.paulgraham.com/avg.html
- angusgr 16y agoI'm deeply conflicted about this article because I agree with many of its premises. For instance: that C is superior to full-blown C++, that object-oriented programming is no panacea, that simplicity is good. I have serious concerns, however, especially with the first page and the first few chapters. - They support the biases of the myopic programmer who believes that now he or she knows C, they know everything one must know about programming. I know that isn't the entire point, but you can get that from the article and I have met such programmers. You don't want to work with them, even on projects written in C. - The "C Craft" section largely describes hacks to work around shortcomings in the syntax or semantics of C. - The languages used for "vs" comparison are: FORTRAN, C++, Java. Fish in a barrel, anyone? Haskell, APL & J are presented as curios. Python is only mentioned in passing, as an inferior choice to Haskell for rapid prototyping of mathematically-oriented code. - Go is presented as the "better C", which is encouraging but I'd feel more encouraged if the author showed they were properly familiar with some additional modern programming languages and the cases in which one might use them. - The assertion that "you can write object-oriented code in C" is accurate, although I think a better point to stress is "you can write mostly-well-modularised code in C, and that's what you want a lot of the time." - The author also ignores the reality that object-oriented C really only scales up to a certain amount of object-orientatedness, and then it becomes very unwieldly if you are not very careful. Unwieldy at a scale where using a small subset of C++ (ie "C with classes") would remove the overhead, improve the code's signal-to-noise ratio, and still not bring in most of the "bad C++" that the author is talking about. - The author seems to have chosen to define certain terms as they see fit. For instance, simplicity is defined in terms of brevity & terseness but the example used to prove the point is that Eiffel requires "character" and "integer" whereas C only requires "char" and "int". For an alternative point of view on what constitutes "brevity" and "simplicity", see the common C idioms for filtering or mapping any variable sized data structure. The only time it becomes less brief is if you rewrite it in C++ w/ STL or Boost. ;) - It's also telling that in Chapter 2 the Fibonacci counterpoint to Java is in Haskell, not C. That's because a full C program would look pretty similar to the Java program quoted, albeit without the sore-thumb of wrapping it all in a class . Anyhow, I should quit ranting but IMHO (a) you should know C, (b) you should respect C but (c) you should know some other languages and use C only when you actually need to. (c) may not apply if you're a super-whizz genius C programmer, some of those people seem like they can carry off ridiculous use cases without making horrible messes. Most of us are not those people. ;)
- enneff 16y agoIt's nice to see that the things he describes as C's greatest virtues are the same things we carried over into the design of Go. (http://golang.org/ http://golang.org/)
- naner 16y agoIt appears he has something to say about that, too: http://www-cs-students.stanford.edu/~blynn/c2go/ http://www-cs-students.stanford.edu/~blynn/c2go/
- nmnjjjjk 16y ago==== http://www.findsoso.com http://www.findsoso.com ===== Here, there's good news to tell you: The company recently launched a number of new fashion items! ! Fashionable and welcome everyone to come buy. If necessary, please plut: ====== http://www.findsoso.com========== http://www.findsoso.com========== We need your support and trust!
- onan_barbarian 16y agoThere are some reasonable things here, but: "Lumping together function pointers that operate on the same data structure is essentially object-oriented programming" is unbelievably contentious regardless of which of the various definitions of OOP you follow.
- jedsmith 16y agoI would strongly disagree that it's contentious. (Think that over...disagreement about contention. I raised my eyebrow too.) Some of the more exotic features of what people call "OOP", like polymorphism, take a bit more care and feeding to pull off in straight C. They are most certainly possible, that being said. Unwind "OOP" to "working with objects that contain data and methods together," and structs with function pointers fit the bill. I've had people stand in my face and practically shout that C++ is object-oriented and C isn't, and one refused to believe or admit that C++ basically compiles down to C in the end.
- tsuraan 16y agoIf you try to equate C and C++ by saying C++ can compile to C (not sure if that's what you're saying but it sort of looks like it), you might want to change your argument a bit. Haskell can also compile to C, but that doesn't make C strongly-typed, functional, or garbage collected.
- deleted 16y ago[deleted]
- onan_barbarian 16y agoAt a high enough level of 'care and feeding' it's possible to pull off these things in assembly, or Fortran, or whatever, rendering the term completely meaningless. C++ 'basically compiles down to C' for some constructs more than others (exceptions, RTTI, multiple inheritance goop) but if the C it compiles to is dark, arcane, unreadable crap, then this is no more illuminating than the truism that C++ basically compiles down to assembly language.
- 16y ago
- tptacek 16y agoYou can easily beat "static const volatile signed long long int bar" with a function pointer (and without the unrealistic redundant indirection of "int const ... const foo"). Start with static const volatile signed long long int (*foo)(static const volatile signed long long int bar). :)
- d_r 16y agoAnother excellent resource (a Git tutorial) by the same author: http://www-cs-students.stanford.edu/~blynn/gitmagic/ http://www-cs-students.stanford.edu/~blynn/gitmagic/
- Ruudjah 16y ago> Not only is C easy for humans to understand, Is he being funny or serious?
- billforsternz 16y agoSerious. C is a simple, elegant language. If your natural inclination is towards low level tools without elaborate abstractions built in, then yes it is easy to understand. Easier than the equivalent assembly language for example.
- chalst 16y agoAssembly language is generally easy, excepting only that it typically doesn't insulate issues of memory addressing, &c. The big advantage of C over assembler is portability.
- dasil003 16y agoJust the notion of a single desert island language is a bit humorous, but if I had to choose I'd pick something meatier like Haskell; and not out of lack of fondness for C.
- nadam 16y agoIt says: "In my Eiffel days, I was encouraged to write "integer", not "int", "character", not "char", and so on. I believe Java encourages this practice too." 1. No Java uses char, int, and so on. 2. No, C is absolutely not concise even compared to Java. At least how I write Java code. Look at this Java method: String strangeConcat(String str1, String str2) { return str1.substring(0, str1.length() - 1) + str2.substring(1, str2.length()); } Is it really more concise in C? How do you tell the caller to free the memory of the returned string? And if you compare C to Ruby, Python, Scala, Clojure etc... then it is no question that C is not a concise language for most tasks.
- ElliotH 16y agoIn Java int and char are primitive types. They don't provide methods so: int i = 4; i.toString(); //Exception - int isn't a class, so i isn't an object, so you can't call its methods. Integer j = 4; j.toString(); //Returns "4" as a string, because Integer is a class. Java would 'encourage' the latter practice at times because its useful to be able to call methods off of characters and integers and such, but there is a difference.
- msg 16y agoBut you wouldn't do this in Java (need a random string for an integer). Instead you might see: logger.fatal( String.format("Program crashed on index %d: %s", i, message) ); I think C has a similar construct, not quite as verbose, that would save 7 characters out of the line...
- ElliotH 16y agoThere are other methods on Integer and such - it was a contrived example because I was too lazy to read the docs.
- cafard 16y agoDesert island language, as in "like trying to build a hut, and manage your hunting and gathering with the Swiss Army knife that happened to be in your pocket"? I agree that for Torvalds work it is the only sane choice. But I'm not writing kernels.
- unwind 16y agoI didn't get the reference either. It fits my ego better to think of it as the Desert Eagle[1] language, I think. Or at least my subconscious seems to think so, I just misread the title of this story as such. [1] http://en.wikipedia.org/wiki/IMI_Desert_Eagle http://en.wikipedia.org/wiki/IMI_Desert_Eagle
- forgotAgain 16y agoIt's along the lines of "if you were stuck on a desert island and could only have one whatever, which would it be?" So if you had to choose one programming language to do all of your work in, which language would it be?
- cafard 16y agoActually, I understood. I used to listen to Desert Island Disks 15 or 20 years ago--a local NPR affiliate's version. One programming language? As I recall DI Disks let one pick 10 pieces of music, and threw in the Bible, all of Shakespeare, and a luxury of your choosing. But I suppose that if one were not distracted by King James, King Lear, or a luxury, the sensible thing would be to follow in steps of Wall, Ousterhout, Van Rossum, and Matz: use C and create an extensible scripting language. (edit: spelling)
- swah 16y agoI'm assuming the island has power and modern computers.
- discreteevent 16y ago"Other object-oriented programming preachers rebelled in practice, by spending most of their time in “fun” languages like Perl or Python. After all, who enjoys the boilerplate and verbosity of Java?" - What has the verbosity of java vs python have to do with the validity of object oriented programming? "New languages like Erlang and Go have become popular despite not being object-oriented. In fact, Joe Armstrong, inventor of Erlang, explains why OO sucks." - Actually later Joe Armstrong came back and said the following: "Actually it’s a kind of 180 degree turn because I wrote a blog article that said "Why object-oriented programming is silly" or "Why it sucks". I wrote that years ago and I sort of believed that for years. Then, my thesis supervisor, Seif Haridi, stopped me one day and he said "You’re wrong! Erlang is object oriented!" and I said "No, it’s not!" and he said "Yes, it is! It’s more object-oriented than any other programming language." And I thought "Why is he saying that?" He said "What’s object oriented?" Well, we have to have encapsulation, so Erlang has got strong isolation or it tries to have strong isolation, tries to isolate computations and to me that’s the most important thing. If we’re not isolated, I can write a crack program and your program can crash my program, so it doesn’t matter. You have to protect people from each other. You need strong isolation and you need polymorphism, you need polymorphic messages because otherwise you can’t program. Everybody’s got to have a "print myself" method or something like that. That makes programming easy. The rest, the classes and the methods, just how you organize your program, that’s abstract data type and things. In case that the big thing about object-oriented programming is the messaging, it’s not about the objects, it’s not about the classes and he said "Unfortunately people picked up on the minor things, which is the objects and classes and made a religion of it and they forgot all about the messaging."
- ryanisinallofus 16y agoGood thing we don't work on deserted islands right?