Coding standards

From OpenSimulator

(Difference between revisions)
Jump to: navigation, search
(Example)
(Guidelines)
Line 1: Line 1:
 
==Guidelines==
 
==Guidelines==
  
Generally speaking
+
<b>When adding to existing code please respect the conventions already being used in the file you're editing.</b>
 +
 
 +
<b>We follow standard C# coding guidelines unless otherwise stated: [http://msdn.microsoft.com/en-us/library/czefa0ke.aspx]</b>
 +
 
 +
=== Formatting ===
  
 
* 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.
* Method names have all their words capitalized (as opposed to Java, which culturally uses camelCase).
+
 
* Unless the classes are very trivial, there should be one class per file.
+
=== Naming ===
 +
 
 +
* We use full words to name things.
 +
* Aim for assosiative naming so that the logic is clear without explicit comments.
 +
 
 +
=== Classes ===
 +
 
 +
Unless the classes are very trivial, there should be one class per file.
 +
 
 +
<pre>
 +
public class SimpleExample
 +
{
 +
 
 +
}
 +
</pre>
 +
 
 +
=== Methods ===
 +
 
 +
Method names have all their words capitalized (as opposed to Java, which culturally uses camelCase).
 +
 
 +
<pre>
 +
public class SimpleExample
 +
{
 +
    public void ExampleMethod(int exampleAttribute)
 +
    {
 +
        String exampleVariable="test value";
 +
    }
 +
}
 +
</pre>
 +
 
 +
=== Fields ===
 +
 
 +
Fields should be always initialized when declared. Member fields start with m_ and continue in camelCase:
 +
 
 +
<pre>
 +
public class SimpleExample
 +
{
 +
    private long m_exampleMemberField=0;
 +
    private static double m_exampleStaticMemberField=2;
 +
 
 +
    public void ExampleMethod(int exampleAttribute)
 +
    {
 +
        String exampleVariable="test value";
 +
    }
 +
}
 +
</pre>
 +
 
 +
=== Warnings ===
 +
 
 
* Please keep the code warning free, using #pragma only if absolutely necessary.  For instance  
 
* Please keep the code warning free, using #pragma only if absolutely necessary.  For instance  
  
Line 13: Line 65:
 
  #pragma warning restore 0612
 
  #pragma warning restore 0612
  
Otherwise in general, when adding to existing code please respect the conventions already being used in the file you're editing (which should often follow the [http://msdn.microsoft.com/en-us/library/czefa0ke.aspx standard C# coding guidelines]).
+
=== Logging ===
 +
 
 +
Each class should have their own logger which is declared private and is not inherited:
 +
 
 +
<pre>
 +
public class SimpleExample
 +
{
 +
    private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
 +
}
 +
</pre>
  
 
==Example==
 
==Example==

Revision as of 12:23, 11 January 2009

Contents

Guidelines

When adding to existing code please respect the conventions already being used in the file you're editing.

We follow standard C# coding guidelines unless otherwise stated: [1]

Formatting

  • We put curly brackets on separate lines and use 4 space tabs.
  • Tab themselves should be spaces, not actual hard tabs.

Naming

  • We use full words to name things.
  • Aim for assosiative naming so that the logic is clear without explicit comments.

Classes

Unless the classes are very trivial, there should be one class per file.

public class SimpleExample
{

}

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";
    }
}

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";
    }
}

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

Logging

Each class 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);
}

Example

public void WakeUp(bool tooTired)
{
    if (tooTired)
    {
        Thread.Sleep(3600);
    }
}
Personal tools
General
About This Wiki