Skip to content

Variable Coding Standards

Liam Healey edited this page Feb 25, 2022 · 17 revisions

Names

Variable names are critical as good variable names can make code much more readable and eliminates the need for some comments elsewhere in the code. Variables should always be nouns that describe what the variable is storing, though, nouns with irregular plurals (Like data or sheep) should be avoided if possible. They should be clear, specific, and concise. Always use pascal case.

For normal variables i.e. int, float, UObject*, TSubclassOf, variable names should always be singular.

For list-type variables i.e. TArray, TSet, TSparseArray, variable names should always be plural.

For pair-type variables i.e. TPair, TTupple, variable names should be formatted as follows: [Singular Key Noun]To[Singular Value Noun]

For map-type variables i.e. TMap, TGridMap, variable names should be formatted as follows: [Plural Key Noun]To[Pural Value Noun]

For variables declared as an iterator for a for each loop, variable names should be the same as the thing they are iterating though, minus plurality, prefixed by Each.

Good Examples:

  • int NumberOfPlayers;
  • float ThrustForce;
  • TArray<AActor*> AimTargets;
  • for(AActor* EachAimTarget : AimTargets)
  • TMap<APlayerController*, int> PlayersToScores;
  • for(TPair<APlayerController*, int> EachPlayerToScore : PlayersToScores)

Bad Examples:

  • int Players; - int is a sinugalr type, and it is unclear what an integer player is.
  • float Force; - it is too ambiguous.
  • TArray<AActor*> AimTarget; - TArray names should be plural.
  • for(AActor* TargetElement : AimTargets) - Does not follow the for each iterator naming convention.
  • TMap<APlayerController*, int> PlayerScores; - Name dose not indicate that the variable is a map.
  • for(TPair<APlayerController*, int> Score : PlayersToScores) - Does not indicate that the variable is a pair or an for each iterator.

Commenting

All variables should have comments describing what they store. These comments should avoid mentioning how the variable is used in logic. Comments should be a complete sentences.

//[Description]
int MyInt{ 0 };

Clone this wiki locally