C# Programming Guidelines(摘录)
以下条目来自于Larry O'Brien与Bruce Eckel的《Think C#》一书,我个人认为英文表达胜于使用中文来表达,所以也没有翻译相关的内容。
- Elegance Always pays off.
- First make it work, then make it fast. 在敏捷开发中,有一条原则:Keep it simple
- Remember the "divide and conquer" principle.
- Separate the class creator from the class user (client programmer)
- When you create a class, attempt to make your names so clear that comments are unnecessary.
- Your analysis and design must produce, at minimum, the classes in your system, their public interfaces, and their relationships to other classes, especially base classes.
- Automate everything. 类似于敏捷开发中的Continue Integrated.
- Write the test code first (before you write the class) in order to verify that your class design is complete. 类似于敏捷开发中的Write Test First
- All software design problems can be simplified by introducing an extra level of conceptual indirection.
- An indirection should have a meaning.
- Make classes as atomic as possible. 单一职责原则(SRP)很好地解释了这一点,SRP可以理解为为“就一个类而言,应该仅有一个引起它变化的原因”,详细信息请参阅《Agile Software Development Principles, Patterns, and Practices》;
- Watch for long argument lists. 可以使用重构中的Introduce Parameter object(引入参数对象)来重构代码,关于重构,请参阅《Refactoring: Improving the Design of Existing Code》
- Don't repeat yourself. 不要使你的代码有Duplicated Code坏味道,当有后可以使用重构中的Extract Method手段来重构代码;
- Watch for switch statements or chained if-else clauses. 不要使你的代码有Switch Statements坏味道,当有后可以使用重构中的Replace Conditional with Polymorphism手段来重构代码;
- From a design standpoint, look for and separate things that change from thins that stay the same.
- Don't extend fundamental functionality by subclassing.
- Less is more.
- Read your classes aloud to make sure they're logical.
- When deciding between inheritance and composition, ask if you need to upcast to the base type.
- Use data members for variation in value and method overriding for variation in behavior.
- Watch for overloading.
- Use exception hierarchies;
- Sometimes simple aggregation does the job.
- Consider the perspective of the client programmer and the person maintaining the code;
- Watch out for "giant object syndrome."
- If you must do something ugly, at least localize the ugliness inside a class.
- If you must do something nonportable, make an abstraction for that service and localize it within a class.
- Objects should not simple hold some data.
- Choose composition first when creating new classes from existing classes;
- Use inheritance and method overriding to express differences in behavior, and fields to express variations in state.
- Watch out for variance;
- Watch out for limitation during inheritance.
- Using design patterns to eliminate "naked functionality.";
- Watch out for "analysis paralysis.";
- When you think you've got a good analysis, design, or implementation, do a walkthrough.
Implementation
- In general, follow the Microsoft coding conventions.
- Whatever coding style you use, it really does make a difference if your team (and even better, your company) standardizes on it.
- Follow standard capitalization rules.
- Don't create your own "decorated" private data member names;
- Follow a "canonical form" when creating a class for general-purpose use.
- Sometimes you need to inherit in order to access protected members of the base class.
- If two classes are associated with each other in some functional way (such as containers and iterators), try to make one an inner class of the other;
- Anytime you notice classes that appear to have high coupling with each other, consider the coding and maintenance improvements you might get by using inner classes;
- Don't fall prey to premature optimization;
- Keep scopes as small as possible so the visibility and lifetime of your objects are as small as possible.
- Use the containers in the .NET Framework SDK.
- For a program to be robust, each component must be robust.
- Prefer compile-time errors to run-time errors 尽可能地将错误在编译阶段解决;
- Watch for long method definitions;
- Keep things as "private as possible"; 应尽量少暴露外部接口给用户
- Use comments liberally, and use the comment-documentation syntax product your program documentation.
- Avoid using "magic numbers"; 应该使用常量来代替
- When creating constructors, consider exceptions.
- If your class requires any cleanup when the client programmer is finished with the object, make your class implement IDisposable interface;
- When you are creating a fixed-size container of objects, transfer them to an array;
- Choose interfaces over abstract classes;
- Inside constructors, do only what is necessary to set the object into the proper state;
- Watch out for accidental overloading;
- Watch out for premature optimization;
- Remember that code is read much more than it is written;