Though I have to say, I'm not sure I understand how "C-strings are hard to work with" is an indictment of the "functions and structs are enough; object orientation is unnecessary mind-control" school of thought - because whether you implement reverse_string as a function or a method, the details of working with c-strings are the same. The implementation details of the method version of reverse_cstring in a hypothetical c-with-objects scenario have exactly the same sharp edges as the function version. (In C++ the terminology "non-member function" refers to a "function" and "member function" refers to what in most other languages would be called a "method". For this post I use "function" and "method" even though I will give C++ examples).
OO, as it is implemented in C++, C#, Java, Objective-C, Python, Ruby, and other class-based OO paradigms, is fundamentally a namespacing principle. I don't think anyone really knows if "OO" exists as a clearly defined mathematical abstraction separate from some of the details of the particular system. So for the rest of this little post I'm going to refer to the basics of class-based inheritance object orientation. It seems to be what you're complaining about, and it seems to be what most people mean when they refer to OO. So, in that kind object-oriented code scheme, OO is really about namespacing and information hiding. Namely, methods are namespaced by the data on which they are designed to operate, and the data on which they are designed to operate is hidden from methods which aren't designed for that data. From this single idea two advantages fall out: cleaner names for things, and code reuse. For example, consider two data structures: a c-string and a struct that represents a sequence of Unicode codepoints.
There are many useful methods which take the same logical action on both data structures. In "functions and structs" world, you have to name those functions differently to avoid namespace conflicts. Let me just make up an API for these structs:
Code:
capitalize_string(*char)
copy_string(*char)
reverse_string(*char)
strip_whitespace(*char)
capitalize_unicode_string(*unicode_str_ptr)
copy_unicode_string(*unicode_ptr)
reverse_unicode_string(*unicode_ptr)
strip_unicode_whitespace(*unicode_ptr)
1. Object orientation facilitates compiler-enforced namespacing and type-checking, reducing
cognitive load.
In the hypothetical API above, these functions are namespaced by me, according to a convention I just made up. There's namespacing, but it's ad-hoc. Like a good engineer, I have
imposed some order. This ad-hoc namespacing is inferior during the writing and reading phases of the code's lifecycle.
At write-time, I can only hope that my fellow programmers will follow this convention. In a real piece of software, I would have to spend time planning out our company's namespacing policy to make sure it handles our use cases, and then document that policy, and then insist that everyone who works on this code learns and conforms to the policy. Even if you already know C, you'll still have to learn our company's namespacing policy as soon as you get here, steepening the learning curve. Moreover, every time a function is added or searched-for by a developer who's writing code, there's additional overhead involved for that developer. Since the namespacing policy is not compiler-enforced, he has to think about how to name his functions.
Best of all, having a single, general-purpose namespacing convention built into the language naturally extends the type system. All of those class names are also
types for the compiler. This allows the compiler to catch many classes of type errors automatically. A non-trivial side-benefit of having agreed upon your namespacing policy with the compiler is that intellisense/code-completion can be provided out of the box to assist the developer in resolving semi-qualified names.
There's a read-time overhead as well. After the code is written, the yield is uglier. Too much context is made explicit in a function littered with reverse_unicode_string. In the context of the code where those function calls appear, one of two things will be true: either I will already know that the code is operating on a unicode string, or I won't know and won't care whether the code is operating on a unicode string or a c-string. So seeing that long reverse_unicode_string(*my_string) is at best giving me redundant information leading to line noise, and at worst it's over-sharing and messing up my separation of concerns. This leads me to the second benefit of formally structuring your namespace.
2: Formally structured namespacing
facilitates simultaneous code re-use and optimizations.
That's right, I'm going to show you ways in which object-oriented code is either faster or less verbose than equivalent "functions and structs" code.
Circle back to the original proposition: OO is a namespacing principle. Think of function names as URIs for the code they symbolize. Now, notice that when you switch to object orientation, you're switching from a single token with ad-hoc namespacing to a sequence of tokens. Now, instead of just the developers who read your namespacing policy, all developers in your language and the compiler understand your namespacing policy, and the compiler can make use of that policy by understanding what you mean when you vary some parts of that sequence but not others.
Let's take our unicode/c-string example again.
Suppose that, in our API, we want to expose a "reversed_copy" function which copies a string and then reverses it. The implementation for our two data types looks like:
Code:
char* reversed_copy_string(char* str) {
char* copy = copy_string(str)
return reverse_string(copy);
}
unicode* reversed_copy_unicode_string(unicode* str) {
unicode* copy = copy_unicode_string(str);
return reverse_unicode_string(copy);
}
The implementations are the same except they dispatch to different copy and reverse functions. It's no faster than the equivalent C++ object-oriented code, but there are twice as many functions and the symbols in each function are twice as long as in the C++ version.
Now, you might argue that the genericity of this function can be captured in C. It's true. You actually can capture this genericity in C by passing in function pointers to the specific versions of functions. You'll have to drop the modicum of type-safety that C affords and work on void pointers, but you technically can do it. It's something like this:
Code:
void* generic_reversed_copy(void* data, void* (*copy_func)(void*), void* (*reverse_func)(void*)) {
void* copy = (*copy_func)(data);
return (*reverse_func)(copy);
}
I'm not going to dwell on how nasty this code is to read and how nasty it is to pass around void pointers like this. Apparently the OP is an ubermensch who doesn't care about paltry things such as code readability and compile-time type checking. Those things are actually important for real people who write real software, but not to the ubermensch. Instead I am going to talk about a less obvious issue arising from our reliance on function pointers. This code is
slower than the C++ equivalent compile-time dispatched generic code. The reason for this is that the compiler doesn't, and can't, know which bits of code those function pointers resolve to. For this reason it is unable to perform one of the most important optimizations for modern hardware: function in-lining. Instead of a nearly-guaranteed cache-hit for the inlined version of the code (as you'd get in the C++ or the verbose C versions), you have a guaranteed cache miss to follow those function pointers back to main memory.
In contrast, here's the OO version:
Code:
class AbstractStringBaseClass {
AbstractStringBaseClass reversed_copy() {
return this.copy().reverse();
}
}
Not only is this shorter and type-safe, but, because you and the compiler have agreed on what code "this.copy" and "this.copy().reverse" resolve to, the compiler can inline those functions. Namely, this.copy resolves at compile time to (say) AbstractStringBaseClass.Unicode.copy or AbstractStringBaseClass.CString.copy, and this.copy().reverse resolves to AbstractStringBaseClass.Unicode.reverse or AbstractStringBaseClass.CString.reverse. You are varying the second part of the URI - the class name - but don't have to vary the last part of the URI, so you don't have to write it twice. This is only possible because of the structure you've induced on your namespace by agreeing to be "constricted" to the OO specification of your language.
Strange but true, my friend. This isn't exactly the same, but a similar principle is at work in this comparison of C++ vs C sorting. The C++ templating system which makes the standard library algorithms so fast is made possible by of C++'s type system, which as I've shown is a result of the OO principle that you're heedlessly maligning.
As a final thought, I want to point out that I've been careful not to conflate the advantages of formalizing your namespacing rules with OO. OO is one way of formalizing these rules. There are other formalizations, collectively called "type systems", and some are more general than OO. Haskell's typeclass system, for instance. But OO is still very useful.