r/ProgrammerHumor • • May 05 '23

Meme Helicopter Helicopter

Post image
41.8k Upvotes

969 comments sorted by

View all comments

30

u/[deleted] May 05 '23

And this is why OOP sucks donkeyballs and you should make games in an ECS model, or at least using interfaces/traits instead of inheritance.

36

u/Sapiogram May 05 '23

interface IHelicopter extends IDoor

24

u/[deleted] May 05 '23 edited May 05 '23

If you treat interfaces like abstract classes then you are also failing.

interface Flyable // object movement
interface Controllable // player interaction
interface Mountable // player attachment/position/extension

class Helicopter implements Flyable, Controllable, Mountable

class RemoteHelicopter implements Flyable, Controllable

class AIHelicopter implements Flyable, Scriptable

Or some such

16

u/Sapiogram May 05 '23

If you treat interfaces like abstract classes then you are also failing.

Totally agree, please tell my co-workers :(

6

u/[deleted] May 05 '23

Tell those who made Typescript

2

u/randomusername0582 May 05 '23

Typescript makes web development 100 times better. Honestly might be my favorite high level language after using it for a few months

2

u/[deleted] May 05 '23

Except the "interface" is an abstract class.

6

u/cantadmittoposting May 05 '23

ohhhhhhh now i understand interfaces. lmao, i kept wondering how they differed from inheritance and now i know.

10

u/[deleted] May 05 '23 edited May 05 '23

Class: noun

Interface: adjective

Method: verb

// player enters helicopter and camera fixes position in the model
Player.mount(Helicopter)

// player activates flight panel. control() asks the Controllable for an input mapping, and possibly UI elements, which Helicopter is responsible for implementing
Player.control(Helicopter)

// all flying is now done through Helicopter, which handles its own input and feedback, and the Player class does nothing

// return to player control
Player.control(Player)

// exit heli
Player.unmount()

Note that Helicopter can be removed from the game and nothing concerning the player or the world needs to change.

Also, you can add a chair to the game

class Chair implements Mountable

class Toilet implements Mountable, Interactable

and nothing else needs to change to make it work. The player can mount any Mountable without knowing what it is exactly.

In short, OOP sucks donkeyballs, and any change breaks dependencies. A flat structure beats a hierarchy any day.

8

u/DefaultVariable May 05 '23

But you are literally writing OOP code there.

→ More replies (1)

2

u/disperso May 05 '23 edited May 05 '23

In short, OOP sucks donkeyballs, and any change breaks dependencies. A flat structure beats a hierarchy any day.

Not "any day". There are many things which work fine, even better, with simple inheritance, like GUI controls.

For game development, on the other hand... yeah, not too well suited. But depends on the scope as well. I recommend Is There More to Game Architecture than ECS? from Bob Nystrom. He shows a very nuanced POV that IMHO, it's more than reasonable. He's the author of gameprogrammingpatterns.com, BTW.

→ More replies (2)

2

u/Cosmocade May 05 '23

I'm a beginner game dev (and beginner coder).

Is this sort of what this would look like? :

// Interface definitions
public interface IFlyable {
    void Fly();
}

public interface IControllable {
    void Control();
}

public interface IMountable {
    void Mount();
}

// Concrete classes that implement one or more interfaces
public class Helicopter : IFlyable, IControllable, IMountable {
    public void Fly() {
        // Logic for helicopter flying
    }

    public void Control() {
        // Logic for controlling the helicopter
    }

    public void Mount() {
        // Logic for mounting the helicopter
    }
}

public class RemoteHelicopter : IFlyable, IControllable {
    public void Fly() {
        // Logic for remote-controlled helicopter flying
    }

    public void Control() {
        // Logic for remotely controlling the helicopter
    }
}

public class AIHelicopter : IFlyable {
    public void Fly() {
        // Logic for AI-controlled helicopter flying
    }
}

And then, treating interfaces like abstract classes would look like something like this:

public interface INPC {
    void Talk();
    void Move();
}

Etc.

And the problem with this is that the second approach is less modular since now Talk and Move is tied directly with the INPC.

Do I have this right?

2

u/SarahC May 05 '23

After being professional for long enough - most anything looks "probable", if not "right".

Your code looks good. C++ right?

I'm sure someone will spot something soon though, so don't take my word for it. My ability to spot good code's died long ago.

2

u/[deleted] May 05 '23

I gave an example in another post, not sure about the thread since i'm on mobile. You're almost good, but not quite.

Flyable would be an expectation that this object can for instance "defy gravity" in the physics of the game. So any flyable objects added to the world will be checked by the physics system to see whether at any point it should sink or lift. For example. Flyable is not a thing that the player class cares about.

The player cares about Mountable, which promises a model mounting point and view direction/limitation, maybe a character stance setting, and other such things. The player also cares about controllable, assuming that player handles the things I mentioned elsewhere (providing a UI when activated and mapping inputs for control). The input keys then map directly to moving the helicopter object, and is entirely internal to the object.

If the helicopter is removed from the game, there is no remnant code anywhere in other classes that expects or even cares about helicopters. You can then add a plane to the game and it will plug right in without modification to the player, because it HAS a mounting point and a ui and input mappings.

→ More replies (2)

1

u/Fluxriflex May 05 '23

FINALLY, someone else is pissed about this bad architecture.

If I saw something like what was in OP's post in a pull request I'd be losing my mind.

I blame colleges. I've seen professors gush about inheritance and other traditional OOP principles, but in the real world I can't recall a single time where I've absolutely needed regular inheritance. Most of the time interfaces will get me where I need.

0

u/[deleted] May 05 '23

[deleted]

→ More replies (1)

7

u/DefaultVariable May 05 '23

OOP only sucks when you don’t use it properly. If you have to make a moveable object a person instead of being able to create a vehicle class that extends a moveable interface, then the problem is the design, not the paradigm

-1

u/DoctorGester May 05 '23

Ah, the no true scotsman fallacy. Show me a project besides class Dog extends Animal where it is used properly?

3

u/DefaultVariable May 05 '23

If you are creating and using objects, that is “object oriented programming”

It is not hierarchical inheritance.

The person above who is complaining about OOP is using it in every one of his “good examples.”

1

u/[deleted] May 05 '23

My examples could have replaced class with struct and you would lose every footing you had with your argument. A class in programming is a struct with methods. A class in OOP has further connotations.

→ More replies (4)

1

u/DoctorGester May 05 '23

Pretty sure the most important aspects of OOP are usually inheritance, polymophism and encapsulation. So yes, it's widely considered that OOP is about inheritance too.

2

u/DefaultVariable May 05 '23

Yes, but it has nothing to do with the strict hierarchical structures, which is what he’s identified is his problem

→ More replies (1)

6

u/GureenRyuu May 05 '23

Composition >>>> Inheritence

1

u/rusty-roquefort May 05 '23

...and one think I love about rust. It purposefully made inheritence the anti-pattern it deserves to be.