Class variable naming guidelines?

Working with classes here and I feel like the use of the same name in different parts of the code isn't good practice. Some times name is "Name" and other times it's "name" and the same with mana and health. I'll provide my main.cpp, player.cpp, and player.h below. Using my current names and setup. could you suggest a more appropriate alternative for these names? They even confuse me and I'm the one that wrote it.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "Player.h"
#include <conio.h>
#include <iostream>
#include <string>

std::string name;
int health = 0, mana = 0;

int main()
{
	Player Player1(name, health, mana);

	return 0;
}


Player.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "Player.h"

Player::Player(std::string name, int Health, int Mana)
{
	newName = name;
	newHealth = Health;
	newMana = Mana;
}

Player::~Player()
{

}

//Get class data.
std::string Player::getName()
{
	return newName;
}

int Player::getHealth()
{
	return newHealth;
}

int Player::getMana()
{
	return newMana;
}

//Set class data.
void Player::setName(std::string name)
{
	newName = name;
}

void Player::setHealth(int Health)
{
	newHealth = Health;
}

void Player::setMana(int Mana)
{
	newMana = Mana;
}


Player.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#pragma once
#ifndef PLAYER_H
#define PLAYER_H

#include <iostream>
#include <string>

class Player
{
public:
	Player();

	Player(std::string, int, int);

	~Player();

	std::string getName();
	int getHealth();
	int getMana();


	void setName(std::string);
	void setHealth(int);
	void setMana(int);

private:
	std::string newName;
	int newHealth;
	int newMana;
};


#endif


Any other suggestions to the way I create and handle this class are appreciated as well. Thanks folks and Merry Christmas!
Last edited on
It looks fine to me (I just dislike how you store it as newName... it should be simply name, change the constructor's parameters instead).

Personally, lately I only follow the g_ and m_ convention:

Globals begin with g_ (g_name) and class members begin with m_ (m_name).
Occasionally I do the same for parameters (p_) and locals (l_).

Your constructor would then become:
Player::Player(std::string const& p_name, int p_health, int p_mana) : m_name(p_name), m_health(p_health), m_mana(p_mana) {}
(Note: I added a const& in the constructor. It helps to avoid expensive object copies).

Merry Christmas!
Makes sense and I like your g_ and m_ solution. Appreciate the feedback.
Topic archived. No new replies allowed.