Live data from Hacker News

Object-Oriented Programming in C (2019) [pdf]

state-machine.com

41–50 of 95 posts

Re: Object-Oriented Programming in C (2019) [pdf]

#41
post #27
post #7

1. Creating a struct with desired state data 2. Creating functions that take a pointer to the struct as the first parameter 3. Declaring a variable with that struct type to create an instance of the object 4. Declaring another variable with that struct type for another object instance

... is not, in fact, OO at all, by any meaningful definition. 1..4 is just programming. People have done it since long before there was any "OO" buzzword. There are formal definitions of OO. The above satisfy exactly none of them.

The above pattern using opaque pointers resembles objects in C++. A construct() and destruct() function returning opaque pointers (handles for the struct really) can act as custom constructors and destructors. The opaque pointer ensures a simplified API, protects internal struct data, and the defining code of struct can change without impacting the remaining source code.

This is in fact, by any meaningful definition of OOP, is a very reasonable pattern. With regards to formal definitions of OO, message passing aspect ought to be recognised rather than overemphasizing the object aspect.

Re: Object-Oriented Programming in C (2019) [pdf]

#42
post #37
post #23

Earlier quoted context omitted.

Not an array, a struct is ok, and in C99 style it is very well readable. For example Linux Kernel style is: static const struct file_operations fops = { .open = my_open, .release = my_release, .read = my_read, .write = my_write }; that's all, very straightforward. Also there is no need to prefix the function name with &

This pattern goes back to pre-Version-7 UNIX kernels, with only minor syntactic updates. That it long predates OO demonstrates it is not OO. That does not make it any less effective, or less useful.

It does not predate OO.

The first OO language, SIMULA-67 became public knowledge before the start of even the PDP-7 UNIX, which is extremely unlikely to have used such a pattern, which probably appeared only when UNIX was rewritten in C, starting in 1973.

Even Smalltalk-72 predated the C-version of UNIX.

However, it is likely that this pattern was chosen in UNIX independently of the previous OO languages.

Re: Object-Oriented Programming in C (2019) [pdf]

#43
post #25
post #21

Back in the '90s, "Object-Oriented" got redefined in popular imagination to "good". If your language or system was good , it was then by definition object-oriented . Anything good had therefore to be called OO. Saying something was not actually OO (e.g. this) was taken to mean it was not good , generating spurious conflict. The fiction has largely dissipated, except among pundits and the aggressively ignorant. Meanwh…

> Back in the '90s, "Object-Oriented" got redefined in popular imagination to "good". If your language or system was good, it was then by definition object-oriented, and anything good had therefore to be called OO. Saying something was not actually OO (as we find here) was taken to mean it was not good, generating spurious conflict. A similar thing happened with functional programming.

thank you, it doesnt matter what brand of hammer you use aslobg as the quality is sufficient.

Re: Object-Oriented Programming in C (2019) [pdf]

#44

If you need to do OOP in C, it's time to move to a more powerful language.

There is one advantage to doing OOP in C. Once you figure it out, the mystery of how OOP works all falls away. Frankly, I didn't understand how OOP worked before doing that. None of the tutorials explained what was going on under the hood. As an engineer, I'm never comfortable using something when I don't know how it works.

While it's hardly a complete object system, perl's bless() operator associating a package name with a data structure, where the package is (for bless()'s purposes at least) basically just a global hash mapping method names to subroutines, was pretty much how I first got my brain around the concept.

I wish more "introduction to OO" things would start by demonstrating a dispatch table and then showing how the vtable concept maps onto that, I suspect it would make things significantly clearer to a bunch of people as they learn.

Re: Object-Oriented Programming in C (2019) [pdf]

#45
post #38
post #21

Back in the '90s, "Object-Oriented" got redefined in popular imagination to "good". If your language or system was good , it was then by definition object-oriented . Anything good had therefore to be called OO. Saying something was not actually OO (e.g. this) was taken to mean it was not good , generating spurious conflict. The fiction has largely dissipated, except among pundits and the aggressively ignorant. Meanwh…

https://microsoft.github.io/microsoft-ui-xaml > WinUI is powered by a highly optimized C++ core that delivers blistering performance, long battery life, and responsive interactivity that professional developers demand. Its lower system utilization allows it to run on a wider range of hardware, ensuring your sophisticated workloads run with ease. Apparently Microsoft does care about OOP in C++. As does Apple, https://…

OOP is great when you write APIs/libraries (your examples), but is useless or even counter productive when writing applications, where encapsulation or inheritance just obfuscate everything.

My main grip with Java is that everything is by default a library.

But there is hope, recent changes in Java, records and pattern matching move away from that idea.

Re: Object-Oriented Programming in C (2019) [pdf]

#46
post #37
post #23

Earlier quoted context omitted.

Not an array, a struct is ok, and in C99 style it is very well readable. For example Linux Kernel style is: static const struct file_operations fops = { .open = my_open, .release = my_release, .read = my_read, .write = my_write }; that's all, very straightforward. Also there is no need to prefix the function name with &

This pattern goes back to pre-Version-7 UNIX kernels, with only minor syntactic updates. That it long predates OO demonstrates it is not OO. That does not make it any less effective, or less useful.

To a great extent, the difference between "a data structure plus a dispatch table" and "an object" is primarily in how you squint.

It's certainly a form of polymorphism.

Re: Object-Oriented Programming in C (2019) [pdf]

#47
post #46
post #37

Earlier quoted context omitted.

This pattern goes back to pre-Version-7 UNIX kernels, with only minor syntactic updates. That it long predates OO demonstrates it is not OO. That does not make it any less effective, or less useful.

To a great extent, the difference between "a data structure plus a dispatch table" and "an object" is primarily in how you squint. It's certainly a form of polymorphism.

It's a measurable difference. OO in C++ must use double indirect vtable method calls, whilst this OO in C uses only one indirection. It's measurably faster. Objects are a bit larger though, cloning is a bit more expensive, but method calls are much faster and much easier to cache.

Re: Object-Oriented Programming in C (2019) [pdf]

#48
post #31

OOP in C inevitably leads to LOTS of pointer dereferencing. Pointer dereferencing hurts performance.

OOP in C++ leads to twice as much pointer dereferencing. Just that you don't see it doesn't mean it does not exist. Look at the generated code. It sucks.

The best thing in C++ is compile-time computation (ignoring the horrible template syntax). OO is not esp. well implemented.

Re: Object-Oriented Programming in C (2019) [pdf]

#49
post #47
post #46

Earlier quoted context omitted.

To a great extent, the difference between "a data structure plus a dispatch table" and "an object" is primarily in how you squint. It's certainly a form of polymorphism.

It's a measurable difference. OO in C++ must use double indirect vtable method calls, whilst this OO in C uses only one indirection. It's measurably faster. Objects are a bit larger though, cloning is a bit more expensive, but method calls are much faster and much easier to cache.

I think you're misinterpreting GP's code. It is a vtable, hence the static const. There would be a pointer in each struct "instance" to this static vtable, thus the same double indirection would occcur as in C++.

Re: Object-Oriented Programming in C (2019) [pdf]

#50
I think, everybody forgets one thing with C OO systems - no matter how primitive or advanced they are, they integrate much easily with languages that are sharing C libraries, than e.g. C++. Take, for example, GObject [1] from glib.

C++ had, for a long time, problem mapping classes and objects in python or ruby, and usually, that was done via C wrappers and hacks. In recent years, things are a little better thanks to clang, but there are still tons of template hacks and C workarounds if you want portable code between compilers. Even then, you often rely on compiler specific stuff.

Other languages are much worse, so the only solution, frequently, is to use a virtual machine, like JVM or .NET.

[1] https://en.wikipedia.org/wiki/GObject

Post reply on HN