Pages: [1] 2   Go Down
Author Topic: On naming variables, functions and constants  (Read 1152 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

I find myself struggling naming variables, constants and functions I use.
It seems there is something like there is something going on with case depending on the type of object (variable, constant or function) but I can't quite figure it out...

I'm trying to write more readable and understandable code. Any advice?

Thanks,
Logged

I am a total amateur at electronics and all things related. I have no education in the matter other than self taught.

Please factor this in

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How I work:

All constants that are #define'd are in capitals.

Everything else is camel case.  Don't be afraid to use long names for functions and variables.

If you don't know camel case, it's multiple words joined together where the first word is all lower case and the subsequent words have an uppercase initial letter.  That is how most Arduino functions are done.  pinMode(), digitalWrite(), convertMySignalIntoCheese() etc.

Oh, and don't be afraid to overload your functions if you have two functions that do the same job on two different variable types.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Interesting.
Do you have a naming system as far as variables go? I often find myself struggling to find logical and organised variable names when several functions need values that are similar in purpose but should be kept as separate registers (most notably lastTimeEventOccured or table index counters)
Logged

I am a total amateur at electronics and all things related. I have no education in the matter other than self taught.

Please factor this in

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4820
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can use local variables in functions and the name stays inside the function. Local variables declared static will keep their values while just plain local variables are only good while the function is running.

If you get into C++ objects then you can pack data and code together as object-specific. I can have a widget class with size and color (and maybe a do() function) and make 2 widget objects each with its own size and color, widget_A and widget_B. I would get them as widget_A.size, widget_A.color, etc. Then I can address my variables by what they belong to and use widget_A.do() to make it work.

 
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
lastTimeEventOccured

In that situation I would name the event. lastTimeButtonAPressed, or lastTimeObjectPassedSensor3.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks!
Logged

I am a total amateur at electronics and all things related. I have no education in the matter other than self taught.

Please factor this in

New Jersey
Offline Offline
Faraday Member
**
Karma: 71
Posts: 3744
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Longer the better - that's what copy & paste is for.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh, and keep the scope as tight as possible.

That way you can reuse generic names (like iterator, etc) in multiple places.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

New Jersey
Offline Offline
Faraday Member
**
Karma: 71
Posts: 3744
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
lastTimeEventOccured

In that situation I would name the event. lastTimeButtonAPressed, or lastTimeObjectPassedSensor3.
Usually, you should be able to do better than that: Sensor3 presumably is a physical device of some type, its name should reflect it e.g.  lastTimeObjectPassedCrankshaftHallSensor. I'd agree that that's getting uncomfortably long, but having variables distinguished only by number makes your code harder to follow/maintain.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
lastTimeEventOccured

In that situation I would name the event. lastTimeButtonAPressed, or lastTimeObjectPassedSensor3.
Usually, you should be able to do better than that: Sensor3 presumably is a physical device of some type, its name should reflect it e.g.  lastTimeObjectPassedCrankshaftHallSensor. I'd agree that that's getting uncomfortably long, but having variables distinguished only by number makes your code harder to follow/maintain.

Obviously.  Except I am interfacing to the Yang Fung Sensor3™ smiley
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

New Jersey
Offline Offline
Faraday Member
**
Karma: 71
Posts: 3744
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I see. My apologies, that would indeed then be a perfectly cromulent name  smiley-razz
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4820
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 smiley-roll

The specific names are good and all but they can also lead to code like this:

Code:
int pinForVeeblefeetzerSensor1;
int pinForVeeblefeetzerSensor2;
int pinForVeeblefeetzerSensor3;
int pinForVeeblefeetzerSensor4;
int pinForVeeblefeetzerSensor5;
int pinForVeeblefeetzerSensor6;
int pinForVeeblefeetzerSensor7;
int pinForVeeblefeetzerSensor8;

which should be more like:

Code:
byte veeblefeetzerSensorPin[ 8 ];

And of course we all hope to learn at the company picnic just what a Veeblefeetzer does.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
And of course we all hope to learn at the company picnic just what a Veeblefeetzer does.

I have a box of them here.  I get them as free samples from Schnellblinkenlitze Companieret.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8606
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It's been mentioned but I though I re-iterate.

Constants should be in UPPERCASE, that way you can see at a glance that for example

for (i = 0; i < SOME_VAL; i++)

the number of loop iterations is not based on some variable and you don't waste time searching for code that updates that variable.

Long variable names are good, in general don't use no i, x, y, z etc, OTOH don't create descriptive variable names for the sake of it, I've seen

for (loopIterationCounter = 0; loopIterationCounter < SOME_VAL; loopIterationCounter++)

That's just plane ridiculous, for a simple value that's very local 1-char names are acceptable IMO.


______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50524
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
That's just plane ridiculous, for a simple value that's very local 1-char names are acceptable IMO.
I'd go so far as to say preferred. The key, though, is consistency. Using i as a loop index throughout the code, rather than i one time, and loopIterationCounter another time.
Logged

Pages: [1] 2   Go Up
Jump to: