Forum

> > Off Topic > CONST or kConst? | Naming Rules
ForenübersichtOff Topic-ÜbersichtEinloggen, um zu antworten

Englisch CONST or kConst? | Naming Rules

10 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

verschoben CONST or kConst? | Naming Rules

Gaios
Reviewer Off Offline

Zitieren
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 hat geschrieben
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 Kommentar

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

alt Re: CONST or kConst? | Naming Rules

Avo
User Off Offline

Zitieren
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.

alt Re: CONST or kConst? | Naming Rules

_Yank
User Off Offline

Zitieren
user Masea hat geschrieben
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.

alt Re: CONST or kConst? | Naming Rules

Starkkz
Moderator Off Offline

Zitieren
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.

alt Re: CONST or kConst? | Naming Rules

Yates
Reviewer Off Offline

Zitieren
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.

alt Re: CONST or kConst? | Naming Rules

Flacko
User Off Offline

Zitieren
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.
2× editiert, zuletzt 23.09.17 05:15:02
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antwortenOff Topic-ÜbersichtForenübersicht