在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
IntroductionWhen I was looking for a standard convention for generics, I came across a 50-page manual for coding standards for C#. I am sure that, apart from the author, there are few who actually go through and read all of them. So, I thought of coming up with this article. I have tried to comprehend the essential facts which we developers sometimes neglect in C#. I will also discuss some common pitfalls. Small, sweet and simple. That's how I describe it. I hope you enjoy reading. Why Coding StandardsSimple: maintainability. If, 6 months down the line, your customer isn't too happy with the product and wants an enhancement in the application you have created, you should be able to do it without introducing new bugs. There are a lot of other good reasons, but this is the one which concerns us more than anything else. Not following any standard is like going with a temporary solution (which might lead to a permanent problem) and, as you will see, it takes less effort to keep in mind a few simple measures than to do haphazard coding. All you have to do is study good standards once and keep them in the back of your head. Trust me; it's worth it. Contents
Naming"The beginning of wisdom is to call things by their right names" - Chinese Proverb "Meaningful" is the keyword in naming. By meaningful names, I mean concise names that accurately describe the variable, method or object. Let's see how this would be in C#: Namespaces - Names should be meaningful and complete. Indicate your company or name, product and then your utility. Do not abbreviate. //Good namespace CompanyName.ProductName.Utility //Bad namespace CN.PROD.UTIL
Classes - Class names should always be a noun and, again, should be meaningful. Avoid verbs. //Good class Image { ... } class Filters { ... } //Bad class Act { ... } class Enhance { ... } Methods - Always use a verb-noun pair, unless the method operates on its containing class, in which case, use just a verb. //Good public void InitializePath(); public void GetPath(); public void ShowChanges(); public void System.Windows.Forms.Form.Show(); //Bad public void Path(); public void Changes(); Methods with return values - The name should reflect the return value. //Good public int GetImageWidth(Bitmap image); //Bad public int GetDimensions(Bitmap image); Variables - Do not abbreviate variable names. Variable names should again be descriptive and meaningful. //Good int customerCount = 0; int index = 0; string temp = ""; //Bad int cc = 0; int i = 0; string t = ""; Private member variables - Prefix class member variables with public class Image { private int m_initialWidth; private string m_filename; ... } Interfaces - Prefix all interface names with interface IClock { DateTime Time { get; set; } ... } interface IAlarmClock : IClock { void Ring(); DateTime AlarmTime { get; set; } ... } interface IDisposable { void Dispose(); } interface IEnumerable { IEnumerator GetEnumerator(); }
Custom attributes - Suffix all attribute class names with public class IsTestedAttribute : Attribute { public override string ToString() { return "Is Tested"; } } //"Attribute" suffix can be omitted [IsTested] public void Ring(); Custom exceptions - Suffix all custom exception names with public class UserNotExistentException : System.ApplicationException { ... } Delegates - Suffix all event handlers with public delegate void ImageChangedHandler(); public delegate string StringMethodDelegate(); CasingC# standards dictate that you use a certain pattern of Pascal Casing (first word capitalized) and Camel Casing (all but first word capitalized). Pascal Casing - Use PascalCasing for classes, types, methods and constants. public class ImageClass { const int MaxImageWidth = 100; public void ResizeImage(); } enum Days { Sunday, Monday, Tuesday, ... } Camel Casing - Use camelCasing for local variables and method arguments. int ResizeImage(int imageCount) { for(int index = 0; index < imageCount; index++) { ... } } GenericsGenerics, introduced in .NET 2.0, are classes that work uniformly on values of different types. Use capital letters for types; don't use " //Good public class Stack ‹T› //Bad public class Stack ‹t› public class Stack ‹Type› DelegatesUse delegate inference instead of explicit delegate instantiation. public delegate void ImageChangedDelegate(); public void ChangeImage() { ... } //Good ImageChangedDelegate imageChanged = ChangeImage; //Bad ImageChangedDelegate imageChanged = new ImageChangedDelegate(ChangeImage); Use empty parenthesis on anonymous methods without parameters. public delegate void ImageChangeDelegate(); ImageChangedDelegate imageChanged = delegate() { ... } Miscellaneous
Common PitfallsLet's face it, we all do these things one time or another. Let's avoid them as best as we can: Names that make sense to no one but ourselves. string myVar; MyFunction(); Single or double letter variable names (this is excusable for local variables). int a, b, c, a1, j1, i, j, k, ii, jj, kk, etc.
Abstract names. private void DoThis(); Routine48(); string ZimboVariable; Acronyms. //AcronymFunction AF(); //SuperFastAcronymFunction SFAT() Different functions with similar names. DoThis();
DoThisWillYa();
Names starting with underscores. They look cool, but let's not ;) int _m1 = 0; string __m2 = ""; string _TempVariable = ""; Variable names with subtle and context-less meanings. string asterix = ""; // (this is the best function of all) void God() { ... } Abbreviations. string num; int abr; int i; References
And ThanksFor reading this far. I am looking forward to your valuable suggestions. Take care! History
LicenseThis article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL) |
请发表评论