拿朋友的ppt翻译的 还没翻译完就被bs了 ╮(╯▽╰)╭ 继续努力
Detail Design Principle in .Net
Agenda
Method
Method Parameter
Method Return Value
Field
Property
Lock
Multi-Threading
Constructor
一个方法内的代码不能超过30行
The code line in a method no more than 30 lines.
绝大部分方法都可以在30行内结束
Most of method can end less than 30 lines.
这是一种强迫开发者优化设计和代码的方式
This is the way to force developer to optimize design and code.
什么样的方法不应该抛出异常
What kind of method should not throw exception.
静态构造函数
The static constructed function
构析函数
The destructor function
Dispose
属性的Get方法(有的时候可违背)
Property’s get method(Sometimes can break it)
方法注释
Annotation of method
未实现的方法应该抛出
The method have not implement shoud throw
NotImplementedException
移除未被使用到的方法
The method not used should be removed.
方法命名规则推荐Pascal命名法
Nominate use the Pascal on the method named rule.
推荐动词+名词,例如GetUser, DisableUser
Nominate verb + noun, e.g.GetUser, DisableUser
类的实例方法可以直接用动词做方法名 User.Do()
The class’s instance method can direct use verbs as method name.
为什么使用静态方法
Why use static method
较容易实现面向过程的编程
More easy to implement object-oriented programme.
调用较为简单,不需要new
More easy to invoke, not need new it.
静态方法的耦合度往往较低
Lower degree of coupling in static method
较好的性能
Better performance
为什么使用实例方法
Why use instance method
和面向对象的设计结合的比较好
With object-oriented design with better.
功能强大,例如多态,接口继承
Powerful, e.g.polymorphism , interface inheritance
静态方法将被预期是线程安全的
Static method will forecast as thread safty.
如果一个方法没有使用到this,建议改为静态
If a method not use this, suggest you edit it to static.
常用方法有限考虑静态方法,例如Log.Error
Usually use method limited to consider static method, e.g.Log.Error.
显式声明可访问性
Obvious declare accessibility
保持最低的可访问性
Keep the lowest accessibility
避免过度暴露细节
Avoid excessive expose details.
避免过度依赖
Avoid excessive dependent on.
降低开发量
Reduce the development.
使用new重写父类方法,一般要保持相同或者较高的可访问性
Use new to cover superclass method, usually should to keep the same or more accessibility.
显式声明可访问性
Obvious declare accessibility
保持最低的可访问性
Keep the lowest accessibility
避免过度暴露细节
Avoid excessive expose details.
避免过度依赖
Avoid excessive dependent on.
降低开发量
Reduce the development.
使用new重写父类方法,一般要保持相同或者较高的可访问性
Use new to cover superclass method, usually should to keep the same or more accessibility.
重写 – new, 覆盖 – override
Rewrite – new, Cover - override
有些方法应该成组的被override
Some method should be override as group.
IComparable.CompareTo Equals
又例如 == 和 !=
Also e.g. == and !=
构造函数中不能调用虚方法
Construct function should not include invoke visual method.
如果父类也实现了IDispose,那么在子类的Dispose方法中请调用base.Dispose
If superclass also have implemented Idispose, then please invoke base.Dispose in the childclass’s Dispose method.
只在希望子类override的时候使用virtaul
Only hope when childclass override use virsual
一般来说属性返回值相对静态
Generally speaking the return value of property more static.
DateTime.Now就是一个不好的设计
The DateTime.Now is exactly the bad design.
一般来说方法返回值相对动态
Generally speaking the return value of method more dynamic.
Guid.NewGuid()就是一个比较好的设计
The Guid.NewGuid() is exactly a quite good design.
如果可以,优先使用父类作为参数类型
If it is possible, prior to use superclass as the type of parameter
更强的兼容性和处理能力
Stronger compatibility and handling ability.
不要都声明为Object
Not all to declare as object.
参数类型应该尽量准确
The type of parameter should try to accurately
公开方法需要检查所有的输入参数是否有效
Publicly method need to check the effective of all the input parameter.
如果输入参数不合法,终止执行并抛出异常
If the input parameter illegal, termination executive and throw execption.
Performance
公开方法需要检查所有的输入参数是否有效
Publicly method need to check the effective of all the input parameter.
如果输入参数不合法,终止执行并抛出异常
If the input parameter illegal, termination executive and throw execption.
Performance
重载方法最好保持顺序
The overload method better to keep the order.
在某些情况可以使用其他方案取代参数
At some condition could use another solution replace parameter.
不能/不方便修改方法签名
Can’t or not convenience to change the method’s signature.
常见方案
Common scheme
CallContext
Thread Local storage
类型定义
Type definition
避免使用ref和out
Avoid to use ref and out.
偶尔为了性能的原因也会使用
Once a while use for the performance reason.
如果有若干个同类型同含义对象,建议使用params参数
If it have several same type and same meaning object, I recomment to use the params parameter.
适当使用默认参数,可以减少开发的成本
Proper to use default parameter, it could reduce the cost of development.
骆驼命名法
The camel-case.
移除所有的没有被使用到的输入参数
Remove all of the input parameter which is not used.
不要忽略返回值, 不要返回没有意义的值
Don’t ignore the return value, don’t return to meaningless value.
建议将返回值存在本地变量以后再使用
Recomment use the return value after save it to local variable.
链式表达式是个例外
The chain expression is an exception.
将接口作为返回值,往往意味该设计希望解耦
Use interface as return value, often means the design hope to decoupling.
优先使用返回值而不是ref和out参数
Prior to use return value and not the parameter of ref and out.
参数类型和方法名意义保持一致
The type of parameter and the name of method meaning keep the same.
公开方法
Publicly method
尽可能使用父类和接口(封装性)
As far as possible to use superclass and interface(Encapsulation)
内部方法
Internal method
尽可能使用子类
As far as possible to use subclass.
调用方可以得到更多的细节
Caller can get more details.
Void
Int 和其他数值类型
Int and another value type
逻辑为正数,使用-1作为条件不成立的情况
Logic value is positive number, use -1 as the false condition.
逻辑为整数,考虑使用int?
Logic value is integer, consider to use int?
String
Null作为条件不存在的表示
Null as the condition not exist display.
传递为表现层的考虑Strin.Empty优于null
The layer deliver as presentation consider String.Empty first to null.
Class
Null
Collection
不为null但是数量为0的集合
The collection is Not null but count is zero.
泛型
Generics
Default(T)
When & Why Exception
内部方法一般使用异常来表示错误
Internal method general to use exception to show error.
和FCL的行为保持一致
The same with the FCL’s behavior.
异常不影响正常逻辑代码的阅读
The exception not influence the normal read in logic code.
异常的信息更为丰富,例如堆栈
The information of exception more rich, e.g.stack.
异常的功能更为强大,例如UnhandledException
The exception’s function is more powerful, e.g. Unhandledexception.
强迫用户处理异常(用户容易忽略返回值)
Force user solve exception(user easy ignore the return value)
When & Why ReturnValue
外部方法中应该使用返回值
External method should use return value.
Asp.net和WCF都提供了全局的错误转换机制
The asp.net and the WCF support global error conversion mechanism.
性能问题
The issues of performance
例如Int.TryPrase
E.g.Int.TryPrase
不要依赖于字段的初始化顺序
Should not dependent on the order of field initialize.
字段不应该是Public的
The field should not be public.
Const可以是public的,不过不推荐使用
Const can publicly, but not recomment to user.