Coding standards
From OpenSimulator
(→Fields) |
|||
| (13 intermediate revisions by 5 users not shown) | |||
| Line 1: | Line 1: | ||
| − | ==Guidelines== | + | __NOTOC__ |
| + | {{Quicklinks|Coding_standards}} | ||
| + | <br /> | ||
| + | == Guidelines == | ||
| − | + | Current maximum C# version to use is 12. | |
| − | + | Dotnet version is 8. | |
| − | |||
| − | + | ||
| + | We follow standard C# coding guidelines unless otherwise stated: [http://msdn.microsoft.com/en-us/library/czefa0ke.aspx]. Corrections to existing files to follow these standards are welcome. | ||
=== Formatting === | === Formatting === | ||
| Line 13: | Line 16: | ||
* We put curly brackets on separate lines and use 4 space tabs. | * We put curly brackets on separate lines and use 4 space tabs. | ||
* Tab themselves should be spaces, not actual hard tabs. | * Tab themselves should be spaces, not actual hard tabs. | ||
| − | * Use string instead of String when refering to System.String type. | + | * Use string instead of String when refering to the System.String type. |
=== Naming === | === Naming === | ||
| Line 40: | Line 43: | ||
public void ExampleMethod(int exampleAttribute) | public void ExampleMethod(int exampleAttribute) | ||
{ | { | ||
| − | string exampleVariable="test value"; | + | string exampleVariable = "test value"; |
} | } | ||
} | } | ||
| Line 64: | Line 67: | ||
=== Warnings === | === Warnings === | ||
| − | * Please keep the code warning free, using #pragma only if absolutely necessary. | + | * Please keep the code warning free, using #pragma only if absolutely necessary. For instance |
#pragma warning disable 0612 | #pragma warning disable 0612 | ||
| Line 72: | Line 75: | ||
=== Logging === | === Logging === | ||
| − | Each class should have their own logger which is declared private and is not inherited: | + | Each class, when needed, should have their own logger which is declared private and is not inherited: |
<pre> | <pre> | ||
| Line 80: | Line 83: | ||
} | } | ||
</pre> | </pre> | ||
| + | |||
| + | === use of var and new()=== | ||
| + | |||
| + | Only use var in a variable declaration when the type of the variable is clear on the right side | ||
| + | Only use new(..) in a variable declaration when the type of the variable is clear on the left side | ||
| + | |||
| + | <pre> | ||
| + | var a = new List<string>(); | ||
| + | //or | ||
| + | List<string> b = new(); // ok | ||
| + | |||
| + | var c = mything(); // not ok. we cant see the type without looking for mything details. | ||
| + | </pre> | ||
| + | |||
| + | [[Category:Standards]] | ||
Latest revision as of 13:54, 7 October 2025
| Languages: |
|
[edit] Guidelines
Current maximum C# version to use is 12.
Dotnet version is 8.
We follow standard C# coding guidelines unless otherwise stated: [1]. Corrections to existing files to follow these standards are welcome.
[edit] Formatting
- We put curly brackets on separate lines and use 4 space tabs.
- Tab themselves should be spaces, not actual hard tabs.
- Use string instead of String when refering to the System.String type.
[edit] Naming
- We use full words to name things.
- Aim for assosiative naming so that the logic is clear without explicit comments.
[edit] Classes
Unless the classes are very trivial, there should be one class per file.
public class SimpleExample
{
}
[edit] Methods
Method names have all their words capitalized (as opposed to Java, which culturally uses camelCase).
public class SimpleExample
{
public void ExampleMethod(int exampleAttribute)
{
string exampleVariable = "test value";
}
}
[edit] Fields
Fields should be always initialized when declared. Member fields start with m_ and continue in camelCase:
public class SimpleExample
{
private long m_exampleMemberField = 0;
private static double m_exampleStaticMemberField = 2;
public void ExampleMethod(int exampleAttribute)
{
string exampleVariable = "test value";
}
}
[edit] Warnings
- Please keep the code warning free, using #pragma only if absolutely necessary. For instance
#pragma warning disable 0612
ASSET_TYPE_TO_EXTENSION[(sbyte)AssetType.Script] = "_script.txt"; // Not sure if we'll ever see this
#pragma warning restore 0612
[edit] Logging
Each class, when needed, should have their own logger which is declared private and is not inherited:
public class SimpleExample
{
private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
}
[edit] use of var and new()
Only use var in a variable declaration when the type of the variable is clear on the right side Only use new(..) in a variable declaration when the type of the variable is clear on the left side
var a = new List<string>();
//or
List<string> b = new(); // ok
var c = mything(); // not ok. we cant see the type without looking for mything details.