What is the best alternative: define, enums or variables?

Asked

Viewed 455 times

10

Recently, in a project where I have to declare many counts, I came to this doubt. Which is the best option define's, enum's or constant variables?

At first I think to use enum is the best alternative by not polluting the code and working better with the auto-completion of the IDE, but what would be the pros and cons of each approach?

Example of the options I have:

enum Pin
{
    PIN_00 = 0x10,
    PIN_01 = 0x11,
    PIN_02 = 0x12,
    // ... muitos outros 
};

Or:

#define PIN_00 0x10
#define PIN_01 0x11
#define PIN_02 0x12
// ... outros defines

Or:

namespace Pin
{
     const int PIN_00 = 0x10;
     const int PIN_01 = 0x11;
     const int PIN_02 = 0x12;
     // ... Outros declarações
}
  • Better or worse at all do not exist. What are the pros and cons of each approach, however, is something that can be answered.

  • I prefer to avoid define and Num because compilers cannot always handle this type of information when displaying values during a debug mode execution.

2 answers

10


If all counts are related and you want to give cohesion to them in the form of a type, enum is the best choice because they don’t allow you to mistakenly assign a wrong value to them.

Pin pin1 = PIN_00; //Ok
Pin pin2 = 0x10;   //Não compila

Now if the purpose is just to name one magic number, there is controversy. Personally I prefer const than #define, because the variables const are handled by the same compiler. You have an identifier, with well defined type and value. Already the define is handled by the preprocessor and may generate unexpected results in some cases. For example:

const int CONST = 2 + 5;
#define DEFINE 2 + 5

int x = 3 * CONST;  //Resultado = 3 * (2 + 5)
int y = 3 * DEFINE; //Resultado = 3 * 2 + 5 !!!

Another problem is that the guy from define is only defined by the literal, which can also generate problems:

#define FATOR 1.5

double val = FATOR / 2;

If ever someone changes the value of FATOR for 1 (instead of 1.0) that division becomes an entire division, the result of which is 0, instead of the 0.5 expected. This problem does not happen if FATOR for const double.

2

There are several pros and cons of each approach depending on the context, with respect to memory consumption and performance I believe that Enum and the constant variable are the same since Enum is like a more strongly typed variable. Already defines it does not occupy memory since it is evaluated by the preprocessor that replaces the chunk in which it appears by its definition.

  • 2

    The impact of memory and performance is zero for all alternatives. In the case of the variable, it will take up space in memory only if you try to create a pointer for it (in other cases this is not possible).

  • @Guilhermebernal In the case of the variable, if I use this approach, will the compiler treat the variable as a literal? Or will it store the variable in memory? I don’t quite understand this.

  • 3

    @Lucas if it is a global variable you must do static const. Being local only the const enough. In this case and if you never create a pointer to the variable, the optimization is trivial and any compiler will apply "inline" to the variable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.