好:if ( showResult == true ){for ( int i = 0; i10; i++ ){//}}
不好:if(showResult==true){for(int i= 0;i10;i++){//}}
良好的编程习惯
遵从以下良好的习惯以写出好程序
避免使用大文件 。如果一个文件里的代码超过300~400行,必须考虑将代码分开到不同类中 。
避免写太长的方法 。一个典型的方法代码在1~25行之间 。如果一个方法发代码超过25行 , 应该考虑将其分解为不同的方法 。
方法名需能看出它作什么 。别使用会引起误解的名字 。如果名字一目了然,就无需用文档来解释方法的功能了 。
好:void SavePhoneNumber ( string phoneNumber ) {// Save the phone number. }
不好:// This method will save the phone number. void SaveData ( string phoneNumber ) {// Save the phone number. }
一个方法只完成一个任务 。不要把多个任务组合到一个方法中,即使那些任务非常小 。
好:// Save the address. SaveAddress (address );// Send an email to the supervisor to inform that the address is updated. SendEmail ( address, email );void SaveAddress ( string address ) {// Save the address.// ... }void SendEmail ( string address, string email ) {// Send an email to inform the supervisor that the address is changed.// ... }
不好:// Save address and send an email to the supervisor to inform that the address is updated. SaveAddress ( address, email ); void SaveAddress ( string address, string email ) {// Job 1.// Save the address.// ...// Job 2.// Send an email to inform the supervisor that the address is changed.// ... }
使用C# 或 VB.NET的特有类型,而不是System命名空间中定义的别名类型 。
好:int age; string name; object contactInfo;
不好:Int16 age; String name; Object contactInfo;
别在程序中使用固定数值,用常量代替 。
别用字符串常数 。用资源文件 。
避免使用很多成员变量 。声明局部变量,并传递给方法 。不要在方法间共享成员变量 。如果在几个方法间共享一个成员变量,那就很难知道是哪个方法在什么时候修改了它的值 。
必要时使用enum。别用数字或字符串来指示离散值 。
好: enum MailType {Html,PlainText,Attachment } void SendMail (string message, MailType mailType) {switch ( mailType ){case MailType.Html:// Do somethingbreak;case MailType.PlainText:// Do somethingbreak;case MailType.Attachment:// Do somethingbreak;default:// Do somethingbreak;} }
不好:void SendMail (string message, string mailType) {switch ( mailType ){case "Html":// Do somethingbreak;case "PlainText":// Do somethingbreak;case "Attachment":// Do somethingbreak;default:// Do somethingbreak;} }
别把成员变量声明为 public 或 protected 。都声明为 private 而使用 public/protected 的Properties.
不在代码中使用具体的路径和驱动器名 。使用相对路径,并使路径可编程 。
永远别设想你的代码是在“C:”盘运行 。你不会知道,一些用户在网络或“Z:”盘运行程序 。
应用程序启动时作些“自检”并确保所需文件和附件在指定的位置 。必要时检查数据库连接 。出现任何问题给用户一个友好的提示 。
如果需要的配置文件找不到 , 应用程序需能自己创建使用默认值的一份 。
如果在配置文件中发现错误值,应用程序要抛出错误,给出提示消息告诉用户正确值 。
错误消息需能帮助用户解决问题 。永远别用象"应用程序出错", "发现一个错误" 等错误消息 。而应给出象 "更新数据库失败 。请确保登陆id和密码正确 。" 的具体消息 。
显示错误消息时,除了说哪里错了,还应提示用户如何解决问题 。不要用 象 "更新数据库失败 。"这样的,要提示用户怎么做:"更新数据库失败 。请确保登陆id和密码正确 。"
- mysql重置数据库 重配mysql的编码
- mysql查询数据库编码格式 mysql查数据有乱码
- mysql数据库编码格式查询 mysql编码函数
- redis客户端设置编码
- redis 规范 redis协议有什么用
- redis string 编码 redis字符串的编码
- 阿里云redis开发规范 阿里redis开发
- mongodb设计规范 mongodb设计工具
- mysql设置表的编码 mysql在哪设置表前缀
- mysql 编码 mysql编码存储表情
