Naming things and stuff

I wonder how other call/name everything so they know what it is and to distinguish everything.

I have now a Arduino with a lot of sensors and wonder how others name there stuff.
For example:

HTU21D humidityLivingRoomParents
float tempLivingRoomParents
float tempForerunBoiler
float tempRefluxBoiler
float tempForerunMixingLoop
float tempRefluxMixingLoop
float tempHotWaterStorageTank

I often see shortcuts in examples but if i use them then a week later looking at the code it's hart to remember what it is.

Something like:

float humd = myHumidity.readHumidity();

is ok if there is just one sensor. But if you have a lot?
What does "humd" tell you?

MrGlasspoole:
is ok if there is just one sensor. But if you have a lot?
What does "humd" tell you?

its all in your control... what's the issue?

float humd = myHumidity.readHumidity();

Well, not being so posessive is something to stay away from.

After all what does it tell you that you didn't already know?

Is your code so bad you're trying to convince someone else it's their code and not yours?

If you have a lot of "sets" of variables - like, five different things each with their own "temp", "humidity", and "time", then you can define a struct to tie all those things together:

struct  Thermo {
  float temp;
  float humidity;
  uint32_t time;
};

Thermo upstairs;
Thermo downstairs;
Thermo garage;
Thermo outside;

This allows you to use upstairs.temp as a variable.

Even better, you can pass these things in functions by reference.

void foo() {
  if(buttonAIsPressed()) {
    display(upstairs);
  }
  else {
    display(downstairs);
  }
}

void display(Thermo& t) {
  // code to display a Thermo 
}

If you have lots of these things, then you can put them in an array and reference them by number.

Thermo thermo[5]; // five Thermo structs

void quux() {
  display(thermo[2]);
}

This allows you to loop through all of them

void displayAll() {
  for(i=0; i< 5; i++) {
    display(thermo[i]);
    delay(500);
  }
}

You can put functions inside a struct. When a function is inside a struct, it's called a method.

struct  Thermo {
  byte temp_pin;
  byte humidity_pin;
  float temp;
  float humidity;
  uint32_t time;

  void getReading() {
    temp = analogRead(temp_pin);
    humidity = analogRead(humidity_pin);
    time = millis();
  }

};


void updateAll() {
  for(i=0; i< 5; i++) {
    thermo[i].getReading();
  }
}

The variables in a struct are all set to zero at the start, but you can give them an initializer. You can even code up a more complicated kind of initializer called a constructor. Just remember that in arduinos, constructors are invoked before any hardware setup, so they mustn't try to do any talking to pins. In Arduino code, your structs will need their own setup()

struct  Thermo {
  const byte temp_pin;
  const byte humidity_pin;
  const byte led_pin;
  float temp;
  float humidity;
  uint32_t time;

  Thermo(byte attach_temp, byte attach_humidity, byte attach_led) 
    : temp_pin(attach_temp), humidity_pin(attach_humidity), led_pin(attach_led)
  {
  }
  
  void setup() {
    pinMode(led_pin, OUTPUT);
  }

  void getReading() {
    temp = analogRead(temp_pin);
    humidity = analogRead(humidity_pin);
    time = millis();
  }

};

Thermo thermo[5] = {
  Thermo(A0, A1, 3), // garage
  Thermo(A2, A3, 4), // upstairs
  Thermo(A5, A5, 5), // downstairs
  Thermo(A6, A7, 6), // kitchen
  Thermo(A8, A9, 7) // basement
};

void setup() {
  for(i=0; i< 5; i++) {
    thermo[i].setup();
  }
}

For more info, google "C++ tutorial" and learn some stuff about the C++ language. You can also read the page in my sig, but that might be a little more info than you need at this point :slight_smile: .

lloyddean:
Well, not being so posessive is something to stay away from.

Is your code so bad you're trying to convince someone else it's their code and not yours?

Sorry if my english is so bad that is sounds like i criticize other peoples code.
And yes my code is bad. I'm not a coding god like all the other people here.

@Paul, thanks a lot! Very informative.

if(cat.isHungry()){
     feed(cat);
     putOut(dog);  // so he doesn't steal cat's food.
}

Does that order make sense? Shouldn't you put the dog out first? 8)

Nice example, though, and REALLY illustrates the value of proper naming of instances and methods.

I'd kind of like to see the implementation of the methods, to see if the logic continues.

On the most general level, the two biggest naming conventions I use are

  1. Name the variable something informative. Never "x" or "y".

  2. use one-letter prefixes for variable names that correspond to their type.

E.g.

long lNumberOfItemsInCart;
float fTemperatureOfWater;

that way, when you're in the code and can't see the definitions, you should be able to tell what type it is, and what it does.

common exception : for short loops, I'll use i,j,k, but if the loop is so long I scroll it off the page, I use a loop counter that has an information name (for int iAccountIndex = 0; iAccountIndex <10....)

Delta_G:
go google the terms "code smell" and follow down some of the rabbit holes presented there.

There's a very old and venerable rabbit-hole named the Portland Pattern Repository.

http://wiki.c2.com/?CodeSmell