Forum

> > Off Topic > CONST or kConst? | Naming Rules
Forums overviewOff Topic overviewLog in to reply

English CONST or kConst? | Naming Rules

10 replies
To the start Previous 1 Next To the start

moved CONST or kConst? | Naming Rules

Gaios
Reviewer Off Offline

Quote
Hello,

Recently, I was thinking about Programming Style Standards
and I wonder if I should use e.g.:
kDaysInWeek
or
DAYS_IN_WEEK
.

Very often I encounter with CONST style instead of kConst, but Google Style Guide says:
Google Style Guide has written
Preferably, the individual enumerators should be named like constants. However, it is also acceptable to name them like macros. The enumeration name, UrlTableErrors (and AlternateUrlTableErrors), is a type, and therefore mixed case.
1
2
3
4
5
6
7
8
9
10
enum UrlTableErrors {
	kOK = 0,
	kErrorOutOfMemory,
	kErrorMalformedInput,
};
enum AlternateUrlTableErrors {
	OK = 0,
	OUT_OF_MEMORY = 1,
	MALFORMED_INPUT = 2,
};
Hence, the change to prefer constant-style naming was put in place. New code should prefer constant-style naming if possible.

Should I use kConst for Classes and CONST for global constants?

Admin/mod comment

Wrong section forum, the thread has been moved. /user GeoB99

old Re: CONST or kConst? | Naming Rules

Avo
User Off Offline

Quote
To me, using uppercase letters seems fine. But if you mean C++ (not all languages in general), it's good to use some features of the language. For example scoped enums:

1
2
3
4
5
6
enum class PacketStatus {lost, received} myPacket;
...
if(myPacket == PacketStatus::received)
{
	...
}

or namespaces (it's good for global constants):

1
2
3
4
5
6
7
namespace consts
{
	const float gravity = 9.8;
	const float pi = 3.14;
}
...
float force = consts::gravity * mass;

This is what I like personally, but keep in mind that naming convention is also project-dependent.

By the way, wrong section.

old Re: CONST or kConst? | Naming Rules

Masea
Super User Off Offline

Quote
The ones are written with uppercase letters make my eyes bleed out always, so I use the other one. It's all up to you actually.

old Re: CONST or kConst? | Naming Rules

_Yank
User Off Offline

Quote
user Masea has written
It's all up to you actually.

Depends on whether you want your code to be actually read and understood or, for example, you're working with other people.

old Re: CONST or kConst? | Naming Rules

Starkkz
Moderator Off Offline

Quote
Supposedly
wordsLikeThis
are meant to be used for class attributes, so if you want to have a factor that would make a difference you would be using
CAPS_FOR_ENUMERATIONS
(not saying that you can't use
wordsLikeThis
for enumerations).

At least I know people who said that when they worked on big companies, they often saw that.

old Re: CONST or kConst? | Naming Rules

Yates
Reviewer Off Offline

Quote
CONSTANTS_ARE_ALWAYS_CAPITAL_SNAKE_CASED

(including class constants)
snake_case_for_json_and_your_database

CapitalCamelCaseForClassNames

camelCaseForPrettyMuchEverythingElse

(variables, functions etc)

If you follow these you'll be good to go for almost every programming language out there.

old Re: CONST or kConst? | Naming Rules

Flacko
User Off Offline

Quote
Honestly, I haven't seen people writing kConsts at all. CONSTS were inherited from C macro naming conventions so they are much more prevalent.

Either way, I don't think it really matters which one you choose as long as you are consistent. Using one style for globals and another style for class constants is inconsistent, I'd advice to avoid that.

I also think you should avoid having a single header (let alone a single class/namespace) dedicated to storing all your constant values. This is clearly an anti-pattern because it makes your code less reusable:
• If you are only interested in reusing a few constant definitions, you still have to include (and maintain) many other baggage-consts
• If you find out you need new const definitions, you would have to add them to the header/class. The thing will become impossible to maintain after adding new stuff every few projects.
edited 2×, last 23.09.17 05:15:02 am
To the start Previous 1 Next To the start
Log in to replyOff Topic overviewForums overview