News for hackers. Stuff that matters.
Re:The faster you unlearn OOP, the better for you and your s... by jryan49 on Sunday November 25, 2018 @08:47AM (#18526839)
> 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 class Weapon involved? Do we pass it as an argument to isHitBy or does Player has a currentWeapon() getter?

Not saying this is the right way to do things but, if you're actually going to go 100% OO on something:

Player.hits(Monster) returns Hit

Hit takes a Player and a Monster in constructor, and has with(Weapon)

You end up with:

Player.hits(Monster).with(Weapon)

Then Hit reaches into said objects and deals with changing the HP, and XP. You then have encapsulated the details in the actual Hit itself, which seems correct.

It does read kind of nicely IMO...

Re:The faster you unlearn OOP, the better for you and your s... by crazygringo on Sunday November 25, 2018 @08:55AM (#18526864)
Great point... but doesn't it continue to prove the author's point that this is needlessly complex?

It strikes me as bad design if something as simple as an action (hit) now needs to become it's own class that is instantiated merely to execute a single function and then have to delete itself. That just feels like insane overhead/boilerplate, no?

The argument here is that it's far better to have player and monster be simple data structures, and a single function hit(player, monster, weapon).

Re:The faster you unlearn OOP, the better for you and your s... by jryan49 on Sunday November 25, 2018 @08:59AM (#18526878)
The Hit class may be a Singleton and only instantiated once. Creating a class is like 3 actual lines. Also at least in Java the Hit class can be an inner class of the Person class. It just feels like name spacing in that case.

It is needless complex if the code is simple.

This solution is more flexible that just a pure function. If you have different players who use different hit strategies how do you handle that with just a pure function? Lots of if statements in the function? Passing in lambdas in every case you call the function? Then you could curry the function and call it something different too and that works I guess. OOP shines when you have lots of different ways things can work in different combinations. OOP is a tool like other programming paradigms, you use it when it solves your problem better than the other ways.

Re:The faster you unlearn OOP, the better for you and your s... by abernard1 on Sunday November 25, 2018 @09:42AM (#18527049)
> If you have different players who use different hit strategies how do you handle that with just a pure function? Lots of if statements in the function? Passing in lambdas in every case you call the function?

I'll list a few strategies I've seen in different contexts:

1) Clojure has "protocols", which is a strategy of doing polymorphism where the verb is charge instead of the noun.

2) Rust has trait objects, where you have to implement the polymorphic behavior you want for a separate player type but the data is separate from the behavior and not interlinked or owned by the class. You can implement the behavior in the scope of the module where it is used.

3) You already mentioned this, but lambdas in Javascript or functors in C++ satisfy a lot of specialization requirements.

All 3 of these approaches are different ways of approaching the problem of different behavior on the same data. The key thing with them is that it allows domain specific behavior to be decoupled from an owning object. There may be reasons that OO is more suited to a problem (as you mention), but I tend to agree with the author's post around the dogma of using OO everywhere.

In the article, the author also mentions a quote around encapsulation

> Encapsulation is an object-oriented programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse.

In my opinion, this view of binding the data and behavior together stems from an idea that data is scary and can be changed at any time. In an immutable, pass-by-value, functional world, those getters and setters and indirection is just another thing in my way from composing the data together with collections tools I am very familiar with. I think it was Neal Ford who said that "OO encapsulates moving parts, whereas functional programming removes moving parts." I think that's very appropriate in this context. In a data-first view, you model what properties and data shapes you care about in your records, and then you create little functions that connect those things together; in OO, you create the links first through methods (even if those have interfaces), and you're stuck with those connections anywhere you want to pass the object.

Re:The faster you unlearn OOP, the better for you and your s... by geofft on Sunday November 25, 2018 @09:28AM (#18526993)
Ultimately the answer is to use a language in which passing a function reference is as easy as writing a singleton that implements an interface and passing a reference to that. (Or easier—in any language with decent function-passing support, writing a function and referencing it is generally going to be easier because fewer things are happening.)

OO makes sense if the different types of behavior map to object identities and inheritance trees, because it generates a chain of implicit if statements that capture this logic. If they don't—e.g., a hit changes behavior depending on whether the attacker is proficient in the weapon, rather than depending on the weapon class or attacker class itself—you're likely better off writing the if statements yourself.

Re:The faster you unlearn OOP, the better for you and your s... by gameswithgo on Sunday November 25, 2018 @09:31AM (#18526997)
Languages that force you to use the singleton pattern really ought to just let you write A FUNCTION
Re:The faster you unlearn OOP, the better for you and your s... by jryan49 on Sunday November 25, 2018 @09:40AM (#18527038)
A lot of OOP languages have added method references, and lambdas in this day and age.
Re:The faster you unlearn OOP, the better for you and your s... by dragonwriter on Sunday November 25, 2018 @01:17PM (#18527988)
> It strikes me as bad design if something as simple as an action (hit) now needs to become it's own class

Why, it's a noun in the problem domain. Why shouldn't it be a class?

> that is instantiated merely to execute a single function and then have to delete itself.

This may be inefficient in some language implementations because of limits of optimization, but there is no fundamental reason that, if that's all you do with it, the compiled code or runtime behavior needs to be much different than if it were just a procedure call.

On the other hand, lots of times you will want to do more than that with actions occurring in the domain then executing them (queueing, logging/serializing, etc.) and having a datatype for the event (a class in class-oriented OOP) supports that.

Re:The faster you unlearn OOP, the better for you and your s... by fenomas on Sunday November 25, 2018 @10:47AM (#18527317)
> The argument here is that it's far better to have player and monster be simple data structures, and a single function hit(player, monster, weapon).

Naturally it's better to be simple if the logic involved actually is simple, but surely the point of the example is that game logic typically isn't. Hits in games don't just change a hit-point value, they tend to play animations, trigger events, work differently depending on this or that piece of global state, etc.

Re:The faster you unlearn OOP, the better for you and your s... by antialias on Sunday November 25, 2018 @09:58AM (#18527117)
It isn’t uncommon, nor is it bad design to define value classes that represent simple actions. This enables the action/command/event value to be reified, serialized, transmitted over the wire, deserialized, and potentially processed remotely, asynchronously, or by multiple arbitrary consumers.
Re:The faster you unlearn OOP, the better for you and your s... by Novashi on Sunday November 25, 2018 @09:17AM (#18526951)
Hit is an event that occurs, so not sure why it should escape OO even though in this example even though it's only decreasing ints. I routinely code event objects rather than modifying another class directly by the delta that should occur. OO isn't good for continuous time and events but it does add ergonomic convenience for the developer in this case (there could be other information on the hit, a historical list of Hits looks better as List<Hit> rather than List<int>, it's easier to inspect a hit object during debugging rather than setting watches on player/monster/weapon values).
Re:The faster you unlearn OOP, the better for you and your s... by jryan49 on Sunday November 25, 2018 @09:26AM (#18526986)
You could even serialize the Hit classes and throw them on a message bus, and have them automatically applied across the network or something.
Re:The faster you unlearn OOP, the better for you and your s... by bassman9000 on Sunday November 25, 2018 @01:10PM (#18527949)
And you end up having functions that look like.

hit(player, monster, weapon, damage_modifier, arena, previous_turn, next_turn, global_modifiers)

because the author didn't like, or understood encapsulation.

Re:The faster you unlearn OOP, the better for you and your s... by mannykannot on Sunday November 25, 2018 @02:27PM (#18528462)
In the OO version of this pathology, these arguments become the instance variables of a class introduced to do nothing more than hold them.

Poor design has a way of cutting across programming paradigms, or even (in the case of inappropriate inheritance, for example) leveraging their features.

Re:The faster you unlearn OOP, the better for you and your s... by pas on Sunday November 25, 2018 @06:51PM (#18529864)
What's wrong with config/value holder objects? Most OOP languages can store them as simple structs in memory.
Re:The faster you unlearn OOP, the better for you and your s... by mannykannot on Sunday November 25, 2018 @08:34PM (#18530342)
It has nothing to do with efficiency. Insofar as Bassman9000's example indicates a real problem (and there are a couple of ways it might do so, ultimately leading back to the poor use of abstraction and separation-of-concerns), stuffing the arguments (or any other weakly-related collection of variables) into an ad-hoc struct (or object) having no real cohesion is simply sweeping the dust under the rug, and is just as indicative of a likely design problem as are long argument lists of nominally unrelated data.
Re:The faster you unlearn OOP, the better for you and your s... by pas on Monday November 26, 2018 @04:46AM (#18532174)
Why is no cohesion, in, let's say a HitEvent object? This knows the target, source, method (weapon, magic, physics like falling down), etc. You can make handy helper methods and subtypes depending on important concerns.

I don't get the adhocness of the example.

Re:The faster you unlearn OOP, the better for you and your s... by 8note on Sunday November 25, 2018 @05:03PM (#18529349)
I for one, would ocassionally introduce a bug where the monster hits the player instead of the other way around
Re:The faster you unlearn OOP, the better for you and your s... by fenomas on Sunday November 25, 2018 @10:02AM (#18527127)
> 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 s... by scarface74 on Sunday November 25, 2018 @10:11AM (#18527166)
I find the fluent style programming to be much more readable. But in OOP you would define an IHittable interface or use AOP like you suggested.
Re:The faster you unlearn OOP, the better for you and your s... by fenomas on Sunday November 25, 2018 @10:40PM (#18530864)
You can define iHittable and get the code to compile, but it doesn't make the actual issue (polymorphism) any easier to tackle. I think eschewing inheritance in favor of aspects is definitely the way to go.

(Of course, you can wrap everything in a fluent-style interface either way - that part is orthogonal to all the rest.)

Re:The faster you unlearn OOP, the better for you and your s... by pas on Sunday November 25, 2018 @06:52PM (#18529875)
Re:The faster you unlearn OOP, the better for you and your s... by ActingMike on Sunday November 25, 2018 @09:55AM (#18527109)
OOP is here to stay because it is the default model of how we see the world. You actually do not have to teach people to use this, just how to map it to the OOP Software Systems like Java/ C#.

It was great while Moors law still lasted, and to write efficient software was becoming some strange quest for some formula one fields of software like games or other massive workload fields like OS-Wizzardry.

Today, the massive workload is still growing, due to sloppy architectural habits and user demands, but the saviors of parallelization and new chip technologies have not come to rescue the sloppy-devs from the laws of leaky abstraction.

Your code runs on a hardware system.

And currently your code is running out of hardware.

And with that OOP is running out of excuses.

So you would write this. Then it would three months before the shipping be handed to some adult- and this adult will delete it. All of it.

Its full of references, full of class-bloat which will push one another out of the cache. Even though the compiler will try to yank the worst out this mental crutch out of the code. Some references can not be dismissed deterministically, so they will be kept. Which makes it incredible slow.

What replaces it will be a efficient structure, keeping all the chunks necessary for collision detection, in one very array like field.

Over this a Algo traverses, acting with a function, which does not reference non-local stuff, detecting collisions and damage. Allowing not only for a few players- but a workload of players.

It will look very ugly, like C, with inline assembler- ugly, and it will be ready to ship.

Re:The faster you unlearn OOP, the better for you and your s... by naasking on Sunday November 25, 2018 @11:18AM (#18527441)
> OOP is here to stay because it is the default model of how we see the world

A common but false statement. Firstly, there is no standard definition of OOP, only loose sets of features that define various overlapping but disjoint paradigms that some subset of people call OO. So your statement is meaningless as there is no single OOP that we identify as how "people see the world".

Secondly, even given any specific definition of OOP, it's also false. There are many programs that simply aren't suited to OO modelling, because you want to express things as the problem being solved actually requires, and this is often not OO. Sometimes pattern matching is best (compilers), sometimes reactive/event paradigm is best (servers, UIs), and neither of these are OO, as but two examples.

And don't take my word for it, there are a few studies on novice programmers showing quite clearly that event-based temporal reactive primitives are what people find most intuitively natural, ie. when account balance <= O then do some action, which is a declarative reactive action that will execute when that condition becomes true. This is not an OO program in any sense.

Re:The faster you unlearn OOP, the better for you and your s... by jstimpfle on Sunday November 25, 2018 @08:53AM (#18526856)
> It does read kind of nicely IMO...

And this one line is why you're willing to setup all these crap boilerplate supporter classes and take a huge hit in runtime performance?

Re:The faster you unlearn OOP, the better for you and your s... by jryan49 on Sunday November 25, 2018 @08:58AM (#18526872)
A lot of languages are made for this kind of interaction and can inline stuff hopefully, but that's the job for the VM or compiler.

I didn't say it was the "right" way to do it. Also programming is the art of tradeoffs. If this does actually give bad performance, maybe it's a trade off the programmer is willing to make.

Just like the article started off with "I don't think there's a silver bullet". OOP is a tool just like any other with pluses, and minuses.

Also the Hit class may be a Singleton and only instantiated once.

Re:The faster you unlearn OOP, the better for you and your s... by Novashi on Sunday November 25, 2018 @09:27AM (#18526991)
>And this one line is why you're willing to setup all these crap boilerplate supporter classes and take a huge hit in runtime performance?

Hits don't usually happen that often in games relative to everything else the engine is doing unless you're writing something like MMO server code that processes hits from thousands players.

Yours is a classic case of over-optimizing, I think.

Re:The faster you unlearn OOP, the better for you and your s... by jstimpfle on Sunday November 25, 2018 @09:30AM (#18526995)
Over-optimizing as in, write the one obvious line instead of 15 lines of crap that are also slower?
Re:The faster you unlearn OOP, the better for you and your s... by jryan49 on Sunday November 25, 2018 @09:38AM (#18527024)
These examples are supposed to be examples, not what we would actually do in this specific case. In a huge enterprise system that has 500 different ways something could be done, in which your function would have 30 million if statements, all which is dependent on runtime behavior, polymorphism can help solve that problem.

Imagine there are hundreds of ways Hit can be implemented and used in different situations. That's the reason for breaking out this type of abstraction. I agree this example the author used isn't probably a clear one in which one should use OOP.

Re:The faster you unlearn OOP, the better for you and your s... by jstimpfle on Sunday November 25, 2018 @09:52AM (#18527095)
> Imagine there are hundreds of ways Hit can be implemented and used in different situations.

This is the typical argument you get to hear from OOP apologetists. And the codebase is rotting, just for concerns about hypothetical problems...

I remember one time when I was criticized for putting in too much global data and not enough classes "because what if we have two GUI instances?". The guy of course had no idea why he would want that, and how it should work.

Re:The faster you unlearn OOP, the better for you and your s... by Novashi on Sunday November 25, 2018 @09:36AM (#18527021)
Until you get a decent combat system in and now your hit has to go through 30+ calculations. It's almost never as simple as "monster.HP - damage".

Besides, if we really want to be snarky, why haven't you wrote it in assembly for performance?

Re:The faster you unlearn OOP, the better for you and your s... by jstimpfle on Sunday November 25, 2018 @09:40AM (#18527037)
No, I'm not talking about one arithmetic CPU operation. I'm talking about simple, procedural hit(monster, weapon, damage).

Why not in assembly? Because it's not worth the effort. You won't notice any difference in speed. Be pragmatic.

Re:The faster you unlearn OOP, the better for you and your s... by Novashi on Sunday November 25, 2018 @09:51AM (#18527082)
>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. Be pragmatic.

That's the exact same case I'm making. A hit object every so often will have no impact on performance (certainly not a huge impact) like you originally said and you gain developer productivity.

Re:The faster you unlearn OOP, the better for you and your s... by jstimpfle on Sunday November 25, 2018 @09:54AM (#18527105)
(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?

Re:The faster you unlearn OOP, the better for you and your s... by Novashi on Sunday November 25, 2018 @10:09AM (#18527156)
>(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 s... by jstimpfle on Sunday November 25, 2018 @10:26AM (#18527235)
> When something else outside monster/weapon/damage/environment has to change the hit calculations

When something should change, you edit the freakin' code. There is no way around it. If you have different kinds of hits then you make different procedures. e.g. magicPixieDustHit(monster, pixieDust, weapon, damage).

> Side effects are terrible for maintenance and debugging

FP apologetists want to make you believe that, but side effects are the actual point of the program.

> The point is for it to be more human readable

   hit(player, weapon, monster)

   Player.hits(Monster).with(Weapon)
I mean at least acknowledge that that's a highly subjective statement...
Re:The faster you unlearn OOP, the better for you and your s... by Novashi on Sunday November 25, 2018 @10:57AM (#18527346)
>When something should change, you edit the freakin' code. If you have different kinds of hits then you make different procedures. e.g. magicPixieDustHit(monster, pixieDust, weapon, damage).

I dunno, maybe aim for something a little better than writing an exponential amount of functions for all of your interactions.

>FP apologetists want to make you believe that, but side effects are the actual point of the program.

If you want to make all of your co-workers hate you because randomProcedure changes shared state when it shouldn't, go right ahead. If you want to stop that, you're like two steps away from OOP's dependency injection when you write validator functions on the shared state object.

>I mean at least acknowledge that that's a highly subjective statement...

Games are about the worst choice for criticizing OOP because OOP is so useful for describing games. Procedural style is fine for Pong or Sudoku or really simple games, I guess. It's highly subjective in-so-far as the entire industry has mostly adopted it and released great games with it.

Re:The faster you unlearn OOP, the better for you and your s... by jstimpfle on Sunday November 25, 2018 @11:04AM (#18527378)
> I dunno, maybe aim for something a little better than writing an exponential amount of functions for all of your interactions.

You mean "polynomial". And you are still not acknowledging that OOP doesn't help there either. There is only one way out: You as the programmer must do the sensible thing. If you really need runtime polymorphism (which you rarely need, if you structure things correctly and keep separate things separate), then for god's sake go ahead and do it. But in my opinion it's much cleaner to do it with explicit function pointers.

> Games... > the entire industry has mostly adopted it and released great games with it.

I think you are at least 10 to 20 years late. The games industry, especially in the AAA sector where performance is critical, seems to long have acknowledged that OOP doesn't work out.

Re:The faster you unlearn OOP, the better for you and your s... by atoav on Monday November 26, 2018 @01:13AM (#18531497)
Please consider watching (or reading) this talk. If you are interested in how a modern Game ECS works and why indeed OOP often isn’t the way to go: https://kyren.github.io/2018/09/14/rustconf-talk.html
Re:The faster you unlearn OOP, the better for you and your s... by jcelerier on Sunday November 25, 2018 @08:58AM (#18526874)
> And this one line is why you're willing to setup all these crap boilerplate supporter classes and take a huge hit in runtime performance?

at least in C++, why would they ? it'll all gets inlined and you can do it so that there isn't any memory allocations if you know all of your cases at compile time (of course most of the time you want to be able to add new weapons, behaviours, etc, at runtime but in game engines you generally use scripting languages for this anyways).

Re:The faster you unlearn OOP, the better for you and your s... by jstimpfle on Sunday November 25, 2018 @09:32AM (#18526999)
It's nice that some C++ compilers can optimize this out. It means that it only costs maintainer cycles (less straightforward invocation), and compiler cycles. As every C++ programmer knows, there is an unlimited amount of them.
Re:The faster you unlearn OOP, the better for you and your s... by matthewmacleod on Sunday November 25, 2018 @09:48AM (#18527070)
I'm pretty confused by how you perceive this. I don't see "crap boilerplate supporter classes" – I see objects that encapsulate events and actors for all the usual OOP reasons.

I wonder if some of this disagreement is down to the way that different people abstract this problem in their heads.

Re:The faster you unlearn OOP, the better for you and your s... by jstimpfle on Sunday November 25, 2018 @10:49AM (#18527325)

   // player.hits(monster).with(weapon)

   add to class Player {
       // XXX: Bad coupling Player -> Monster
       // XXX: Bad coupling Player -> PlayerMonsterHit
       method hits(Monster monster) {
           return PlayerMonsterHit(player, monster)
       }
   }

   class PlayerMonsterHit {
       member Player player;
       member Monster monster;

       construct PlayerMonsterHit(Player player, Monster monster) {
           this.player = player;
           this.monster = monster;
       }

       method with(Weapon weapon) {
           doTheActualFrigginHit(this.player, this.monster, weapon);  // this line is all we REALLY need
       }
   }
I guess 15 lines (that do NOTHING) was not an overestimation.
Re:The faster you unlearn OOP, the better for you and your s... by matthewmacleod on Sunday November 25, 2018 @11:11AM (#18527411)
They don't "do nothing" - they define structure that can then be used to handle more implementation later. It's a ridiculous, contrived example, and you know that.

Yes – this code is stupid if all you need to implement is a single line with the ability for a player to hit a monster. But that's never what you are implementing, is it?

Re:The faster you unlearn OOP, the better for you and your s... by jstimpfle on Sunday November 25, 2018 @11:37AM (#18527512)
If you want to delay the hit, i.e. transactioning / buffering / delaying: This is sound engineering! But that part is not even included in above boilerplate. It would be:

    struct PlayerMonsterWeaponHit {
        Player player;
        Monster monster;
        Weapon weapon;
    }

    Array<PlayerMonsterWeaponHit> playerMonsterWeaponHits;

    doTheActualFrigginHit(Player player, Monster monster, Weapon weapon) {
        playerMonsterWeaponHits.emplace(player, monster, weapon);
    }
In case it isn't obvious you can do that with either implementation, the above OOP boilerplate or the above imperative 1 line of code.

> Yes – this code is stupid if all you need to implement is a single line

No, it's stupid because it doesn't do anything. The data structures are only for temporaries that will never have any other use. In the end it's going to be PlayerMonsterWeaponHit. So the other classes are just stupid, and no amount of context will change that.

Re:The faster you unlearn OOP, the better for you and your s... by mannykannot on Sunday November 25, 2018 @10:37AM (#18527268)
"Then Hit reaches into said objects and deals with changing the HP, and XP."

This is not essentially different from what happens in non-OO code. OO purists would probably object to this as a violation of encapsulation, and would insist on objects receiving a hit performing the update to their state themselves.

Stepping back from this example and considering transactions in general, it is often the case that there are certain constraints to be observed, and in such cases, it is easier to verify the solution if it is done compactly in one function. In such cases, creating a class and instantiating it just to have that function would be pointlessly excessive obeisance to a principle, and making it a singleton would only underscore that point.

Re:The faster you unlearn OOP, the better for you and your s... by arriu on Sunday November 25, 2018 @09:22AM (#18526971)
This does read nicely in most cases.

There are minor issues when you venture off and decide that it would be cool if a monsters could damage your weapon. Something like Monster.hits(Weapon).with(Weapon). You'll start wondering if Weapon should inherit from the player base class. If not, you'll spend time wondering how to share the code to keep things dry. Obviously there are many solutions even within OOP, but one might consider looking to ECS to remove code duplication and decouple things.

Lots of good examples in the game programming patterns book: http://gameprogrammingpatterns.com/component.html

Re:The faster you unlearn OOP, the better for you and your s... by jryan49 on Sunday November 25, 2018 @09:25AM (#18526982)
I think damage to the weapon could still be handled in the Hit class, because Hit has a reference to Player and Monster and Weapon. You could even create different types of Hit classes that could execute different kinds of Hit behavior like this.

In that case it may be better to rename it to HitStrategy?

Re:The faster you unlearn OOP, the better for you and your s... by alasdair_ on Sunday November 25, 2018 @09:42AM (#18527048)
>You'll start wondering if Weapon should inherit from the player base class.

Probably not. If you want to do that, they should both implement an interface (and perhaps share some code through composition) but code inheritance is likely not the right approach here.

Re:The faster you unlearn OOP, the better for you and your s... by szastupov on Sunday November 25, 2018 @11:21AM (#18527454)
> Player.hits(Monster).with(Weapon)

The problem is that it's an artificial example, in reality you'll ave a lot of different monsters, weapons, and most importantly separate subsystems. I.e. physics processed in one pass, animation in another, same goes for custom logic and rendering.

Modern game engines use component systems that put data first for the same reasons author described in the post.

Re:The faster you unlearn OOP, the better for you and your s... by lerno on Sunday November 25, 2018 @07:05PM (#18529926)
Been exactly there with a game. Solution? Take a data centered approach: a command object (just a function really) updates HP, XP, triggers visual effects etc. Monster and Player classes are just thin wrappers around plain data structures.

I tried the more ”OO”-approaches for all too long thinking it would lead me right. It doesn’t.

Re:The faster you unlearn OOP, the better for you and your s... by iokevins on Sunday November 25, 2018 @10:50AM (#18527329)
Re:The faster you unlearn OOP, the better for you and your s... by scarface74 on Sunday November 25, 2018 @11:30AM (#18527484)
First I had to have restraint to not downvote you for making me go down that very interesting 5 post rabbit hole....

Second, in one of the articles I found one of the best quotes I’ve ever heard about when not to use exceptions....

When you’re writing code in the IDE, it is correct code that is the exception!

Re:The faster you unlearn OOP, the better for you and your s... by z3t4 on Sunday November 25, 2018 @02:03PM (#18528293)
it's a trap. you don't want to complex objects/classes. keep them simple!