Mr Flibble <
flibbleREM...@i42.co.uk> wrote:
> 1) Don't use the unsigned integral types despite the fact that the C++
> standard library is full of their use.
Years ago I used to follow the principle of using the type that most closely
matched the thing being represented. However, this bit me in the posterior
so many times that nowadays I always use signed integers unless there's a
good reason not to.
As an example, the width and height of, for example, a bitmap, or the screen,
cannot be negative. Negative values are nonsensical. Therefore the most
logical type to represent width and height is unsigned.
However, when talking about screen (or bitmap) coordinates, signed values
are usually a necessity. You could quite well draw a sprite or a line
partially outside the screen, which necessitates negative values. Therefore,
if using integrals to represent these pixel coordinates, int is the most
sensible value.
But since you will be using the width and height of the drawing area all
the time for this purpose, you will now be mixing signed and unsigned
integers, which often result in unexpected implicit casts, which often
result in wrong values. (For example, if making floating point calculations
using these variables, you may inadvertently end up with values of over
4 billion instead of some small negative values.)
There are two possible solutions to avoid this problem: Either always
explicitly cast the width and height to signed when using them, or just
do the sensible thing and use signed integrals for the width and height,
even if those variables can never themselves be negative.
And this is just one example of many.
> 2) Don't use abstract interfaces (as they advocate against using public
> virtual functions).
I don't even understand what this is trying to say.
> 3) Never derive from standard containers despite the fact that interface
> augmentation is useful.
I have never found the need to.
(From a design perspective, composition rather than inheritance may often
be the cleaner solution because it means the public interface of your class
remains more minimalistic and focused on the particular task you are trying
to achieve, without all the extraneous baggage that comes from the container.)
--- news://
freenews.netfront.net/ - complaints:
ne...@netfront.net ---