2

I am wanting to use a bool to switch back and forth between loading X and loading Y from a file. I don't want to use "true" and "false", because it doesn't make the code clear. I would rather use something like LOAD_X or LOAD_Y... Is the following code the way to do it? Or is there a better way?

#define LOAD_X true
#define LOAD_Y false

Edit: Okay, so it seems an enum is the way to go... but what should the naming scheme be? Like all caps, or lower case for the first word, uppercase for following words, etc.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Just wait until someone comes along and writes `if(LOAD_Y)`... – Anon. Jan 07 '11 at 02:05
  • @Anon Why would anyone do that? – Lightness Races in Orbit Jan 07 '11 at 02:05
  • @Tomalak: Probably the same reason anyone would ever leak memory or double-free something. Making it easy to screw up (and making it *actually look pretty correct* when it's screwed up) is a terrible thing. – Anon. Jan 07 '11 at 02:06
  • 2
    This kind of situation is exactly what enums were intended for. – Michael Burr Jan 07 '11 at 02:08
  • @Anon: doesn't matter much if `LOAD_X`/`LOAD_Y` are aliases for `true`/`false`, number, or enums - in any of these cases they could be abused just as easily in an `if` condition. – Michael Burr Jan 07 '11 at 02:10
  • @Michael: The difference is that `if(SomeEnum)` is quite obviously wrong. Whereas the difference between `if(COMPILE_TIME_CONSTANT)` and `if(MACRO_MASQUERADING_AS_AN_ENUM)` is non-existent when just looking at the code in question. – Anon. Jan 07 '11 at 02:16
  • 1
    @Jay: Concerning naming, name the enumeration and its enumerators whatever you'd like. Use capital letters, don't use capital letters, whatever. However you choose to name them, just be consistent in your code. – James McNellis Jan 07 '11 at 02:19

4 Answers4

7

You can use an enum:

enum LoadType {
    LoadY,
    LoadX
};

Or, you might prefer to constrain the scope of the enumerators by using a namespace:

namespace LoadType {
    enum Type {
        LoadY,
        LoadX
    };
};

The advantage of using an enum is that if your function takes a LoadType (or a LoadType::Type in the second example), you can't pass it any arbitrary integer or bool; you can only pass it one of the enumerators (or something explicitly cast to the enumeration type, which is really easy to spot in a code review).

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • "you can't pass it any arbitrary integer or bool; you can only pass it one of the enumerators" Not in C++. In C++, an integer will be implicitly converted to a plain enum. However, as of C++11, scoped enumerations are now available which _are_ typesafe. – Pharap Aug 19 '19 at 15:42
4

I would use an enum instead. Just because there are two choices, doesn't mean the type should be bool

enum load_type { loadX, loadY };
Lou Franco
  • 87,846
  • 14
  • 132
  • 192
3

I guess that works, if you'll only ever have two options. I'd be tempted to go for an enum:

enum LOADMODE {
    LOAD_X,
    LOAD_Y
};

At the very least, prefer constants over macros:

const bool LOAD_X = true;
const bool LOAD_Y = false;

They will abide by scope rules and won't silently break stuff without you realising when names conflict.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 1
    I enjoy enums too much, but at least in C++98 I think I would go with the constants, if only because enum value names are available in whatever namespace the enum is in. Fortunately C++0x introduces the `enum class` which, among other things, doesn't do so, making something like `enum class load { X, Y };` possible with `X` and `Y` unable to conflict with anything. – fow Jan 07 '11 at 02:09
  • @fow: You can easily wrap an enumeration in a namespace or class to constrain the scope of the enumerators. – James McNellis Jan 07 '11 at 02:12
  • 2
    @fow The constants are available in whatever namespace they're defined in, too. – Lightness Races in Orbit Jan 07 '11 at 02:12
  • what if you define either of these in a class? it would stay in the class only, right? –  Jan 07 '11 at 02:14
  • 1
    Right. I just wanted to mention `enum class` because, like I said, I enjoy enums too much. :) – fow Jan 07 '11 at 02:17
  • @fow: There are medical professionals that can help you with that. :P – Lightness Races in Orbit Jan 07 '11 at 02:18
-1

i think Load X IS LOAD. Lowercase font look more friendly, you can choose bright colors. enum LoadType { LoadY, LoadX it is all right.

luckywong
  • 1
  • 3