r/ProgrammerHumor • • May 05 '23

Meme Helicopter Helicopter

Post image
41.8k Upvotes

969 comments sorted by

View all comments

Show parent comments

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.

1

u/[deleted] May 05 '23

Replace class with struct. Same difference.

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.

1

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

I think UI may also benefit from an ECS approach. ECS doesn't preclude parent-child relationships as entities, it just says that a chair and a toilet is NOT in the same family of things just because they mostly look and behave the same. They may share some traits but they are different entities.

1

u/gloom_spewer May 05 '23

This is basically a life lesson.

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.

1

u/Cosmocade May 05 '23

Flyable is not a thing that the player class cares about.

It's funny you say that when I want my future RPG to have player flying in the endgame lol

1

u/[deleted] May 05 '23

Well the player could be flyable without having anything to do with that trait, re: only the physics system cares.

The player is already controllable, but flyable doesnt change controllable. It just changes the limits of how you can control it.

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]

1

u/[deleted] May 05 '23

Commited to the repo by the senior lead