Reputation: 161
I have a lot of global variables whose name is very long, e.g:
var1.var2.var3.var4.myGlobalVar1[N]
And I must use this variables in my function. My question is what is the best way to manipulate this variable? is it necessary to do a copy into a local variable to increase the readability?
Upvotes: 3
Views: 161
Reputation: 9814
if you only want to increase the readability, you can use
#define SHORTER_NAME var1.var2.var3.var4.myGlobalVar1[N]
if it is an array you can use
#define SHORTER_NAME var1.var2.var3.var4.myGlobalVar1
to access each value with SHORTER_NAME[index]
Upvotes: 4
Reputation: 215245
The best way to increase readability is indeed to create a local variable. This local variable will get optimized away in the final executable, so it won't affect the program's performance. The local variable can either be a hard copy of the struct member, or a pointer to it, depending on what's most convenient from case to case.
You can also use macros in various ways, but I don't recommend it, since they clutter down the global namespace.
If you still insist in using macros for whatever reason, then you should do like this:
#define SHORT var1.var2.var3.var4.myGlobalVar1
...
SHORT[i] = something;
You should not use function-like macros in this case. They aren't type safe, they are hard to read, hard to debug and hard to maintain. Macros in general are dangerous and bad practice.
Upvotes: 1