Live data from Hacker News

The faster you unlearn OOP, the better for you and your software

dpc.pw

121–130 of 252 posts

Re: The faster you unlearn OOP, the better for you and your software

#121
post #26

> The vast majority of essential code is not operating on just one object – it is actually implementing cross-cutting concerns. Example: when class Player hits() a class Monster, where exactly do we modify data? Monster's hp has to decrease by Player's attackPower, Player's xps increase by Monster's level if Monster got killed. Does it happen in Player.hits(Monster m) or Monster.isHitBy(Player p). What if there's a c…

> You end up with:

> Player.hits(Monster).with(Weapon)

I think games are a particularly pathological case for OOP, and as such probably not a good example for the article's case. But FWIW, the problem with what you describe (and with OOP for games generally) is that game logic tends to be way too polymorphic for code like that. That is, players don't just Hit() monsters, they also hit items, traps, breakable terrain, etc., and they get hit by monsters, by projectiles, maybe explosions, fall damage, etc.

And the dilemma of doing all that in OOP is, you find yourself with 20 different things that can receive a Hit(), that have little in common otherwise. Some have hit points but others don't, some don't have an armor value, some need to receive knockback but others don't even have a physics body, etc. As a result, do you make Hit() accept 20 different types, with special cases for each? Or do you rejigger your class hierarchy so that those 20 classes all inherit from Hittable, etc? Either could work in this or that case, but neither's much fun.

This is all why ECS (or other aspect-based approaches) are so popular for games - they let you define very general "Hit(src, tgt)" chunks of logic, that don't care what type each object is, but can easily query whether or not they have hit points or a physics body.

But again, I think games are a pathological case here and none of this should necessarily be considered an argument against OOP generally.

Re: The faster you unlearn OOP, the better for you and your software

#122

A language has 2 main purposes: 1. communication 2. representation of concepts 1 is pretty obvious, and for a programming language this means communication between a person to a computer 2 might not be as clear, but if you know that people cannot count in languages that have no numbers, it becomes obvious. There is a tribe that only has 0, 1 and many, and guess what, they can't tell the difference between 7 and 8. No…

  OOP in that sense leans very close to concepts of normal humans. 
  Objects, things objects can do, objects have separate responsibilities, etc.
This is not entirely accurate. Human languages are closer to functional languages: they have verbs that operate on nouns. Verbs are not attached to nouns, but rather, the operation of the verb is dependent on the noun.

Re: The faster you unlearn OOP, the better for you and your software

#123
post #40

I've never liked classical OOP much, but multiple dispatch is a lovely paradigm. One doesn't define _classes_ per se, but rather just plain old boring structs. struct Player xp::Int end struct Monster hp::Int end function hit(p::Player, m::Monster) p.xp += 10 m.hp -= 20 end The nicest thing is how one one doesn't need inheritance to "add a method" to an object. One just defines my_function(s::String) to be whatever,…

As a non-OOP-thinker, this seems completely natural to me. The verb hit doesn't belong completely to one noun, so it shouldn't be forced to live within the struct/class with the data (which really is about only that noun).

What problem is solved by forcing the function to belong to one of the actors, as in joe.hit(tiger).with(sword) or something? People say things about encapsulation, but it seems here that hit may change the state of Player, Monster and Weapon, so their states can't be fully private. In this code you can also have hit(p::Player, w::Door), if this were not allowed then perhaps you'd have to have separate hit_player_monster and hit_player_door functions... is that the problem?

Re: The faster you unlearn OOP, the better for you and your software

#124
OO bugs that I see are actually in the “invisible” parts of objects, as these require more experience to know what is really going on. For example, failing to implement necessary operators (or worse, implementing them in ways that are subtly incorrect). Knowing how to implement object “glue” properly is something that just doesn’t come up if you’re using simpler programming styles, and in cases like these it’s better to pick the simplest and most maintainable approach that will suit the task to avoid pitfalls.

Re: The faster you unlearn OOP, the better for you and your software

#125
post #93

Earlier quoted context omitted.

New to OOP programming. Could you explain what sort of arguments are presented against using OOP? I am genuinely curious.

I've been programming for 15 years (so I was around for the rise of OOP). There's nothing fundamentally wrong with OOP. I still write a lot of classes. But what I haven't written in a almost a decade: code with inheritance, encapsulation or polymorphism. Nothing has exploded. Also, objects are best when used to represent actual objects (i.e. collections of data rather than collections of functions). This is OK User u…

I was about to reply in C# that would usually be done with LINQ, but then I realized I kind of just proved your point since LINQ is functional programming not OOP....

Re: The faster you unlearn OOP, the better for you and your software

#126
> At its core, every software is about manipulating data to achieve a certain goal

> This part is very important, so I will repeat. goal -> data architecture -> code.

Wait, what? No. That isn't what you just said. You said my goal was my goal and manipulating the data was the way to achieve that goal.

Take Shopify. They had a goal: Make a ton of money by running ecommerce stores.

They used OOP. They IPO'd and they're doing great.

You can argue all you want about how they would have done better if they'd done some other programming style, but the reality is that almost every startup that I see win in fields like Shopify's (where there are a ton of different concerns with their own, disparate implementation specificities[0]) do so with OOP codebases.[1]

In large corps like Google non-OOP with typed languages like Go might work great. Streams of data and all that. But for startups it's too slow. OOP is agile because you get some data and you can ask it "what can you do?" and you can trick functional or logical programming languages into kinda doing that too, but they do it poorly.

[0] Even wording this in a non-OO way was a stupid waste of time. I could have just said "different models and methods" and 99% of the people here would have nodded and the 1% would have quibbled.

[1] Some startups like WhatsApp are a bit of an exception, but even YouTube used Python.

Re: The faster you unlearn OOP, the better for you and your software

#127
post #47

I'm always confused when people criticize OOP, because most of the time the criticisms they use are just plain bad programming. Or they're using hyperbole that isn't really useful. You want to know why OOP is useful and common? Because it's easy. Easy things allow for faster development, faster onboarding of devs. Humans need a mental models of things and OOP very explicitly gives it. But like most easy things, OOP s…

> You want to know why OOP is useful and common? Because it's easy.

Try rewriting that without adjectives. Engineers are very convincible, but they need evidence. Personally, I don't find OOP to be particularly useful or easy.

Check this out... "I became a much better programmer when I started writing functional code. Now I tend to write OOP code in a functional way, but learning how to do that was and not easy, and I am not sure most would find it useful." By saying that, I'm basically taking a position that's the opposite of yours, and while it's true to me, it provides no evidence for you to help you evaluate my side of the conversation.

Re: The faster you unlearn OOP, the better for you and your software

#128
post #67
post #12

Earlier quoted context omitted.

> What does the non-OOP solution for this problem look like? function hit(player, weapon, monster) { This is a major problem with single dispatch , a popular OOP implementation choice, but not the only one. Common Lisp and C++ have multiple dispatch, which is a generally accepted solution to this problem. It seems like a lot of the challenges you're facing has to do with object-oriented design not being a good fit fo…

I don't think C++ has a dynamic multiple dispatch mechanism. You can craft one with the visitor pattern or some precompiling or macro sheanigans. I'd love to hear about the best way of doing it.

No. Not dynamic. The best way is to use Common Lisp.

I don't know if there's consensus for other, lesser solutions. Stroustrup seems to think C++ just isn't good enough[1], but good ol' function overloading is good enough for the cases we're discussing.

[1]: http://www.stroustrup.com/multimethods.pdf

Re: The faster you unlearn OOP, the better for you and your software

#129

Earlier quoted context omitted.

>I'm talking about simple, procedural hit(monster, weapon, damage). The moment your game needs to know anything else about that hit event, you're going to start creating side effects from that procedure and that QUICKLY snowballs into spaghetti code. Not to mention, just from the signature, that's going to be an absolutely huge procedure. >Because it's not worth the effort. You won't notice any difference in speed. B…

(monster, weapon, damage) is "the object". And yes, the procedure probably has "side effects". Because that's the point. > Not to mention, just from the signature, that's going to be an absolutely huge procedure. How do you intend to write less code with OOP?

>(monster, weapon, damage) is "the object".

Now an environment flag causes fire damage to deal +10% more. You're either going to have to extend the hit method again (further complicating it), or just pull that flag from the environment class in the procedure body itself (undeclared dependency). When something else outside monster/weapon/damage/environment has to change the hit calculations, you'll have to extend it again and again.

>And yes, the procedure probably has "side effects". Because that's the point.

Side effects are terrible for maintenance and debugging, you shouldn't be using that to defend your argument here -- unless you don't know what side effects are which is why it's in scare quotes?

>How do you intend to write less code with OOP?

The point is for it to be more human readable because it's unlikely there's performance impact in this case. You don't want to count code quality by lines/characters of code.

Re: The faster you unlearn OOP, the better for you and your software

#130
post #72
post #47

I'm always confused when people criticize OOP, because most of the time the criticisms they use are just plain bad programming. Or they're using hyperbole that isn't really useful. You want to know why OOP is useful and common? Because it's easy. Easy things allow for faster development, faster onboarding of devs. Humans need a mental models of things and OOP very explicitly gives it. But like most easy things, OOP s…

> You want to know why OOP is useful and common? Because it's easy. With as much proof as you have given, let me offer you a counterargument: it is common because academia loves OOP. It's easy to teach, it's easy to test. It is most decidedly not easy and very often not useful. My favorite example of how everything falls apart in due time is the color of a car. That's it, right? A car has a color. A Porsche Panamera…

> it is common because academia loves OOP.

Absolutely not! I doubt that anybody was ever taught OOP in an academic PL course, unless it was really an "introduction to programming" course, or their professor was working on this topic at the moment. The meaning of a program in an object oriented language with imperative features and inheritance is not pretty.

There are aspects of object oriented programming that are useful for structuring programs (hidden state) and others which are a recipe for disaster (recursive types). This complexity is always swept under the carpet when teaching OOP. Classes/inheritance/objects are always taught via imperfect analogies, which should tell you everything you need to know about how "easy" OOP really is.

No, the reason it is taught so widely is purely practical. It's a popular paradigm and a lot of practical programming projects might need an OOP background.

---

Edit: Just to be clear, I'm not trying to say that OOP is bad per se.

The information hiding and namespacing aspects of objects are really useful, both in theory and in practice. It's just that I think that implementation inheritance is an imperfect way of facilitating code reuse and not something you should teach to new students...

Post reply on HN