C# Naming Standard
In this Article:
π€ Notice something is missing or not working? β Please contact Zoel (zoelbastianbach@agate.id) on Ms. Teams!
Overview
Consistency is the key to maintainable code. This statement is most true for naming your projects, source files, and identifiers including Fields, Variables, Properties, Methods, Parameters, Classes, Interfaces, and Namespaces.
-
β Why should I care about this article?
Naming conventions result in improvements in terms of "four Cs": communication, code integration, consistency and clarity. The idea is that "the code should explain itself". Following a consistent set of naming conventions in the development can be a major contribution to its usability.
-
π― The goal of this article
This article will provide a consistent set of naming conventions utilized in Agate
-
π€·π» If I have further question about this article, who can I reach out to?
Please contact: Fachri Akbar (fachri@agate.id)
The table below is the summary/TL;DR version of the naming conventions in this article:
[Naming Overview](Naming Standard e8d0afdf61254076932a7230c967b92c/Naming Overview 30543ddc7b664078948c460709d767ca.csv)
General Naming Convention
-
β ALWAYS use camelCase or PascalCase names
-
β AVOID the use of ALL CAPS and all lowercase names. Single lowercase words or letters are acceptable.
-
β DO NOT create declarations of the same type (namespace, class, method, property, field, or parameter) and access modifier (protected, public, private, internal) that vary only by capitalization. Assume you will be working with languages that does not support case-sensitivity, even if you know you're not.
public int SomeIntVariable; public int SomeintVariable; public int SomeIntvariable;
-
β DO NOT use names that begin with a numeric character.
// **β** DO NOT do this: public class 6AxisControler {} // **β ** DO this instead: public class SixAxisControler {}
-
β ALWAYS choose meaningful, self-explanation and specific names
// **β** DO NOT do this: public int Asdf; //a = final position //b = current position //c = pivot a = b β c; // **β ** DO this instead: public int BallCount; finalPos = currentPos β pivot;
-
Variables and Properties should describe an entity not the type or size. And also do not use Hungarian Notation
// **β** DO NOT do this: public int nBall; //number of ball public bool bBusy; //Boolean variable // **β ** DO this instead: public int BallCount; public bool IsBusy;
-
β AVOID using abbreviations unless the full name is excessive (more than 25 character).
// **β** DO NOT do this: public float ExtGlow; public int GetTheLatestWebSiteAccessTimeInMilliseconds() // **β ** DO this instead: public float ExternalGlow; public int GetLatestWebAccessTimeMillis()
-
Any Abbreviations must be widely known and accepted.
public int Atk; public int Def; public float Pow;
-
-
β DO NOT use C# reserved words as names.
-
β AVOID naming conflicts with existing .NET Framework namespaces, or types.
-
β AVOID adding redundant or meaningless prefixes and suffixes to identifiers
// **β** DO NOT do this: public enum ColorsEnum public class CVehicle public struct RectangleStruct // **β ** DO this instead: public enum Colors public class Vehicle public struct Rectangle
-
β DO NOT include the parent class name within a property name.
// **β** DO NOT do this: public class Customer { public int CustomerName; } // **β ** DO this instead: public class Customer { public int Name; }
-
β TRY to prefix boolean variables and properties with βCanβ, βIsβ or βHasβ.
-
Append computational qualifiers to variable names like Average, Count, Sum, Min, and Max where appropriate.
-
When defining a root namespace, use a Product, Company, or Developer Name as the root.
Agate.ProductName
Naming Usage
Namespace
Source Files
Capitalization: PascalCase
β ALWAYS match class name and file name.
β AVOID including more than one class, enum (global), or delegate (global) per file.
Use a descriptive file name when containing multiple class, enum (global), or delegate (global).
MyClass.cs
public class MyClass {β¦}
Class or Struct
Capitalization: PascalCase
Use a noun or noun phrase for class name. Add an appropriate class-suffix when sub-classing another type when possible.
private class MyClass {β¦}
internal class SpecializedAttribute : Attribute {β¦}
public class CustomerCollection : CollectionBase {β¦}
public class CustomEventArgs : EventArgs {β¦}
private struct GameSettings {β¦}
Interface
Capitalization: PascalCase
β ALWAYS prefix interface name with capital βIβ
interface ICreature {β¦}
interface IWeapon
Generic Class
Capitalization: PascalCase
β ALWAYS use a single capital letter, such as T or K.
public class FifoStack<T> {
public void Push(T obj) {β¦}
public T Pop() {β¦}
}
Method
Capitalization: PascalCase
β TRY to use a Verb or Verb-Object pair.
public void Execute() {β¦}
private string GetAssemblyVersion(Assembly target) {β¦}
Property
Capitalization: PascalCase
Property name should represent the entity it returns. Never prefix property names with βGetβ or βSetβ.
// DO NOT do this:
public string GetName {
get{β¦}
}
// DO this instead:
public string Name {
get{β¦}
set{β¦}
}
Field (Public, Protected, or Internal)
Capitalization: PascalCase
β AVOID using non-private Fields! Use Properties instead.
public string Name;
protected IList InnerList;
Field (Private)
Capitalization: camelCase
Prefix with a single underscore ( _ ) character.
private string _name;
Constant or Static Field
Treat like a Field. Choose appropriate Field access-modifier above.
Enum
Capitalization: PascalCase (both the Type and the Options).
Add the FlagsAttribute to bit-mask multiple options.
public enum CustomerTypes
{
Consumer,
Commercial
}
Delegate or Event
Treat as a Field. Choose appropriate Field access-modifier above.
public event EventHandler LoadPlugin;
Variable (inline)
Capitalization: camelCase
- β AVOID using single characters like βxβ or βyβ except in FOR loops.
- β AVOID enumerating variable names like text1, text2, text3 etc.
Parameter
Capitalization: camelCase
public void Execute(string commandText, int iterations) {β¦}