Oct-19-2020, 02:49 PM
Hi guys,
in another topic, I shared my first Python project for feedback: My first Python project - Snake game
In case you don't want to check out that other thread, here's the direct link to the repo: https://github.com/bearek/snake
I got quite a few years of programming experience, but class design is still something I don't feel fully comfortable at. I never know how much dependency or flexibility is too much.
I think that Snake is a perfect case for class design practice. Currently, my game doesn't have a Snake class at all. I'd like to create one to separate the snake's logic from the rest. I'm still not sure what should my snake do itself and what it shouldn't do. Let's take a relationship between Snake and Grid (level) as an example. My Grid is just a 2D grid of X×Y size. Here are the three ideas how to implement the Snake class:
1. No information about the grid whatsoever. My Snake would just be a List of directions, which it actually is.
2. Giving the width and height to the Snake class to store the snake's points. That would make my snake know when it wraps around the edge of the grid. It'd also know when it dies, because without it, the snake doesn't know when it wraps long enough to bite its tail.
3. Just using Grid class as a dependency.
I don't have that sense of "the cleanest" solution when it comes to classes. Looks like idea 1. makes the snake the most reusable, but using it would be a bit troublesome. Idea 3 ties Snake and Grid together, so my Snake is no longer useful in any context without the Grid.
Is there some set of questions that I could ask myself to feel what's the cleanest solution? Are all of the above 3 solutions equally good? I'd love to have that skill of being sure I design great classes.
Thanks in advance for your feedback!
in another topic, I shared my first Python project for feedback: My first Python project - Snake game
In case you don't want to check out that other thread, here's the direct link to the repo: https://github.com/bearek/snake
I got quite a few years of programming experience, but class design is still something I don't feel fully comfortable at. I never know how much dependency or flexibility is too much.
I think that Snake is a perfect case for class design practice. Currently, my game doesn't have a Snake class at all. I'd like to create one to separate the snake's logic from the rest. I'm still not sure what should my snake do itself and what it shouldn't do. Let's take a relationship between Snake and Grid (level) as an example. My Grid is just a 2D grid of X×Y size. Here are the three ideas how to implement the Snake class:
1. No information about the grid whatsoever. My Snake would just be a List of directions, which it actually is.
2. Giving the width and height to the Snake class to store the snake's points. That would make my snake know when it wraps around the edge of the grid. It'd also know when it dies, because without it, the snake doesn't know when it wraps long enough to bite its tail.
3. Just using Grid class as a dependency.
I don't have that sense of "the cleanest" solution when it comes to classes. Looks like idea 1. makes the snake the most reusable, but using it would be a bit troublesome. Idea 3 ties Snake and Grid together, so my Snake is no longer useful in any context without the Grid.
Is there some set of questions that I could ask myself to feel what's the cleanest solution? Are all of the above 3 solutions equally good? I'd love to have that skill of being sure I design great classes.
Thanks in advance for your feedback!