4 ms·
Seriously? This is more like, "I hate Java and teams with managers that like making diagrams instead of writing code". Most procedural code is actually object
by jrockway 16y ago
Seriously? This is more like, "I hate Java and teams with managers that like making diagrams instead of writing code".
Most procedural code is actually object-oriented. If you write:
foo_t foo;
init_foo(&foo);
frobnicate_foo(&foo, "with a bar");
cleanup_foo(&foo);
Guess what, that's object-oriented programming. You are ensapsulating state and behavior in a data structure and set of procedures. Calling that a "class" is just syntax sugar that a lot of people find useful.
Even the most die-hard anti-OO advocates write code like this. Why? Because it's a great way to manage complexity, and that's all programming is. Doing stuff, but also doing it in a way that is easy to change and easy to improve.
I'm not even going to comment on the rest of the article. It's clear that the author has never used an OO language other than C++ or Java, or a statically-typed language other than ... C++ or Java. Shockingly, there is a lot more to the world than C++ and Java.
- encoderer 16y agoI'm not directly disagreeing, just, prodding in the direction of disagreeement... But.. In that example, where is the "encapsulation" you mentioned? The way I'm reading it, the only thing connecting the data with the behaviors is the fact that you just happen to call the functions and pass-in the data. IOW, the is-somethings and does-somethings are separate.
- shub 16y agoAll that ever happens is that you "call the functions and pass-in the data", except that OOP languages know to pass this for you. The difference between OOP-style C and an actual OOP language in this regard is syntactic, not conceptual.
- encoderer 16y agoThis remark interests me: "except that OOP languages know to pass this for you". In practice, i'm not seeing this. In that procedural example, you can pass in any input you'd like. Suppose the function in that example accepts an array. Now, inside the function, I have to devote a non-trivial amount of effort to determine if the array contains the type of data I'm expecting. And if I want that function to change the array for possible use later, I'll need it to return the array and I'll have to store that return. Now, that array, implemented as an Object, is structured. Strongly typed. Even private properties with setters to enforce consistency. And that procedure, implemented as a method on that object, needs none of the consistency and validation code at the top of the procedure. And it can make changes to the properties themselves without the need to pass around variables. Sure, most of the benefit of OO is in the structure and organization it brings. But no, it's far more than, as you're making it sound, the compiler is passing around an implicit argument on your behalf.
- rbranson 16y agoI think what you're talking about for type-safety, not necessarily OOP. C supports types, but is not completely type-safe. It will tell the developer that they shouldn't be doing something, but won't enforce it. The argument above is that most developers write object-oriented code in procedural languages. They have a set of functions that perform actions on a specific type of data structure. To encapsulate functionality and keep the codebase clean, all reads and writes to these data structures should go through these functions. This pattern is ubiquitous in well-written C code. OOP itself simply provides a standard framework and syntactic sugar to make this type of code more consistent and clear. EDIT: Here's a real-world example: (came up for the first search of hash.c) http://www.ks.uiuc.edu/Research/vmd/doxygen/hash_8c-source.html http://www.ks.uiuc.edu/Research/vmd/doxygen/hash_8c-source.h...
- encoderer 16y agoNo, not entirely. A strongly typed language will help this a little -- at least I can know i have, say, an array of strings. Suppose i have 2 properties in my class, 'start_date' and 'end_date'. They are private and have setters. In a method that uses those 2 properties, I can trust that they are of the right type, that start_date is < end_date, that they represent a reasonable range based on my logic defined in the setters. All of this can be taken for granted. In this procedural example, imagine some sort of hash or struct or associative array (whatever you want to call it in your given language) is supplied with those 2 keys. The procedure cannot trust that the inputs are valid. It has to check everything. If you can rely on type safety, congrats, that's one less check. But there are plenty of others.
- rbranson 16y agoSo you create a typedef struct of date_range to store the start_date and end_date and use a set of date_range_* functions to manipulate that struct. Most OOP languages allow direct access to even private variables if the developer really wants to, so the guarantees are pretty weak. C++, Java, C#, Ruby, Python... all of those allow indirect access to "private" methods and variables. You must still rely on other developers to do the right thing and not screw things up.
- br1 16y agoYour code is object-based not object-oriented because of the lack of inheritance.
- Xurinos 16y agoHis example is a little small to see how he would implement inheritance. Also, inheritance is not a core requirement of OOP. Before you hit that downvote button, consider this: Inheritance allows your class to share attributes and behaviors with another class, and it allows code to recognize that instances of your class have those attributes and behaviors. In other words, when we say "is a", we really mean "does things that match the interface and meaning of". You can accomplish the same thing with "aspects" instead of "inheritance". Following that idea, it is simply a matter of introducing this simple API to add your behavior to an instance: add_aspect(&foo, 'Some Aspect Identifier'); And that's just the tip of the iceberg. Check out Art of the Metaobject Protocol (http://www.amazon.com/Art-Metaobject-Protocol-Gregor-Kiczales/dp/0262610744?tag=dudugo-20 http://www.amazon.com/Art-Metaobject-Protocol-Gregor-Kiczale...) for some huge eye-openers.
- metageek 16y ago>Also, inheritance is not a core requirement of OOP. The definition I learned was that, if you don't have inheritance, it's object-based. Of course, that was almost 20 years ago; usage might have shifted. I think the example I was given of an object-based language was Ada.
- TimJYoung 16y agoI agree. Most OO implementations are simply the foo instance data combined with a pointer to a table of foo function/method pointers that keep the foo functions in a nice tidy location, Throw in some scoping rules enforced by the compiler as to who/what can access the foo instance data, and that's really all there is to it. The type-safety is basically the same as with procedural code - you can't pass a reference/pointer to foo to a function/method that expects a bar reference/pointer.
- swift 16y agoI agree with your complaints about the article, but the code example you give could be demonstrating programming with abstract data types in C just as well as it could be demonstrating programming with objects. In fact, unless frobnicate_foo is just a convenience function to look up a frobnicate function pointer in the foo_t structure and invoke it, I'd have to say that you're clearly demonstrating an ADT here.