#define, if/else, static vs global variables and functions

 

Quick couple of questions, hoping someone can help.

If I did this

#define NoBox 0
#define BoxForming 1
#define ActiveBox 2
#define ActiveTradePattern 3

int GetBoxCondition;

int somefunc() 
  { 
   if (A==B) GetBoxCondition = NoBox;
   else if (A==C) GetBoxCondition = BoxForming ;
   else if (A==D) GetBoxCondition = ActiveBox ;
   else if (A==E) GetBoxCondition = ActiveTradePattern ;
  }


Would this work?

switch(GetBoxCondition)
  {
   case NoBox:  RunNoBoxFunction();break;
   case BoxForming:  RunBoxFormingFunction();break;
   case ActiveBox :  RunActiveBox Function();break;
   case ActiveTradePattern:  RunActiveTradePatternFunction();break;
  }


Which leads to the next question, is the "switch" call better than an "if, else if" call such as this

if (GetBoxCondition == NoBox)  RunNoBoxFunction();
else if (GetBoxCondition == BoxForming)  RunBoxFormingFunction();
else if (GetBoxCondition == ActiveBox)  RunActiveBox Function();
else if (GetBoxCondition == ActiveTradePattern)  RunActiveTradePatternFunction();
 


Which leads to a query on functions. Is one better "embedding" these function calls into the program or would it be better to define the function inside the block such as this?


if (GetBoxCondition == NoBox) 
  {
   //--- Run some code here
  } 
else if (GetBoxCondition == BoxForming) 
  {
   //--- Run some code here
  } 
else if (GetBoxCondition == ActiveBox)  
  {
   //--- Run some code here
  }
else if (GetBoxCondition == ActiveTradePattern)
  {
   //--- Run some code here
  }


Final question is in relationship to static variables vs global variable. It's fairly simple. If one declares a global variable but that variable is only used inside one block, is one better off declaring it as a static variable?


Now to give a bit of content to why I ask these questions. I'm a very amateur programmer with sufficient skills to develop "simple"bots for back testing. However recently, the bots I build lately seem to slow strategy tester down with each pass made. This is very frustrating. After some research the answer is probably reflective of my programming skills and understanding of program structure . Therefor I want to re-engineer my bots but do things for the right reasons, not for the wrong. So I hope your answers take this into account.


Many thanks in advance

 
Robert Browne:
If I did this

Would this work?

Which leads to the next question, is the "switch" call better than an "if, else if" call such as this

Which leads to a query on functions. Is one better "embedding" these function calls into the program or would it be better to define the function inside the block such as this?

Final question is in relationship to static variables vs global variable. It's fairly simple. If one declares a global variable but that variable is only used inside one block, is one better off declaring it as a static variable?

Now to give a bit of content to why I ask these questions.

  1. No question asked.
  2. Yes.
  3. Yes, cleaner and only one test.
  4. One or two lines, just put the code there, otherwise create functions.
  5. Yes. global variables have to be global because they use in multiple functions, prefer static. If the only reason it has to be global is initialization, I use this patten
    int OnInit(){
       if(!OnInitXXX()) return INIT_FAILED;
       :
    }
    :
    type XXX; bool OnInitXXX(void){ XXX=...; return true; }
    void UseXXX(void){
       if(XXX) // Would be static except for initialization. 
    Or I would make a class.
  6. No question asked.
Don't use constants when you mean an enumeration
#define NoBox 0
#define BoxForming 1
#define ActiveBox 2
#define ActiveTradePattern 3

int GetBoxCondition;

int somefunc() 
  { 
   if (A==B) GetBoxCondition = NoBox;
Use verbs in function names, nouns in variable names. GetBoxCondition sounds like a operation but you can't get anything using that.
enum BoxCondition { NoBox, BoxForming, ActiveBox, ActiveTradePattern };

BoxCondition boxCondition;

int somefunc() 
  { 
   if (A==B) boxCondition = NoBox;

 
whroeder1:
  1. No question asked.
  2. Yes.
  3. Yes, cleaner and only one test.
  4. One or two lines, just put the code there, otherwise create functions.
  5. Yes. Global variables have to be global because they use in multiple functions.
  6. No question asked.
Don't use constants when you mean an enumeration



Many thanks for such a prompt responce. Looks like time to research enumerations. Busy weekend ahead. Thanks again.

 
Robert Browne:

Many thanks for such a prompt responce. Looks like time to research enumerations. Busy weekend ahead. Thanks again.

In relation to this expression

enum BoxCondition { NoBox, BoxForming, ActiveBox, ActiveTradePattern };

BoxCondition boxCondition;

int somefunc() 
  { 
   if (A==B) boxCondition = NoBox;

Been doing my homework and have find some examples where the author would of made the same expression but like this

enum BoxCondition { NoBox, BoxForming, ActiveBox, ActiveTradePattern } boxCondition ;

int somefunc() 
  { 
   if (A==B) boxCondition = NoBox;

or 

enum BoxCondition { NoBox, BoxForming, ActiveBox, ActiveTradePattern };

enum BoxCondition boxCondition;

int somefunc() 
  { 
   if (A==B) boxCondition = NoBox;


would there be a reason for them doing so? Are they right/wrong for doing so?

Reason: