Live data from Hacker News

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

state-machine.com

21–30 of 95 posts

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

#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. Meanwhile, a backlash insisting OO is a fundamentally mistaken design element arose among some loud users of not-OO (not to say not-good!) languages. Even among users of what were called OO languages, C++ particularly, the relative importance of OO techniques has fallen off as other language facilities and conventions surfaced.

C is just not adequately equipped for what is formally defined as OO. You can cobble up a dodgy simulacrum of it, with enough effort, but what is the point? Other languages make it easy, and can invariably be used in place of C if you really feel like you want OO.

If you are coding C++, you probably long ago graduated from the notion that "OO" has any particular merit as an organizing principle, and simply use language features to achieve your goals. Some part of any big-enough system will look OO, more or less, if you care. Few working C++ programmers do.

Some languages, Java particularly, aggressively try to horn everything into the OO shoe. The result is that if what you want to do doesn't fit, you must abuse what features the language offers to achieve what you mean to do, typically with some violence to OO norms. There's nothing wrong with that. It was, rather, wrong for the language to provide you with only OO facilities to the exclusion of all else.

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

#22

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

This to me says that C is the more powerful language. C can do what higher level languages can, in not many more lines, but higher level languages cant do the low level things C can do.

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

#23

I think this approach is working too hard to realize Virtual Tables and Virtual Pointers. Another, different implementation is to define an array of function pointers, one function per method implemented, and then extend the array per subclass; each child class fills in its own or parents function pointers at init time.

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 &

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

#24
post #19

Earlier quoted context omitted.

C++ has plenty of issues, but it's really not that bad, especially modern C++. It's a huge language with tons of features, many of which you'll never use, but as far as writing C++, really not that bad. I've written a lot of C++ over the years, and I've also written a lot of vanilla C. I can tell you that you can definitely get a new project up and running much faster in C++ than in C.

It is not that bad... Compared to c Throwing Rust into the pool

Meaning blood? The sharks circle.

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

#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.

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

#26

This is a good example of how a good programmer is more important than a a complex language with complex features. C lets you use OO if you want it ,and it doesn't require many more lines to do it, but it doesn't push you to always do it. Ive used this a number of times, but its always because its the right approach for the problem, not to be object oriented because the language is.

> C lets you use OO if you want it

Tenuous - if you are willing to implement objects yourself then you could use C sure, but I don't think that means the language "lets you use OO". It's like you could also probably implement algebraic datatypes in C using unions and structs, but would that mean "C lets you use algebraic datatypes"? I would strongly argue no.

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

#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.

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

#28

Earlier quoted context omitted.

C++ has plenty of issues, but it's really not that bad, especially modern C++. It's a huge language with tons of features, many of which you'll never use, but as far as writing C++, really not that bad. I've written a lot of C++ over the years, and I've also written a lot of vanilla C. I can tell you that you can definitely get a new project up and running much faster in C++ than in C.

I'm an OO programmer, and I avoid using classes in C++. The happy path seems to be to treat at C with STL + smart pointers, not C with classes.

If you are not using classes, you are not using OO at all, by definition.

That does necessarily not mean you are programming badly, although you might be. But restricting yourself to STL and smart pointers does very strongly resemble Java Disease. You might as well switch to Java.

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

#29

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

This to me says that C is the more powerful language. C can do what higher level languages can, in not many more lines, but higher level languages cant do the low level things C can do.

This is such a common fallacy. C exposes lower-level memory management, but to then say "oh now you have pointers you can implement anything" is a push.

If you need to implement virtual tables and function pointers, you'd use C++ - there's no need to reinvent the wheel.

Besides software engineering is about focusing on the intrinsic complexity, and using languages and tools to mitigate the incidental complexity.

Post reply on HN