r/programming • • May 28 '14

"Compression-oriented programming" - Casey Muratori's blog

http://mollyrocket.com/casey/stream_0019.html
126 Upvotes

67 comments sorted by

View all comments

11

u/[deleted] May 28 '14

If his stance is generally against OO, then I agree with him for the most part. OO has its uses, but it's far overused, overhyped, and over emphasized in terms of importance. 95% of OO code I've read (including my own) has been poorly implemented and naively designed.

I don't think any one paradigm is a one-stop solution in terms of its methodology (nor should a paradigm never be used). A lot of people in the industry tend to have a major hard-on for OO though...and it tends to be attractive to newbies as well, once they've grasped the concept of classes.

In my opinion, as soon as the focus shifts toward "objects" and extremely pedantic notions in regards to how aesthetically pleasing one's code is, the point has been lost. I still see a very large portion of code employing the use of getters and setters, the majority of which provide absolutely no benefit, and have witnessed near heated discussions on things like IsNullOrEmpty() function calls being argued against, not because of what they do, but because of their title.

Good code is code which is readable, as simple as it needs to be to function properly, is defensive where it needs to be, conforms to a clear set of pragmatic standards, and has a reasonable amount of comments. Anything else is pretty much subjective and detracts from the point, which is to write software which works well for the user and is only as complicated to maintain as is necessary.

So, OO itself isn't necessarily bad; it's the philosophical connotations and over-thought code pollution which unfortunately tends to come with it that's bad.

6

u/ummwut May 29 '14

The problem with OO is exactly that it's easy to grasp, and thusly strongly supported at all levels of programming, so you never get introduced to what's actually happening under the hood until much much later.

Which is a shame, because under-the-hood stuff is what made me respect the OO model and use it more effectively.

1

u/loup-vaillant Jun 01 '14 edited Jun 01 '14

The problem with OO is exactly that it's easy to grasp

Is it?

I rate myself as a good programmer, proficient in quite a few languages (most notably C++, Ocaml, and more recently, Lua). I have read, and heard, and learned about "OO". I know a fair bit. Yet I have never been able to satisfactorily understand what it is all about. The concepts are fuzzy, and the jargon is confusing. Sure, I've become quite confident over time that OO is mostly a load of crap…

…but I still cannot say in good conscience that I really get it

1

u/ummwut Jun 01 '14

OO at its heart is simply calling functions bound to structs in different ways. Before, when you wanted to do something like OO, you'd have to pass the struct as an argument to the functions meant to deal with the structs. With the OO model, the struct (object data) is passed invisibly, accessed by the variable "this". Every function call, therefore, only tacitly refers to the struct which it needs to access.

For a fun exercise, try copying the OO behavior in C++ with C. In fact, Lua supports this with syntactic sugar to make OO-like code less annoying: "object:function(...)" is short for "object'function'".

1

u/loup-vaillant Jun 01 '14

I know all of this. The mechanism you speak of is only useful for delegation and virtual functions however. Without it, you would just have x.f() be syntax sugar for f(x). I believe D has such syntax sugar.

Of course, there are those who would say that inheritance (class based) or delegation (prototype based) is somehow central to OO. Then there are those who warn you to stay away from those, and use composition instead.

Even more telling, I recall coming up once with a design inspired by my Ocaml experience. When I shared it, the reaction was "it looks nice and very OO, but…". Okay, since when FP == OO?

So I gave up. OO isn't worth my time any more. I can already write decent C++ and Java code already, so why bother?

1

u/ummwut Jun 02 '14

I wish others echoed your sentiments, but then again, I know they won't since my comment above still rings true: OO is easy to grasp.

1

u/loup-vaillant Jun 02 '14

Well, some OO is easy to grasp. We all get the person/employee/boss, vehicle/car/bike, colour/red/green inheritance stuff, we all get the x.f() syntax, and above all, we all get the abstract data type stuff, which by the way is not exclusive to OO (I believe modular programming got it first).

What is less clear however is how to use those mechanisms. We could try to "model reality" directly, but that doesn't always work well. We can think up more abstract classes and objects, but it is not clear how one should come up with them.

Then the time came when this didn't feel right any more. Fragile base class problem means inheritance is not so great as a reuse mechanism. Subclass polymorphism is cumbersome, closures are simpler to use. And this genericity stuff was being done by ML languages long before C++ and Java decided it was most OO. All those signs indicate that OO might not be so great after all. Worse, the distinctive features of OO may be the one that suck the most.

Then an OO proponent comes and tell me I don't "really get" OO. And another starts insinuating that bad code is not OO pretty much by definition. And a third starts using big words around "modelling" or something. At that point, I can't tell if there is any substance, or if they just weasel out of criticism.


This "Compression Oriented Programming" on the other hand, I can understand. The objectives are clear and measurable (the LOC count is a good proxy), the methodology is simple and easy to implement, and the "less code is better" conclusion looks obviously true.

1

u/anto2554 Jun 05 '23

As someone still quite new to learning, I hated that way of explaining OO. It was always my professor saying that you could have a Car class, but whenever i program something i never have a class 1:1 corresponding to a physical object

2

u/loup-vaillant Jun 05 '23

My, that takes me back. One thing to add about this piece:

We could try to "model reality" directly, but that doesn't always work well.

Mike Acton has since convinced me that modelling reality directly is a straight up bad idea. Computers do exactly two things: moving data, and transforming data. This is the case even for video games, where said data represent a whole world we may immerse ourselves in.

Thus, our job is to solve problems by transforming and moving data around, using the computational hardware at our disposal. Said like this it sounds a bit tautological, but in practice we still need the remainder.

Thus, to do our job as best we can, we should model our code around a model of the data, not the world. Once you do that, you quickly understand that even in a simulation, a static rock that never moves requires very different data than a rock that may be pushed around and strike enemies as it rolls downhill. Almost identical world, very different data.