Linked by MOS6510 on Thu 10th Jan 2013 23:25 UTC
General Development "For years I've tried my damnedest to get away from C. Too simple, too many details to manage, too old and crufty, too low level. I've had intense and torrid love affairs with Java, C++, and Erlang. I've built things I'm proud of with all of them, and yet each has broken my heart. They've made promises they couldn't keep, created cultures that focus on the wrong things, and made devastating tradeoffs that eventually make you suffer painfully. And I keep crawling back to C."
Thread beginning with comment 548567
To view parent comment, click here.
To read all comments associated with this story, please click here.
RE[6]: Point by point
by ingraham on Sun 13th Jan 2013 05:23 UTC in reply to "RE[5]: Point by point"
ingraham
Member since:
2006-05-20

In Java, your class extends Comparable, then provides a definition of comparesTo(). You can now use your class with anything that wants to sort or search. You could argue the difference is just semantics or syntactic sugar. Still, the concept and syntax of function pointers is considerably more difficult to understand, and results in code that isn't as checkable by the compiler and can cause really wacky things to happen if you mess it up. In C, if I accidentally pass my string compare function to qsort while I'm trying to sort an array of ints, the program will compile and run, and then screw up badly at runtime.

Reply Parent Score: 2

RE[7]: Point by point
by kwan_e on Sun 13th Jan 2013 06:27 in reply to "RE[6]: Point by point"
kwan_e Member since:
2007-02-18

In C, if I accidentally pass my string compare function to qsort while I'm trying to sort an array of ints, the program will compile and run, and then screw up badly at runtime.


That's not so much a problem with using function pointers as it is with weak typing. qsort has to be able to sort any types, so the arguments passed to the sorting function are void*, making the function signature similarly unhelpful.

However, if you write a thin wrapper on top of qsort that takes in a function pointer with more specific argument types, then the compiler will complain reliably.

Reply Parent Score: 3

RE[8]: Point by point
by ingraham on Sun 13th Jan 2013 19:35 in reply to "RE[7]: Point by point"
ingraham Member since:
2006-05-20

if you write a thin wrapper on top of qsort that takes in a function pointer with more specific argument types, then the compiler will complain reliably.


That sounds obvious now that you mention it, but in over 20 years of C programming that never occurred to me, nor have I ever seen anybody do it. Of course, it adds that much more complexity, but it still sounds like a damn good idea.

Reply Parent Score: 1

RE[7]: Point by point
by saso on Sun 13th Jan 2013 11:09 in reply to "RE[6]: Point by point"
saso Member since:
2007-04-18

I think you just consider it simpler because it's what you're used to. For instance, it would take me a good while to figure out what you said about comparesTo(), simply because I'm not used to it. Also, your example covers custom sorting of objects, but not of first class types - how do I do a custom sort on strings, or ints, or something else atomic? Sure, you could wrap the primitive types in custom object classes and incur a significant performance penalty (an array of 10000 int's is going to need 10000 malloc's and subsequent free's), in addition to adding tons of lines of code (plus a few new extra classes/files), whereas in C the problem could have been solved efficiently in about half a dozen lines of code with no extra allocation needed.

Reply Parent Score: 2

RE[8]: Point by point
by ingraham on Sun 13th Jan 2013 19:52 in reply to "RE[7]: Point by point"
ingraham Member since:
2006-05-20

I think you just consider it simpler because it's what you're used to.


Always a danger, but I think it's more likely the way my mind works than experience. I learned C and qsort a good decade before picking up Java and C++. In general, the implementations are quite similar. In C, you write a compare function, calling it whatever you want. In Java, you write a compare function and make sure you call it compareTo. Anyway, you have a point.

your example covers custom sorting of objects, but not of first class types - how do I do a custom sort on strings, or ints, or something else atomic?


I've never needed to do that, so I'll have to think about it. You may be right that wrapping the type in a new class may be the only way to do it. Of course, in Java ints would have to get wrapped in an object anyway, so that's a sunk cost. While it is a niche case, it's pretty interesting.

Reply Parent Score: 1