Best Practices for .NET enums.
2012-02-03 10:45
351 查看
Naming
In .NET, enums should not have an "Enum" suffix.
Enum documentation
Enums should have an API comment according following pattern.
/// <summary>
/// <para>
/// Defines values that specify the possible ....
/// </para>
/// </summary>
Unit tests
In .NET internally, enums are normally nothing else than ints. Enum members get numerated ascending (0, 1, 2, 3...) according their appearance
in the enum.
This leads to a problem. If we do not exactly specify their values within the code, we will introduce breaking API changes as soon as we add new values somewhere in the middle of the enum because now, all following values get a different
number. This is a critical bug in case we store the enum values persistently, such as in a database or on hard-disk.
Example:
public enum Something
{
None = 0,
Any, // will have value 1
Other, // will have value 2
}
public enum Something
{
None = 0,
Any, // will have value 1
BreakingAPI, // will get value 2, breaks API!!!
Other, // will now have value 3, API broken!!!
}
So, to avoid such situation, as soon as enums members are defined, they are not allowed to change their values anymore for all time. To ensure that, we write unit tests that check for those values to be correct.
[TestMethod]
public void Test_Something_ValuesRemainStable()
{
Assert.AreEqual(3, Enum.GetValues(typeof(Something)).Length, "There have been enumeration values added or removed. This is a BREAKING API CHANGE! More unit tests are needed.");
Assert.AreEqual(0, (int)Something.None, "The value of the enumeration member has changed. This is a BREAKING API CHANGE!");
Assert.AreEqual(1, (int)Something.Any, "The value of the enumeration member has changed. This is a BREAKING API CHANGE!");
Assert.AreEqual(2, (int)Something.Other, "The value of the enumeration member has changed. This is a BREAKING API CHANGE!");
}
In .NET, enums should not have an "Enum" suffix.
Enum documentation
Enums should have an API comment according following pattern.
/// <summary>
/// <para>
/// Defines values that specify the possible ....
/// </para>
/// </summary>
Unit tests
In .NET internally, enums are normally nothing else than ints. Enum members get numerated ascending (0, 1, 2, 3...) according their appearance
in the enum.
This leads to a problem. If we do not exactly specify their values within the code, we will introduce breaking API changes as soon as we add new values somewhere in the middle of the enum because now, all following values get a different
number. This is a critical bug in case we store the enum values persistently, such as in a database or on hard-disk.
Example:
public enum Something
{
None = 0,
Any, // will have value 1
Other, // will have value 2
}
public enum Something
{
None = 0,
Any, // will have value 1
BreakingAPI, // will get value 2, breaks API!!!
Other, // will now have value 3, API broken!!!
}
So, to avoid such situation, as soon as enums members are defined, they are not allowed to change their values anymore for all time. To ensure that, we write unit tests that check for those values to be correct.
[TestMethod]
public void Test_Something_ValuesRemainStable()
{
Assert.AreEqual(3, Enum.GetValues(typeof(Something)).Length, "There have been enumeration values added or removed. This is a BREAKING API CHANGE! More unit tests are needed.");
Assert.AreEqual(0, (int)Something.None, "The value of the enumeration member has changed. This is a BREAKING API CHANGE!");
Assert.AreEqual(1, (int)Something.Any, "The value of the enumeration member has changed. This is a BREAKING API CHANGE!");
Assert.AreEqual(2, (int)Something.Other, "The value of the enumeration member has changed. This is a BREAKING API CHANGE!");
}
相关文章推荐
- Best practices for .Net Performance - I
- 第二讲 Best Practices For Running IIS 6.0 And ASP.NET
- Building Solutions with the Microsoft .NET Compact Framework: Architecture and Best Practices for Mo
- Best Practices for .NET events
- Performance Improvement for WCF Client Proxy Creation in .NET 3.5 and Best Practices
- Best Practices for Using ADO.NET
- ASP.NET Solutions - 23 Case Studies: Best Practices for Developers by Rick Leinecker
- 24 JavaScript Best Practices for Beginners
- Best Practices for Speeding Up Your Web Site
- ASP.NET MVC Best Practices 27+
- Best Practices for Speeding Up Your Web Site (from Yahoo! Developer Network)
- Best Practices for Speeding Up Your Web Site
- Optimizing Data Access and Messaging - Best Practices for Maximizing Scalability and Cost Effectiven
- Exception Handling Best Practices in .NET (By Daniel Turini)
- Best Practices for Speeding Up Your Web Site
- 15 Minute SEO List - Learn how to SEO:List of Best and Worst practices for designing a high traffic website
- Exception Handling Best Practices in .NET
- Best Practices for Spies, Stubs and Mocks in Sinon.js
- Five best practices for Oracle Application Developers
- Best Practices for Web Apps