Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 03 Nov 2012
Offline Last Active Nov 19 2012 09:16 PM

#4999956 What's your take on protected variables?

Posted by on 11 November 2012 - 11:24 AM

I believe the context here can't be too obscure. This is a video game dev site after all.

Hodgeman rightly mentioned the warning bells that emerge when "Circle" has to ignore either width or height. The warning bells are actually telling you that Circle doesn't have a width or a height, it has a radius or a diameter. You use the diameter to "calculate" the circle's width or height - except that the diameter is exactly equal to the height and is exactly equal to the width, so nobody bothers explicitly calculating it with circles. But just because a circle's width and height happen to be equal to the circle's diameter, that doesn't mean the circle's width and height are the diameter (or vise-versa). You'd actually be reusing the variable "width" or the variable "height" for an entirely different purpose that just happens to be equal, which wouldn't be good (in this situation, it's a very mild issue - but it's still not a good habit to get into).

No. As I see it, the Circle class will include all three: width, height and diameter. The width and height still serves the same purpose for Circle class as it did for Rectangle class. Now, the diameter variable (or maybe radius), will be used to calculate the area of the circle.

There's a more serious, but more subtle issue with re-using the Shape's width, height, x, and y. As I already mentioned, you'd actually be re-purposing either width or height for a different purpose. But how many people here realize you'd also be re-purposing x and y for a different purpose as well?
What is x and y? A coordinate on a Cartesian grid. Right? But in naming it 'x' and 'y' you are actually naming it what it is, instead of what it's for (Which is why it would be better off wrapped in a Point or Coord class). A variable's type should say what the variable is, but the variable's name should describe it's purpose.
What does 'x' mean to a Shape? 'x' does not describe the purpose, it only describes that it is a horizontal location in space. It could serve any purpose - the name isn't describing the purpose.
It's precisely because it's so generically named that you tricked yourself with your Shape class, and also trick future users of your class:

I don't agree with this. The width and height variables are raw data. Data should be used for whatever it needs to be used.
Of course, the 'x' variable could be named better, let's say 'x_coordinate', or even better, as you say, it could be a point. But my argument is that EVERY child of the Shape class will need a x variable and a y variable, or a point, etc.
It doesn't matter where the point will be located in the shape, the object stills need it.

The Circle class example is the 'whale' of the mammals class.

I must say, I have nothing against composition. I use it much in my code. Yet some things simply belong in a class. And I can't conceive how the child can't have access to the parents data when he is INHERING from that same class. If you permit to me make another example:

[source lang="cpp"]class Dog{private: Fangs fangs; //many other variables };class GermanShepherd : public Dog{}[/source]

So you are telling me that class GermanShepherd can't have access to Dog class's private variables?

Or are you going to argue that some dogs don't howl, so they don't fit together in a class. Should I compose GermanShepherd class from Dog class?

You are arguing against inheritance, not against protected variables!

inhere [ɪnˈhɪə]
vb (intr; foll by in) to be an inseparable part (of)

#4998571 Time to write rendering engine from scratch

Posted by on 07 November 2012 - 02:36 PM

Posted Image