r/programming • • May 28 '14

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

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

67 comments sorted by

View all comments

5

u/archagon May 28 '14 edited May 28 '14

I wish I could read this article with a clear mind, but the author's stance is just so... bloody arrogant that I have a hard time doing it. How can you say that object orientation is "objectively bad" when most of the best software in the world is written using object-oriented principles? I dunno — maybe the decades of research starting all the way from Smalltalk have actually been conducted by very intelligent computer scientists who knew what they were talking about? In my experience, it seems that it's mostly old-school programmers in their 40s who prefer the described imperative approach. Given the absolute cornucopia of fascinating new programming paradigms out there today, I feel that they're a bit stuck in the past.

Additionally, I know of literally zero programmers who write things out on index cards and create rigid Employee/Manager/Contractor-like hierarchies.

(With that said, enough smart people have complained about OO that I'm willing to give it more thought.)

2

u/[deleted] May 29 '14

maybe the decades of research starting all the way from Smalltalk have actually been conducted by very intelligent computer scientists

You are definitely overestimating the amount of "research" that has gone on. Most research is about introducing new concepts, and finding a solid mathematical basis for those concepts. There's very little research that attempts to answer whether one methodology is better than another one. Instead we have millions of practitioners each doing incremental trial-and-error to eventually settle on the best practices (which is useful too).

FWIW, I'm not an old school programmer in my 40s and I agree with their anti-OOP attitude. The problem with OOP is that it's overprescribed. Ever since Java there has been a trend of "everything should be represented with inheritance", which leads to some ridiculous code.

-1

u/archagon May 29 '14 edited May 29 '14

I'd call the developers of C#, Cocoa, Ember.js/Angular.js, etc. "reserachers". They're professional computer scientists with decades of experience who've determined that OO (of a particular flavor) is the way to go, at least for their domains. Why wouldn't I trust their expertise?

Here's practical example. Maybe you could help me figure it out. Let's say you're building a little app for scheduling events for a group of people. The main screen for this app is a scrollable view that shows all the currently scheduled events (x = time, y = person). You add events by "painting" them on with your finger on the x axis. The app has to have the following features:

  • Whenever anything changes the scheduling data, the view has to update immediately to reflect those changes. The data could change through network sync, another app, another view, etc.

  • The changes have to animate. If you scheduled something for 11:00am—12:00pm in the app and then somebody adds 5 minutes through the web client, you have to see the scheduled event animate in the app to expand its width by 5 minutes. It can't just get replaced immediately by an 11:00am—12:05pm event. (This animation should continue if you're scrolling the view with your finger.)

  • Undo/redo functionality. Also has to animate.

  • The scrollable view has to decelerate with the appropriate speed when let go, and bounce a little when it hits the edge of the timeline.

  • If you're adding an event and you accidentally overlap with the next event in your timeline, the screen should shake slightly, the event you're currently "painting" should blink red, and the width should animate backwards until it's no longer overlapping. These animations should continue even if you lift your finger and then scroll the view.

  • Some of the buttons should animate and "expand" to add additional UI when held down for 1 second.

  • When the buttons are held down, the hold should cancel when the user moves their finger outside of a certain radius from the button center.

  • Finally, the scheduling view should be usable in other places with other data. Side-by-side editing of two different schedule files? Offline thumbnail rendering of the scheduling timeline? A popover with the scheduler view in another part of the app? All these things should be as easy as instantiating another scheduling view and hooking it up with the appropriate scheduling data.

I'm trying to wrap my mind around how to deal with all these overlapping animations, timers, and events in the imperative style and all I see is spaghetti code. My gut feeling is that when you have lots of "juice" in your app, it just makes sense to keep that state squared away in separate objects.

What's more, if you're writing software for a particular ecosystem, it's likely that the frameworks will all be object oriented. You might not even get control of your own run loop! And since much, if not most, development these days is done for iOS and Android, I think it's silly to say that imperative is the "one true way" when it won't even work for those platforms (unless you drop down to the C++ layer, which... ugh).

Finally, here's what I posted on the blog where I found this article:

I’ve heard from some smart people that OO is overused, so I’m very willing to look into the argument, but flame wars in programmer circles around the internet have shown that there’s no real consensus on the issue. One person on HN framed it as “thinking about code as nouns vs. verbs”, which makes sense to me. Yes, computers are just a series of sequential commands under the hood, but that’s just an implementation detail. Our users think about our software as a series of objects: a button here, a bit of text there, a thing that plays sound and a screen that shows some pictures. Why shouldn’t we code with the same mindset? Isn’t it best to work on the highest conceptual level possible?

Thoughts?

2

u/[deleted] May 29 '14

So I'm pretty sure that at the end of this, it's going to turn out that we meant different things by the term OOP and it turns out that we agree all along.

For that app you're talking about, I would definitely prefer an entity-component system. One component would receive the latest server data, and it would create entities as needed, and assign a "targetLocation" field. Another module would store the animation time, another component would keep track of the current touch/drag state, another component would figure out the final renderable metrics from all that information, and etc for the other features.

I'm trying to wrap my mind around how to deal with all these overlapping animations, timers, and events in the imperative style and all I see is spaghetti code.

I think when you say "imperative style" you're probably referring to immediate-mode GUI which Casey used in his blog post. He's written more about IMGUI in the past, it's a fun topic to google. For the level of complexity you're talking about with your app, IMGUI would be a pretty bad fit, and Casey would surely agree.

So anyway in your juicy app there would surely be some data structures that act like objects and some code that looks like classes. The blog post (and I) aren't saying "never write anything that looks like an objects" because that's effectively impossible in a complicated program. They're saying: don't spin out new structs/classes until necessary. If you can get something done with a simple function instead of a class, then do it.

Also when I say I hate OOP, other things I'm specifically talking about are:

  • Inheritance is awful and should be avoided as much as possible. There are better ways to share behavior.

  • Classes/structs are fine if needed. Interface-based polymorphism is also fine (also only if needed).

  • Class-private data is bad. Encapsulation & information-hiding are good things in general, but class-private data usually ties you into awkward designs that are more trouble than help. Instead do encapsulation by convention (make up your own rules for data visibility and stick to them).

Our users think about our software as a series of objects: a button here, a bit of text there, a thing that plays sound and a screen that shows some pictures.

That's definitely a fine philosophy that goes all the way back to the Smalltalk days. I don't personally agree.. I think the way that the end user perceives the software is usually not the most effective way to organize the implementation. Buttons aren't really buttons, they are just textures and click areas and event handlers.

Isn’t it best to work on the highest conceptual level possible?

If you change that to "most effective conceptual level" then I will agree :)