Naming Conventions

I am looking at two sketches from the Examples. They use the same circuit (attachment) which led me to ask what else is the same in both. In the first sketch, the button is named pushButton. In the second sketch, the same button is named buttonState. Two different names, but are they different, or the same?
I tried changing the name of the button in the first sketch to the name used in the second sketch, upload it, send to serial monitor, and it works the same as before. I suppose the button name in the second sketch could be changed to the name originally given in the first sketch, as well.
Can I infer by this simple instance that naming conventions work like this in general? It appears to me that there are three main parts of a sketch: the naming section, the setup, and the function, or functions. Whatever name is given to, for instance, a pin, in the naming section, is read in the rest of the sketch by the board as referring to the same thing.
I realize this is elementary. If I were still in school I would enroll in the course. As it is, I have to figure it out as I go. Any comments expanding on this simple definition are appreciated.

In general, names are COMPLETELY arbitrary. Any string of letter, digit, and underscore characters is a valid 'identifier' (variable or function name) as long as it doesn't start with a digit. A 'naming convention' is just that: a convention. A set of rules that only exist to make understanding your code easier.

I like something like:

ALL_UPPER_CASE_WITH_UNDERSCORES for global constants. CamelCaseWithInitialCapital for global functions and variables. camelCaseWithInitialLowercase for local variables and class members (both functions and variables).

Other people follow other conventions.

When naming Arduino pins I try to use 'const byte ALL_UPPER_CASE_WITH_UNDERSCORES_AND_ENDING_IN_PIN' which makes it clear that the constant represents a pin number. If several pin numbers are in a const byte array the name ends in '_PINS'.

1 Like

brianeo: In the first sketch, the button is named pushButton. In the second sketch, the same button is named buttonState.

Which examples are you referring to?

I use ALL_UPPER_CASE_WITH_UNDERSCORES only for macros. The are very inconvenient to type, especially since I have disabled the caps lock button on my keyboard so I wouldn't like that convention for global constants, which I use a ton of to avoid magic numbers.

Thanks for the definitions. I will print this thread out as soon as I have access to a printer. I realized after sending the o.p. that the Example references would help. One is 01.BASICS DigitalReadSerial, the other is 02.DIGITAL Button. Now, I realize one sends an on/off signal to the serial monitor when the button is pressed, the other turns the LED on/off. What has me speculating is that "state" as in buttonState, defines a simple on/off response, as in the turn-on a LED sketch, but the pushButton convention, used in the other sketch also defines a state change. HOWEVER, that seems to be defined later with these lines of code:

  int buttonState = digitalRead(pushButton);
  // print out the state of the button:
  Serial.println(buttonState);

That definition, in turn, seems to be covered by this line of code:

int buttonState = 0;

These simple Example sketches are not so simple when you start comparing code conventions. Thanks again for the replies.

I wouldn't go so far in trying to figure out why one used buttonState and the other pushButton. It is probably as simple as two different people wrote those two different pieces of code and one used one name and the other used a different name. There's not much to read into it. Use names that make sense to you.

Both sketches have a variable named buttonState and use it for the exact same thing. As expected, it stores the return value of digitalRead() on the pin the button is connected to.

The difference between the two is that the DigitalReadSerial example sketch uses the variable name pushButton to define the pin the button is connected to. The Button example sketch uses the variable name buttonPin for the same purpose. I prefer the latter as it's more descriptive but you're free to use whatever names you like.