[C/C++] Using static variable in large-size-file.

There is several important reason trying to avoid using global variable. One of them is “Tracking usage is very difficult.”. It is extremely difficult for developer to know enough when this variable is used and changed.
It is also true for static variable in C and member variable in C++, JAVA and so on. Let’s focus on the case of static variable in C (the case of member variable is also same.).
In simple and short file (ex. less than 500 lines), it’s not matter. But, static variable in large-size-file is also difficult to track. Yes, we should keep file small. But in practice, this case is not rare. Whenever we use static variable in large-size-file, we should take care of it. Especially, using flag and state is top of that. In many cases, flag and state are used to branch. So, tracking these values are very important. Let’s see following example.

static int _b_ui_mode = FALSE;
static int _operational_mode = IDLE;
funcA ()
  _b_ui_mode = TRUE;
  _operational_mode = USER_INPUT;
  if(_operational_mode == IDLE){

This is bad example. Assigning value to the static variable directly – especially to the flag and state variable – is the main reason to make tracking software difficlut. At least we should use get()/set() function.; Even in worst case, this makes logging easy. This is minimal requirement to use static variable in large-size-file.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s