This post is a exact copy from the Godot engine documentation article with same name.
I agree with almost all the conventions and guidelines that it encourages, so, I think is good to share it.
Original article:
https://docs.godotengine.org/en/stable/getting_started/scripting/c_sharp/c_sharp_style_guide.html#doc-c-sharp-styleguide
C# style guide
Having well-defined and consistent coding conventions is important for every project, and Godot is no exception to this rule.
This page contains a coding style guide, which is followed by developers of and contributors to Godot itself. As such, it is mainly intended for those who want to contribute to the project, but since the conventions and guidelines mentioned in this article are those most widely adopted by the users of the language, we encourage you to do the same, especially if you do not have such a guide yet.
Note
This article is by no means an exhaustive guide on how to follow the standard coding conventions or best practices. If you feel unsure of an aspect which is not covered here, please refer to more comprehensive documentation, such as C# Coding Conventions or Framework Design Guidelines.
Language specification
Godot currently uses C# version 7.0 in its engine and example source code. So, before we move to a newer version, care must be taken to avoid mixing language features only available in C# 7.1 or later.
For detailed information on C# features in different versions, please see What's New in C#.
Naming conventions
Use PascalCase for all namespaces, type names and member level identifiers (i.e. methods, properties, constants, events), except for private fields:
namespace ExampleProject
{
public class PlayerCharacter
{
public const float DefaultSpeed = 10f;
public float CurrentSpeed { get; set; }
protected int HitPoints;
private void CalculateWeaponDamage()
{
}
}
}
Use camelCase for all other identifiers (i.e. local variables, method arguments), and use an underscore (_
) as a prefix for private fields (but not for methods or properties, as explained above):
private Vector3 _aimingAt; // Use a `_` prefix for private fields.
private void Attack(float attackStrength)
{
Enemy targetFound = FindTarget(_aimingAt);
targetFound?.Hit(attackStrength);
}
There's an exception with acronyms which consist of two letters, like UI
, which should be written in uppercase letters where PascalCase would be expected, and in lowercase letters otherwise.
Note that id
is not an acronym, so it should be treated as a normal identifier:
public string Id { get; }
public UIManager UI
{
get { return uiManager; }
}
It is generally discouraged to use a type name as a prefix of an identifier, like string strText
or float fPower
, for example. An exception is made, however, for interfaces, which should, in fact, have an uppercase letter I
prefixed to their names, like IInventoryHolder
or IDamageable
.
Lastly, consider choosing descriptive names and do not try to shorten them too much if it affects readability.
For instance, if you want to write code to find a nearby enemy and hit it with a weapon, prefer:
FindNearbyEnemy()?.Damage(weaponDamage);
Rather than:
FindNode()?.Change(wpnDmg);
Member variables
Don't declare member variables if they are only used locally in a method, as it makes the code more difficult to follow. Instead, declare them as local variables in the method's body.
Local variables
Declare local variables as close as possible to their first use. This makes it easier to follow the code, without having to scroll too much to find where the variable was declared.
Implicitly typed local variables
Consider using implicitly typing (var
) for declaration of a local variable, but do so only when the type is evident from the right side of the assignment:
// You can use `var` for these cases:
var direction = new Vector2(1, 0);
var value = (int)speed;
var text = "Some value";
for (var i = 0; i < 10; i++)
{
}
// But not for these:
var value = GetValue();
var velocity = direction * 1.5;
// It's generally a better idea to use explicit typing for numeric values, especially with
// the existence of the `real_t` alias in Godot, which can either be double or float
// depending on the build configuration.
var value = 1.5;
Other considerations
- Use explicit access modifiers.
- Use properties instead of non-private fields.
- Use modifiers in this order:
public
/protected
/private
/internal
/virtual
/override
/abstract
/new
/static
/readonly
. - Avoid using fully-qualified names or
this.
prefix for members when it's not necessary. - Remove unused
using
statements and unnecessary parentheses. - Consider omitting the default initial value for a type.
- Consider using null-conditional operators or type initializers to make the code more compact.
- Use safe cast when there is a possibility of the value being a different type, and use direct cast otherwise.