XNA Creators Club Online
Page 1 of 1 (3 items)
Sort Posts: Previous Next

Choosing Primitive Types

Last post 7/2/2009 9:55 PM by BenC. 2 replies.
  • 6/28/2009 5:00 PM
    • (0)
    • premium membership
    • Posts 13

    Choosing Primitive Types

    Hey Guys, I have a simple question about choosing primitive types.

    Usually when I create a variable, I try to choose the smallest type possible that can handle the "range requirements" for that variable. So for example if I use a byte variable anywhere in my code I know for sure that the variable would never need to go beyond the range of [0 - 255]. I guess the philosophy here is... if you don't need it don't use / allocate it.

    Now that's all fine and dandy, but my problem comes in when I perform operations on those variables.. more specifically variables of type byte.

    Simple operations like (just an example)...

    byte var = 0;
    var = var + 10;

    ..forces me to cast the resulting value back into a byte.

    byte var = 0;
    var = byte(var + 10);

    My question is... how expensive do you think these conversions are and do you think the cost of the conversions are worth the reduction in memory usage?
  • 6/28/2009 5:36 PM In reply to

    Re: Choosing Primitive Types

    Why are you worried about memory usage? Unless you have a specific problem with memory somewhere, I wouldn't do this. Not for (most likely) insignificant performance vs memory trade-offs, but just because it'll make your code much harder to read when it's littered with casts. Plus each time you cast between these types you have to think if you're not truncating values and what behaviour you really want when that happens.

    It's better just to stick to standard types like ints in most cases and use specific types to solve specific problems. If you're making heavy use of a framework (like XNA), use the types that are predominantly used in that framework.
  • 7/2/2009 9:55 PM In reply to
    • (0)
    • premium membership
    • Posts 13

    Re: Choosing Primitive Types

    Cheers for that Vicent, I've settled with using the more or less default type int. You're right memory shouldn't really be an issue especially with the type of game I am working on (Platformer).
Page 1 of 1 (3 items) Previous Next