Am I thinking right OO way?

Started by
66 comments, last by oleksiy 13 years, 2 months ago

[quote name='Madhed' timestamp='1299060955' post='4780920']
Erm, nobody tried to shove his beliefs down your throat. All I see is simply a: "I think this is better, think about it" Yet you are the one who gets all jumpy about it.


Wasn't referring to how you handled things. You were fine. Ravyne said things like "You're wrong this is why" and Hodgman summarized my entire argument as "[color="#1c2837"]Science is wrong, biatches!" and stated, but I guess removed, his opinion that I've degenerated this thread into trolling by having an opinion. ^^

"[color="#1c2837"]You can't grantstand on "fighting the established order of things" without nodding to the fact that many of the "rigid" standards have already weathered generations of cynicism, scrutiny, and testing through application. You also have to accept that the answer to "questioning those before us" is sometimes "well, yes, they WERE right.""

[color="#1c2837"]Very true. They often are. But I just don't live my life blindly. I don't see the point in living life by the book without questioning it. But the matter at hand can't be tested XD That's why I stated that it's not science. There's no measurement for success. There's no performance increment to look at. If you can't test it, isn't it an opinion? If someone tells you that the sky is green what if they really see it as such? There's no way to prove them otherwise but by collective "opinion".
[/quote]

Let me add my own small opinion to this. Yes, people should just blindly follow a widespread practice in an industry until they understand why exactly it is there. In general, these practices are there because countless professionals, each with many years of practice, have tried out different methodologies, different paradigms and formed valuable opinions, a consensus. You can swim against the tide if you want, though you won't get anywhere fast.

Also take into account convention. When you are part of a team, it is to your advantage to follow common ways of thinking because that results in more efficient communication and thus faster, more painless work.



Advertisement

[quote name='Madhed' timestamp='1299060955' post='4780920']
"[color="#1C2837"]You can't grantstand on "fighting the established order of things" without nodding to the fact that many of the "rigid" standards have already weathered generations of cynicism, scrutiny, and testing through application. You also have to accept that the answer to "questioning those before us" is sometimes "well, yes, they WERE right.""

[color="#1C2837"]Very true. They often are. But I just don't live my life blindly. I don't see the point in living life by the book without questioning it. But the matter at hand can't be tested XD That's why I stated that it's not science. There's no measurement for success. There's no performance increment to look at. If you can't test it, isn't it an opinion? If someone tells you that the sky is green what if they really see it as such? There's no way to prove them otherwise but by collective "opinion".
[/quote]

It's one thing to question things, and honestly you are entirely within your right to say "Well, I know I'm violating established practice, but it hasn't blown up in my face yet, so I'm fine with it." however, composition vs. inheritance, and the appropriate use of such, is well established in the computer science community. As I stated earlier, LSP and SRP have been vetted by hundreds of smart people over the past 30-40 years -- you may not see the difference adhering to these principles make in small projects, but in large projects which change and have to be maintained and extended over time, you will rue the day you chose inheritance over composition when it was not warranted.

You charge that the benefit is immeasurable and therefore opinion -- false on both accounts. While hard numbers like performance or binary measurements like "well, did it work in the end" don't seem to indicate a difference, and "source code quality" might seem far too subjective to be a useful measure, I would argue that the latter is not as subjective as it seems, and indeed at least as useful a measure as the former.

Let us assume that there exists a group of very smart computer scientists, and let us assume that they have written a great deal of software, much of it medium or large-scale. Let us also assume that they have an academic and professional interest in developing the best methods possible, and that originating said methods is incentively (prestige, better jobs). Now let us assume that these people, having gotten together to share their experiences noticed certain patterns across their independently-developed projects. They talk about what works and where their individual solutions fall flat, and over time they co-develop a solution to a particular problem that seems to have the greatest benefit and the least amount of headache in aggregate. This is what a Design Pattern is (keeping in mind that a pattern is just an idea to be applied, not a thing to be applied.), this is what idioms are, this is what stands behind "theories" like LSP, SRP and innumerable others.

What you seem to insist on arguing, is that your one opinion, based on your own limited experience, should call into question the collective work of literally thousands of man-years of insight on these topics. If that's what you want to *do* then fine, go do so -- I'm confident enough in these assertions to say that, without question, your casting aside of these principles will one day make your programming life more difficult than it need be. But I would be remiss as a member of this community and as something of a computer scientist myself, if I did not counter such wild claims. You offered a solution that works, I offered one which "is better" -- and I can't point you to a chart or statistics, only to the collective experience of hundreds of minds greater than my own, or your own. If you want to take those odds on, so be it, but to recommend following suit is negligent.

throw table_exception("(? ???)? ? ???");


I thought I made that fairly clear with the 'no inheritance' bit, but here goes...

Four concrete classes: Paddle, Player, Enemy and World (rename as you see fit). Player and Enemy each hold an instance of Paddle via composition. Paddle contains a position, and a collision primitive. Player processes input and moves the player paddle, Enemy performs AI and moves the enemy paddle. World contains references to both Paddles (*not* Player and Enemy), and simulates the ball.

Even though Player and Enemy may share certain attributes, there is no reason for them to present a uniform interface, because there is no client to make use of that shared interface.

I was under the impression that the Player and Enemy classes would be holding relevant player data, not controlling the paddle. I guess this depends on how complicated the rest of the game gets. I think we just have different concepts of the scope it should be designed for.

Player and Enemy should be the subclass of some class if you want to extend to have more than 2 players so they can be stored in a player array or have variable numbers of ai and human players.
I wouldn't think about the number of classes versus the size of the game. When I am designing games or applications I think about how to logically break things up into classes that makes sense. In the end, my solution may be 4x larger with all of the class definitions and extra curly braces and other crap but my design is a true object oriented approach and easily maintainable opposed to 1 UBER class that has everything in it.

When I wrote a pong clone I had the following;
-Abstract paddle class
-Enemy class extends paddle
-Player class extends paddle
-Ball class
-Wall class that were the outter boundaries for collision

Remember to mark someones post as helpful if you found it so.

Journal:

http://www.gamedev.net/blog/908-xxchesters-blog/

Portfolio:

http://www.BrandonMcCulligh.ca

Company:

www.gwnp.ca


It's one thing to question things....


I think this is the first thing you said that I can mostly get behind. ^^ It's really hard to argue this without you knowing the exact circumstances in which I use my method. For example, when on team projects, I follow what ever pattern I'm told to. Obviously you should or it would be a programming nightmare. However, when doing solo projects, I see nothing wrong in my method. For example, I usually inherit player from an animated sprite class in my games. Why? It just make my life easier. It's the way I code. Nothing will ever inherit from player. There's no in between inheritance so why not? I've done small things like pong, to mid size puzzle games, to a full blown link to the past style clone and have *never* ran into an issue. Ever. At least within the context of what we are discussing.

I have been programming for only 9 years and that's still very young for this industry but you have to admit, if I've done so many projects of different sizes then maybe just maybe there can be two methods. Sure yours may be better in your opinion but it's still subjective. To me, if the performance is the same then "better" falls to whom ever's method makes more sense to the programmer.

"[color="#1C2837"]What you seem to insist on arguing, is that your one opinion, based on your own limited experience, should call into question the collective work of literally thousands of man-years of insight on these topics."

[color="#1C2837"]It only takes one person who sees things differently to define an alternative. ^^
Ravyne's sentiments are applied to general software engineering. Not personal projects.

Since the OP asked for 'best practices in OO', without mentioning a team-size or industry scope, Ravyne made the (correct) choice of illuminating 'best practices for oo in an industry setting'. This is where your "my method has always worked for me" argument falls flat. You openly admit that you could make bad design choices, such as a player class inheriting from an animated sprite class, based on the assumption you'll be the only one who had to extend/understand/maintain the code. You cannot, honestly, believe your approach would be "best practices" in a mature, enterprise sized software system.

My job, as a software engineer for a DOD contractor, entails maintaining code that is literally 20 years old and has hundreds of thousands of lines of code. Unnecessary inheritance is one of the worst parts of understanding this code base, and I'm very grateful most of it is done composition-first.

Player and Enemy should be the subclass of some class if you want to extend to have more than 2 players so they can be stored in a player array or have variable numbers of ai and human players.


Thus why I said they should be subclasses only if they need to present a uniform interface to some client. My broader point is that in the context of Pong, there is no such client, so there is no need for a uniform interface. Were we instead designing 'Pong the MMO', we might have to revisit that distinction...

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

You openly admit that you could make bad design choices, such as a player class inheriting from an animated sprite class, based on the assumption you'll be the only one who had to extend/understand/maintain the code. You cannot, honestly, believe your approach would be "best practices" in a mature, enterprise sized software system.


But what makes it a bad design choice? Why does it have to be bad design to bind body and soul into a singular object if that's how you view the world? I mean that's why classes were invented right? To make languages higher level. To give us the ability to describe logical entities as we see fit. If I view the an enemy AI and his body (sprite) as one, who's to say I'm wrong in binding them via inheritance?

If I view the an enemy AI and his body (sprite) as one, who's to say I'm wrong in binding them via inheritance?

The Single Responsibility Principle should be your first clue. Wrapping up graphical representation with logical representation in a single class is only going to lead to trouble down the road.

What happens when you need an enemy AI without a sprite? An enemy sprite without an AI? The same AI with a non-sprite representation? The same sprite rendering attached to a different type of AI?

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]


[quote name='No Style Guy' timestamp='1299093957' post='4781097'] You openly admit that you could make bad design choices, such as a player class inheriting from an animated sprite class, based on the assumption you'll be the only one who had to extend/understand/maintain the code. You cannot, honestly, believe your approach would be "best practices" in a mature, enterprise sized software system.


But what makes it a bad design choice? Why does it have to be bad design to bind body and soul into a singular object if that's how you view the world? I mean that's why classes were invented right? To make languages higher level. To give us the ability to describe logical entities as we see fit. If I view the an enemy AI and his body (sprite) as one, who's to say I'm wrong in binding them via inheritance?
[/quote]

By your logic, if i view a game as an object who is a singular object with attributes such as players and terrain and game mechanics and physics, then why not just make one God class that inherits everything from everybody and has no composition? Does that sound like fun to manage?

You're entitled to your view, and in your own one-man-team, you can even make it law, but at the end of the day, you're still relying on people to have the same view as you if you want your code to be easily maintained and extended.

Frankly, composition results in better re-use and maintainability than inheritance by all-but-forcing loose coupling and good cohesion. Inheritance results in less code written, but more tightly coupled, uncohesive code. These are facts, not opinions. The only question is which attributes are more important to you: maintainability, re-use, loose coupling, good cohesion, OR less code written?

This topic is closed to new replies.

Advertisement