user33435: I'm relatively new at programming and many of the coding best practices I'm reading effectively state that there are very few good reasons to use a global variable (or that the best code has no globals at all).
I've done my best to keep this in mind, when writing software to make an Arduino interface with an SD card, talk to a computer and run a motor controller.
I currently have 46 globals for about 1100 lines of "beginner level," code (no line having more than one action). Is this a good ratio or should I look at reducing it more? Also what practices can I employ to reduce the number of globals further?
I'm asking this here because I'm specifically concerned with best practices for coding on Arduino products rather than computer programming in general.

user3508: Global variables are never evil. A general rule against them is just a crutch to let you survive long enough to gain the experience to make better decisions.
What a global variable is, is an inherent assumption that there is only one of a thing (it doesn't matter if we're talking about a global array or map that might contain multiple things, that still contains the assumption that there is only one such list or mapping, and not multiple independent ones).
So before you make use of a global, you want to ask yourself: is it conceivable that I will ever want to use more than one of this thing? If it turns out to be true down the line, you will have to modify the code to un-globalize that thing, and you'll probably find out along the way that other parts of your code depend on that assumption of uniqueness, so you'll have to fix them as well, and the process becomes tedious and error-prone. "Don't use globals" is taught because usually it's a pretty small cost to avoid globals from the beginning, and it avoids the potential of having to pay a large cost later on.
But the simplifying assumptions that globals allow also make your code smaller, faster, and use less memory, because it doesn't have to pass around a notion of which thing it's using, doesn't have to do indirection, doesn't have to consider the possibility of the desired thing maybe not existing, etc. On embedded you're more likely to be constrained for code size and/or CPU time and/or memory than you are on a PC, so these savings can matter. And many embedded applications also have more rigidity in the requirements — you know that your chip only has one of a certain peripheral, the user can't just plug another one into a USB port or something.
Another common reason for wanting more than one of something that seems unique is testing — testing the interaction between two components is easier when you can just pass a testing instance of some component to a function, whereas trying to modify the behavior of a global component is a trickier proposition. But testing in the embedded world tends to be very different from elsewhere, so this may not apply to you. As far as I know, Arduino has no test culture whatsoever. 
So go ahead and use globals when they seem worthwhile. The code police won't come and get you. Just know that the wrong choice could lead to a lot more work for you down the road, so if you're not sure...

user24459: As with everything (other than gotos which are truly evil) globals have their place.
e.g. If you have a debug serial port or a log file that you need be to able to write to from everywhere then it often makes sense to make it global. Similarly if you have some critical piece of system status information then making it global is often the easiest solution. There is no point in having a value that you have to pass to every single function in the program.
As others have said 46 seems a lot for only just over 1000 lines of code but without knowing the details of what you are doing it's hard to say if you are over using them or not.
However for every global ask yourself a few important questions:
Is the name clear and specific so that I don't accidentally try to use the same name somewhere else? If not change the name.
Does this need to ever change? If not then consider making it a const.
Does this need to be visible everywhere or is it only global so that the value is maintained between function calls? Is so consider making it local to the function and using the keyword static.
Will things really screw up badly if this gets changed by a piece of code when I'm not being careful? e.g. if you have two related variables, say name and ID number, that are global (see previous note on using global when you need some information almost everywhere) then if one of them gets changed without the other nasty things could happen. Yes you could just be careful but sometimes it's good to enforce carefulness a little. e.g put them in a different .c file and then define functions that set both of them at the same time and allow you to read them. You then only include the functions in the associated header file, that way the rest of your code can only access the variables through the defined functions and so can't mess things up. This is basically a pure c way of doing what a c++ class would allow but without having to learn how to define and create classes so you aren't learning too much at once.
-- update -- I just realized that you had asked about Arduino specific best practice rather than general coding and this is more of a general coding reply. But honestly there isn't much difference, good practice is good practice. The startup() and loop() structure of Arduino means that you have to use globals a little more than other platforms in some situations but that doesn't really change much, you always end up aiming for the best you can do within the limitations of the platform no matter what the platform is.

user31105: Although I wouldn't use them when programming for a PC, for the Arduino they have some benefits. Most if it has already been said:

No dynamic memory usage (creating gaps in the limited heap space of an Arduino)
Available everywhere, thus no need to pass them as arguments (which costs stack space)

Also, in some cases, especially performance-wise it can be good to use global variables, instead of creating elements when needed:

To decrease gaps in heap space
Dynamically allocating and/or freeing memory can take substantial time
Variables can be 'reused', like a list of elements of a global list that are used for multiple reasons, e.g. once as a buffer for the SD, and later as a temporary string buffer.


user35436: Are they evil? Maybe. The problem with globals is that they may be accessed and modified at any point in time by any function or piece of code being executed, without restrictions. This may lead to situations that are, let's say, difficult to trace back and explain. Minimizing the amount of globals, if possible bringing the amount back to zero, is therefore desirable.
Can they be avoided? Almost always yes. The problem with Arduino is, that they force you into this two function approach in which they assume you setup() and you loop(). In this particular case you have no access to the scope of the caller function of these two functions (probably main()). If you had, you would be able to rid yourself of all globals and use locals instead.
Picture the following:
int main() {
  setup();

  while (true) {
    loop();
  }
  return 0;
}

This is probably more or less what the main function of an Arduino program looks like. Variables you need in both the setup() and the loop() function would then preferably be declared inside the scope of the main() function rather than the global scope. They could then be made accessible to the two other functions by means of passing them as arguments (using pointers if necessary).
For example:
int main() {
  int myVariable = 0;
  setup(&myVariable);

  while (true) {
    loop(&myVariable);
  }
  return 0;
}

Note that in this case you also need to change the signature of both functions.
As this might not be feasible nor desirable, I see really only one way to remove most globals from an Arduino program without modifying the forced upon program structure.
If I recall correctly, you're perfectly able to use C++ while programming for Arduino, rather than C. If you're not familiar (yet) with OOP (Object Oriented Programming) or C++, it might take some getting used to and some reading.
My proposal would be to create a Program class and create a single global instance of this class. A class should be considered the blueprint for objects.
Consider the following example program:
class Program {
public:      
  Program();

  void setup();
  void loop();

private:
  int myFirstSampleVariable;
  int mySecondSampleVariable;
};

Program::Program() :
  myFirstSampleVariable(0),
  mySecondSampleVariable(0)
{

}

void Program::setup() {
  // your setup code goes here
}

void Program::loop() {
  // your loop code goes here
}

Program program; // your single global

void setup() {
  program.setup();
}

void loop() {
  program.loop();
}

Voilà, we've rid ourselves of almost all globals. The functions in which you would start adding your application logic would be the Program::setup() and Program::loop() functions. These functions have access to the instance specific member variables myFirstSampleVariable and mySecondSampleVariable whereas the traditional setup() and loop() functions do not have access as these variables have been marked class private. This concept is called data encapsulation or data hiding.
Teaching you OOP and/or C++ is a bit out of scope of the answer to this question so I'll stop here.
To summarize: globals should be avoided and it's almost always possible to drastically reduce the amount of globals. Also when you're programming for Arduino.
Most importantly I hope my answer is somewhat useful to you :)

user31745: The main issue with global variables is code maintenance. When reading a line of code, it is easy to find declaration of variables passed as parameter or declared locally. It is not so easy to find declaration of global variables (often it requires and IDE).
When you have many global variables (40 is already a lot), it becomes difficult to have an explicit name that is not too long. Using namespace is a way to clarify role of global variables.
A poor way to mimic namespaces in C is:
static struct {
    int motor1, motor2;
    bool sensor;
} arm;

On intel or arm processor, access to global variables is slower than other variables. It is probably the opposite on arduino.

user7508: It's very difficult to give a definitive answer without seeing your
actual code.
Global variables are not evil, and they often make sense in an embedded
environment where you typically do a lot of hardware access. You have
only four UARTS, only one I2C port, etc. So it makes sense to use
globals for variables tied to specific hardware resources. And indeed,
the Arduino core library does that: Serial, Serial1, etc. are global
variables. Also, a variable representing the global state of the program
is typically a global.

I currently have 46 globals for about 1100 lines of [code]. Is this a
  good ratio [...]

Is not about the numbers. The right question you should ask yourself is,
for each of these globals, whether it makes sense to have it in global
scope.
Still, 46 global seems a bit high to me. If some of these hold constant
values, qualify them as const: the compiler will usually optimize out
their storage. If any of those variables is only used inside a single
function, make it local. If you want its value to persist between calls
to the function, qualify it as static. You could also reduce the
number of “visible” globals by grouping variables together inside an
class, and having one global instance of this class. But only do so when
it makes sense to put stuff together. Making a big GlobalStuff class
for the sake of having only one global variable will not help make your
code clearer.

user4143: They aren't evil per se, but they do tend to get overused where there is no good reason to use them.
There are plenty of times when global variables are advantageous. Especially since programming an Arduino is, under the hood, vastly different to programming a PC.
The biggest benefit to global variables is static allocation. Especially with large and complex variables such as class instances. Dynamic allocation (the use of new etc) is frowned upon due to lack of resources.
Also you don't get a single call tree like you do in a normal C program (single main() function calling other functions) - instead you effectively get two separate trees (setup() calling functions, then loop() calling functions), which means that sometimes global variables are the only way to achieve your goal (i.e., if you want to use it in both setup() and loop()).
So no, they're not evil, and on an Arduino they have more and better uses than on a PC.

33435: <p>I'm relatively new at programming and many of the coding best practices I'm reading effectively state that there are very few good reasons to use a global variable (or that the best code has no globals at all).</p>

<p>I've done my best to keep this in mind, when writing software to make an Arduino interface with an SD card, talk to a computer and run a motor controller.</p>

<p>I currently have 46 globals for about 1100 lines of "beginner level," code (no line having more than one action). Is this a good ratio or should I look at reducing it more? Also what practices can I employ to reduce the number of globals further?</p>

<p>I'm asking this here because I'm specifically concerned with best practices for coding on Arduino products rather than computer programming in general.</p>
 3508: <p>Global variables are never <em>evil</em>. A general rule against them is just a crutch to let you survive long enough to gain the experience to make better decisions.</p>

<p>What a global variable is, is an inherent assumption that there is only one of a thing (it doesn't matter if we're talking about a global array or map that might contain multiple things, that still contains the assumption that there is only <em>one</em> such list or mapping, and not multiple independent ones).</p>

<p>So before you make use of a global, you want to ask yourself: is it conceivable that I will ever want to use more than one of this thing? If it turns out to be true down the line, you will have to modify the code to un-globalize that thing, and you'll probably find out along the way that other parts of your code depend on that assumption of uniqueness, so you'll have to fix them as well, and the process becomes tedious and error-prone. "Don't use globals" is taught because usually it's a pretty small cost to avoid globals from the beginning, and it avoids the potential of having to pay a large cost later on.</p>

<p>But the simplifying assumptions that globals allow also make your code smaller, faster, and use less memory, because it doesn't have to pass around a notion of which thing it's using, doesn't have to do indirection, doesn't have to consider the possibility of the desired thing maybe not existing, etc. On embedded you're more likely to be constrained for code size and/or CPU time and/or memory than you are on a PC, so these savings can matter. And many embedded applications also have more rigidity in the requirements — you <em>know</em> that your chip only has one of a certain peripheral, the user can't just plug another one into a USB port or something.</p>

<p>Another common reason for wanting more than one of something that seems unique is testing — testing the interaction between two components is easier when you can just pass a testing instance of some component to a function, whereas trying to modify the behavior of a global component is a trickier proposition. But testing in the embedded world tends to be very different from elsewhere, so this may not apply to you. As far as I know, Arduino has no test culture whatsoever. </p>

<p>So go ahead and use globals when they seem worthwhile. The code police won't come and get you. Just know that the wrong choice could lead to a lot more work for you down the road, so if you're not sure...</p>
 24459: <p>As with everything (other than gotos which are truly evil) globals have their place.</p>

<p>e.g. If you have a debug serial port or a log file that you need be to able to write to from everywhere then it often makes sense to make it global. Similarly if you have some critical piece of system status information then making it global is often the easiest solution. There is no point in having a value that you have to pass to every single function in the program.</p>

<p>As others have said 46 seems a lot for only just over 1000 lines of code but without knowing the details of what you are doing it's hard to say if you are over using them or not.</p>

<p>However for every global ask yourself a few important questions:</p>

<p>Is the name clear and specific so that I don't accidentally try to use the same name somewhere else? If not change the name.</p>

<p>Does this need to ever change? If not then consider making it a const.</p>

<p>Does this need to be visible everywhere or is it only global so that the value is maintained between function calls? Is so consider making it local to the function and using the keyword static.</p>

<p>Will things really screw up badly if this gets changed by a piece of code when I'm not being careful? e.g. if you have two related variables, say name and ID number, that are global (see previous note on using global when you need some information almost everywhere) then if one of them gets changed without the other nasty things could happen. Yes you could just be careful but sometimes it's good to enforce carefulness a little. e.g put them in a different .c file and then define functions that set both of them at the same time and allow you to read them. You then only include the functions in the associated header file, that way the rest of your code can only access the variables through the defined functions and so can't mess things up. This is basically a pure c way of doing what a c++ class would allow but without having to learn how to define and create classes so you aren't learning too much at once.</p>

<p>-- update -- I just realized that you had asked about Arduino specific best practice rather than general coding and this is more of a general coding reply. But honestly there isn't much difference, good practice is good practice. The <code>startup()</code> and <code>loop()</code> structure of Arduino means that you have to use globals a little more than other platforms in some situations but that doesn't really change much, you always end up aiming for the best you can do within the limitations of the platform no matter what the platform is.</p>
 31105: <p>Although I wouldn't use them when programming for a PC, for the Arduino they have some benefits. Most if it has already been said:</p>

<ul>
<li>No dynamic memory usage (creating gaps in the limited heap space of an Arduino)</li>
<li>Available everywhere, thus no need to pass them as arguments (which costs stack space)</li>
</ul>

<p>Also, in some cases, especially performance-wise it can be good to use global variables, instead of creating elements when needed:</p>

<ul>
<li>To decrease gaps in heap space</li>
<li>Dynamically allocating and/or freeing memory can take substantial time</li>
<li>Variables can be 'reused', like a list of elements of a global list that are used for multiple reasons, e.g. once as a buffer for the SD, and later as a temporary string buffer.</li>
</ul>
 35436: <p>Are they evil? Maybe. The problem with globals is that they may be accessed and modified at any point in time by any function or piece of code being executed, without restrictions. This may lead to situations that are, let's say, difficult to trace back and explain. Minimizing the amount of globals, if possible bringing the amount back to zero, is therefore desirable.</p>

<p>Can they be avoided? Almost always yes. The problem with Arduino is, that they force you into this two function approach in which they assume you <code>setup()</code> and you <code>loop()</code>. In this particular case you have no access to the scope of the caller function of these two functions (probably <code>main()</code>). If you had, you would be able to rid yourself of all globals and use locals instead.</p>

<p>Picture the following:</p>

<pre><code>int main() {
  setup();

  while (true) {
    loop();
  }
  return 0;
}
</code></pre>

<p>This is probably more or less what the main function of an Arduino program looks like. Variables you need in both the <code>setup()</code> and the <code>loop()</code> function would then preferably be declared inside the scope of the <code>main()</code> function rather than the global scope. They could then be made accessible to the two other functions by means of passing them as arguments (using pointers if necessary).</p>

<p>For example:</p>

<pre><code>int main() {
  int myVariable = 0;
  setup(&amp;myVariable);

  while (true) {
    loop(&amp;myVariable);
  }
  return 0;
}
</code></pre>

<p>Note that in this case you also need to change the signature of both functions.</p>

<p>As this might not be feasible nor desirable, I see really only one way to remove most globals from an Arduino program without modifying the forced upon program structure.</p>

<p>If I recall correctly, you're perfectly able to use C++ while programming for Arduino, rather than C. If you're not familiar (yet) with <a href="https://en.wikipedia.org/wiki/Object-oriented_programming" rel="nofollow noreferrer">OOP (Object Oriented Programming)</a> or C++, it might take some getting used to and some reading.</p>

<p>My proposal would be to create a Program class and create a single global instance of this class. A class should be considered the blueprint for objects.</p>

<p>Consider the following example program:</p>

<pre><code>class Program {
public:      
  Program();

  void setup();
  void loop();

private:
  int myFirstSampleVariable;
  int mySecondSampleVariable;
};

Program::Program() :
  myFirstSampleVariable(0),
  mySecondSampleVariable(0)
{

}

void Program::setup() {
  // your setup code goes here
}

void Program::loop() {
  // your loop code goes here
}

Program program; // your single global

void setup() {
  program.setup();
}

void loop() {
  program.loop();
}
</code></pre>

<p>Voilà, we've rid ourselves of almost all globals. The functions in which you would start adding your application logic would be the <code>Program::setup()</code> and <code>Program::loop()</code> functions. These functions have access to the instance specific member variables <code>myFirstSampleVariable</code> and <code>mySecondSampleVariable</code> whereas the traditional <code>setup()</code> and <code>loop()</code> functions do not have access as these variables have been marked class private. This concept is called <a href="https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)" rel="nofollow noreferrer">data encapsulation</a> or data hiding.</p>

<p>Teaching you OOP and/or C++ is a bit out of scope of the answer to this question so I'll stop here.</p>

<p>To summarize: globals should be avoided and it's almost always possible to drastically reduce the amount of globals. Also when you're programming for Arduino.</p>

<p>Most importantly I hope my answer is somewhat useful to you :)</p>
 31745: <p>The main issue with global variables is code maintenance. When reading a line of code, it is easy to find declaration of variables passed as parameter or declared locally. It is not so easy to find declaration of global variables (often it requires and IDE).</p>

<p>When you have many global variables (40 is already a lot), it becomes difficult to have an explicit name that is not too long. Using namespace is a way to clarify role of global variables.</p>

<p>A poor way to mimic namespaces in C is:</p>

<pre><code>static struct {
    int motor1, motor2;
    bool sensor;
} arm;
</code></pre>

<p>On intel or arm processor, access to global variables is slower than other variables. It is probably the opposite on arduino.</p>
 7508: <p>It's very difficult to give a definitive answer without seeing your
actual code.</p>

<p>Global variables are not evil, and they often make sense in an embedded
environment where you typically do a lot of hardware access. You have
only four UARTS, only one I2C port, etc. So it makes sense to use
globals for variables tied to specific hardware resources. And indeed,
the Arduino core library does that: <code>Serial</code>, <code>Serial1</code>, etc. are global
variables. Also, a variable representing the global state of the program
is typically a global.</p>

<blockquote>
  <p>I currently have 46 globals for about 1100 lines of [code]. Is this a
  good ratio [...]</p>
</blockquote>

<p>Is not about the numbers. The right question you should ask yourself is,
for each of these globals, whether it makes sense to have it in global
scope.</p>

<p>Still, 46 global seems a bit high to me. If some of these hold constant
values, qualify them as <code>const</code>: the compiler will usually optimize out
their storage. If any of those variables is only used inside a single
function, make it local. If you want its value to persist between calls
to the function, qualify it as <code>static</code>. You could also reduce the
number of “visible” globals by grouping variables together inside an
class, and having one global instance of this class. But only do so when
it makes sense to put stuff together. Making a big <code>GlobalStuff</code> class
for the sake of having only one global variable will not help make your
code clearer.</p>
 4143: <p>They aren't <em>evil</em> per se, but they do tend to get overused where there is no good reason to use them.</p>

<p>There are plenty of times when global variables are advantageous. Especially since programming an Arduino is, under the hood, vastly different to programming a PC.</p>

<p>The biggest benefit to global variables is static allocation. Especially with large and complex variables such as class instances. Dynamic allocation (the use of <code>new</code> etc) is frowned upon due to lack of resources.</p>

<p>Also you don't get a single call tree like you do in a normal C program (single <code>main()</code> function calling other functions) - instead you effectively get two separate trees (<code>setup()</code> calling functions, then <code>loop()</code> calling functions), which means that sometimes global variables are the only way to achieve your goal (i.e., if you want to use it in both <code>setup()</code> and <code>loop()</code>).</p>

<p>So no, they're not evil, and on an Arduino they have more and better uses than on a PC.</p>
