English CONST or kConst? | Naming Rules

10 replies
Goto Page
To the start Previous 1 Next To the start
Up
Gaiyos
Security Supporter
Offline Off
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.
Code:
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?
IMG:http://i.imgur.com/YFN2ICK.png
IMG:http://i.imgur.com/TCHCwEk.png
IMG:http://i.imgur.com/i2Kif0O.png

Admin/mod comment:

Wrong section forum, the thread has been moved. /user Borealis
17.09.17 01:13:56 pm
Up
Avo
User
Offline Off
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:

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


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

Code:
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.
17.09.17 01:17:33 pm
Up
Masea
Super User
Offline Off
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.
"The best taste of the life is doing things that people say you cannot."
17.09.17 01:25:48 pm
Up
_Yank
User
Offline Off
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.
17.09.17 02:44:32 pm
Up
Starkkz
Moderator
Offline Off
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.
lol
17.09.17 05:14:24 pm
Up
Gaiyos
Security Supporter
Offline Off
@user Starkkz: They saw
kWordsLikeThis
or
CAPS_FOR_ENUMERATIONS
?
IMG:http://i.imgur.com/YFN2ICK.png
IMG:http://i.imgur.com/TCHCwEk.png
IMG:http://i.imgur.com/i2Kif0O.png
17.09.17 05:21:34 pm
Up
Starkkz
Moderator
Offline Off
@user Gaiyos:
CAPS_FOR_ENUMERATIONS
.
lol
17.09.17 05:28:42 pm
Up
Yates
Reviewer
Offline Off
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.
18.09.17 11:29:10 am
Up
oxytamine
User
Offline Off
You become a better programmer when you stop looking up programming style standards and start writing code that works instead.
18.09.17 12:10:06 pm
Up
Masea
Super User
Offline Off
@user oxytamine: You get it very wrong :S
"The best taste of the life is doing things that people say you cannot."
21.09.17 06:29:42 pm
Up
Flacko
User
Offline Off
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