# Q. What's a good prefix for global variable?

Status
Not open for further replies.

#### Pommie

##### Well-Known Member
A. //

What do people think?

Mike.

#### rjenkinsgb

##### Well-Known Member
It's down to both end use of the software and style..

For team projects and business software, where there could be conflicts between use or unwanted visibility of data, globals are a real bad idea.

For real-time code, embedded, single-author games etc. those problems do not apply and using globals or static storage as appropriate is rather more efficient than unnecessarily passing data or pointers.

Also, some of the workarounds that people come up with to avoid actual globals just disguise them rather than eliminating them - eg. a shared object to allow common data such as status or config info to be accessed by multiple other code modules compiles to the same code as basic global variables would...

##### Well-Known Member
Pommie .. I read this thread several times and now I realise what a poor 'programmer' I must be... my 'C' if full to the brim with global variables.. Can't seem to do without them ! A prefix sounds like a good idea and will start using one ...

#### Pommie

##### Well-Known Member
I kinda understand the principal of "no globals" when it comes to large team projects. However, most of my projects will only have myself writing code so i'm more comfortable with globals. I do try to reduce my use as much as reasonable as they do eat ram but having function calls just to read/write a private variable eats up program space. Hence, when it comes to $\mu$controllers I tend to use them as needed.

Mike.
Edit, it seems using the character function under latex turns it into a complete paragraph rather than a character!!! I just wanted to write microcontroller with a mu character!!!

#### Pommie

##### Well-Known Member
You cheated and used charmap or something.

Go on, do it via the tools available.

Mike.
Edit, µ - see I can do it too. Alt 0181.

##### Well-Known Member
no #5 was just a "quote" from your post #4

#### Nigel Goodwin

##### Super Moderator
I kinda understand the principal of "no globals" when it comes to large team projects. However, most of my projects will only have myself writing code so i'm more comfortable with globals. I do try to reduce my use as much as reasonable as they do eat ram but having function calls just to read/write a private variable eats up program space. Hence, when it comes to $\mu$controllers I tend to use them as needed.
I agree, I love global variables - when you've not got 'unlimited' resources (such as in a PIC) then it makes absolute sense to use them.

For example, in my current GPRS project(s) I have a number of global variables that need to be accessed from various subroutines as they store the settings for the operation of the unit - such as the time and/or interval that it's triggered. Passing them as local variables to subroutines would be much more inefficient, and wasteful of resources.

As for globals 'eating ram', I would disagree, as they mean that you know what you're using, rather than hoping the compiler can handle them all in a reliable manner. Using globals means one copy of the variable, at a location that you know.

Obviously I do pass variables (or pointers to them), where it makes more sense to do so.

#### Pommie

##### Well-Known Member
no #5 was just a "quote" from your post #4
So why does my one symbol take multiple paragraphs?

Mike.

#### alec_t

##### Well-Known Member
Did you insert as 'inline' or 'block'?

#### MichaelaJoy

##### Active Member

(g underscore)

A quick look will allow you to realize that it's a global variable.

Recently, I spent some time programming in c#. No global variables allowed at all. Sometimes, they make things so easy.
Unless they're being shared by multiple threads, in which case you have to be sure to use a locking construct (mutex)

Microcontrollers are a different animal. You're not running more than one thread, so there's no risk.

But you have to be careful if the same global variable is being accessed from more than one interrupt.

(Break out the atomic locking gizmo... )

Last edited:

#### dknguyen

##### Well-Known Member

(g underscore)

A quick look will allow you to realize that it's a global variable.

Recently, I spent some time programming in c#. No global variables allowed at all. Sometimes, they make things so easy.
Unless they're being shared by multiple threads, in which case you have to be sure to use a locking construct (mutex)

Microcontrollers are a different animal. You're not running more than one thread, so there's no risk.

But you have to be careful if the same global variable is being accessed from more than one interrupt.

(Break out the atomic locking gizmo... )
But then if you need a g_string local variable you are screwed

#### atferrari

##### Well-Known Member
I would resort to
GLBL_

I personally prefer names not too cryptic, if at all possible, somewhat legible.

Jointly with good comments, helped me when revisiting old code.

#### gophert

##### Well-Known Member
The local university's style guide says, avoid them whenever possible but, if you cannot figure out how to avoid them, use prefix "g_" as in g_variable