博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C# Programming Guidelines(摘录)

Posted on 2004-12-21 18:47  jeseeqing  阅读(2823)  评论(10编辑  收藏  举报

C# Programming Guidelines(摘录)

以下条目来自于Larry O'BrienBruce Eckel的《Think C#》一书,我个人认为英文表达胜于使用中文来表达,所以也没有翻译相关的内容。

 

  1. Elegance Always pays off.
  1. First make it work, then make it fast.  在敏捷开发中,有一条原则:Keep it simple
  1. Remember the "divide and conquer" principle.
  1. Separate the class creator from the class user (client programmer)
  1. When you create a class, attempt to make your names so clear that comments are unnecessary.
  1. 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.
  1. Automate everything. 类似于敏捷开发中的Continue Integrated.
  1. Write the test code first (before you write the class) in order to verify that your class design is complete. 类似于敏捷开发中的Write Test First
  1. All software design problems can be simplified by introducing an extra level of conceptual indirection.
  1. An indirection should have a meaning.
  1. Make classes as atomic as possible.  单一职责原则(SRP)很好地解释了这一点,SRP可以理解为为“就一个类而言,应该仅有一个引起它变化的原因”,详细信息请参阅《Agile Software Development Principles, Patterns, and Practices》;
  1. Watch for long argument lists. 可以使用重构中的Introduce Parameter object(引入参数对象)来重构代码,关于重构,请参阅《Refactoring: Improving the Design of Existing Code
  1. Don't repeat yourself. 不要使你的代码有Duplicated Code坏味道,当有后可以使用重构中的Extract Method手段来重构代码;
  1. Watch for switch statements or chained if-else clauses. 不要使你的代码有Switch Statements坏味道,当有后可以使用重构中的Replace Conditional with Polymorphism手段来重构代码;
  1. From a design standpoint, look for and separate things that change from thins that stay the same.
  1. Don't extend fundamental functionality by subclassing.
  1. Less is more.
  1. Read your classes aloud to make sure they're logical.
  1. When deciding between inheritance and composition, ask if you need to upcast to the base type.
  1. Use data members for variation in value and method overriding for variation in behavior.
  1. Watch for overloading.
  1. Use exception hierarchies;
  1. Sometimes simple aggregation does the job.
  1. Consider the perspective of the client programmer and the person maintaining the code;
  1. Watch out for "giant object syndrome."
  1. If you must do something ugly, at least localize the ugliness inside a class.
  1. If you must do something nonportable, make an abstraction for that service and localize it within a class.
  1. Objects should not simple hold some data.
  1. Choose composition first when creating new classes from existing classes;
  1. Use inheritance and method overriding to express differences in behavior, and fields to express variations in state.
  1. Watch out for variance;
  1. Watch out for limitation during inheritance.
  1. Using design patterns to eliminate "naked functionality.";
  1. Watch out for "analysis paralysis.";
  1. When you think you've got a good analysis, design, or implementation, do a walkthrough.

 

Implementation

  1. In general, follow the Microsoft coding conventions.
  1. Whatever coding style you use, it really does make a difference if your team (and even better, your company) standardizes on it.
  1. Follow standard capitalization rules.
  1. Don't create your own "decorated" private data member names;
  1. Follow a "canonical form" when creating a class for general-purpose use.
  1. Sometimes you need to inherit in order to access protected members of the base class.
  1. 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;
  1. 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;
  1. Don't fall prey to premature optimization;
  1. Keep scopes as small as possible so the visibility and lifetime of your objects are as small as possible.
  1. Use the containers in the .NET Framework SDK.
  1. For a program to be robust, each component must be robust.
  1. Prefer compile-time errors to run-time errors 尽可能地将错误在编译阶段解决;
  1. Watch for long method definitions;
  1. Keep things as "private as possible"; 应尽量少暴露外部接口给用户
  1. Use comments liberally, and use the comment-documentation syntax product your program documentation.
  1. Avoid using "magic numbers"; 应该使用常量来代替
  1. When creating constructors, consider exceptions.
  1. If your class requires any cleanup when the client programmer is finished with the object, make your class implement IDisposable interface;
  1. When you are creating a fixed-size container of objects, transfer them to an array;
  1. Choose interfaces over abstract classes;
  1. Inside constructors, do only what is necessary to set the object into the proper state;
  1. Watch out for accidental overloading;
  1. Watch out for premature optimization;
  1. Remember that code is read much more than it is written;