Pages: [1] 2   Go Down
Author Topic: On naming variables, functions and constants  (Read 837 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: 92
Posts: 3969
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

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

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: 58
Posts: 4036
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

Examples can be found in your IDE.

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
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

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

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
Online Online
Faraday Member
**
Karma: 50
Posts: 3429
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: 92
Posts: 3969
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

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

New Jersey
Online Online
Faraday Member
**
Karma: 50
Posts: 3429
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: 92
Posts: 3969
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

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

New Jersey
Online Online
Faraday Member
**
Karma: 50
Posts: 3429
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: 58
Posts: 4036
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

Examples can be found in your IDE.

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
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

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8458
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
Online Online
Brattain Member
*****
Karma: 551
Posts: 46254
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: