C Sharp Coding Standards and Best Programing Practices
来源:互联网 发布:英语网络教育 编辑:程序博客网 时间:2024/05/01 03:54
CSDN下载上面有C#编码规范,要5个积分,算了,自己找吧。
找到一个The Code Project上面的T Manjaly写的一篇文章,且不论大师不大师,看着有用就好。
写这个是想印象深刻点
http://www.codeproject.com/KB/cs/c__coding_standards.aspx?pageflow=FixedWidth
前言:
任何人都可以写代码,拥有几个月的代码经验情况下写出的大都是应用层面的代码,如果想让工作变得快捷高效,就必须写高效代码。
命名规范:
- Pascal 全部单词首字母大写
- Camel 除第一个单词外的所有单词首字母大写
类名、函数名:Pascal
变量名、函数参数:Camel
例子:
注意:
不要用Hungarian(匈牙利)命名法去命名变量
不要用缩写
不要用单个字母代替变量名,for循环里面循环变量除外
命名空间的命名规则:
<company name>.<product name>.<top level module>.<bottom level module>
文件名应该和类名保持一致
缩进和空格:
用TAB随进,不要用空格
最好用一个空行区分开代码逻辑
良好的编程习惯
- 避免同时出现两个大的代码文件,如果一个文件代码超过了300-400行,就需要考虑重新包装。
- 避免写代码非常长的函数,一个函数的代码应该控制在1-25行,如果不行,考虑分割开来
- 不要用模糊不清的函数名,如果函数名的意思已经很明显了,没必要注释了
- 一个函数一个功能,不要把不同的功能代码段套在一个函数里面
灵活使用常量
不要使用很多的成员变量
有必要的话考虑用枚举类型来代替很多的分离值
代码例子:
反例子就不贴了
不要把成员变量设置为public或protected
在代码里面不要包括路径或驱动器名
在应用程序打开的时候进行自检,比如运行库,文件位置,数据库连接
如果默认的配置文件丢失,则创建一个新的默认配置文件。
错误提示信息,提示较为详细的错误信息,如数据库服务器出错,给出解决方法
记录错误日志以便解决问题
注释:
不需要为每行代码或每个变量注释,需要的时候再注释
错误处理:
不要隐藏任何可见错误,如果出错,就提示用户并且记录详细的出错信息,函数名、类名
要捕捉特有错误而不是一般性的错误
示例:
没必要处理所有的一般性错误,先不处理让程序崩溃,然后就可以找到大部分可能的出错信息了,
可以用一个应用层面(线程级别)的错误处理机制来处理所有的一般性异常,如果出现意外的一般性异常,记录错误然后提示
有可能的话提示用户是否忽略错误继续运行程序。
针对性的用try-catch
例如一个文件写入函数,只处理一个 FileIOException就可以了
不要写很大的try-catch块,给每个模块分开写
写自己的意外处理类,但是不要继承自 SystemException,推荐 ApplicationException
- C Sharp Coding Standards and Best Programing Practices
- Coding Standards & Best Practices
- C++ Coding Standards: 101 Rules, Guidelines, and Best Practices
- Coding Standards and Best Practices for "appobjects" Files
- C++ Coding Standards and Practices
- JQuery Coding Standards & Best Practices(转)
- Secure Coding Best Practices for Memory Allocation in C and C++
- Secure Coding Best Practices for Memory Allocation in C and C++
- Secure Coding Best Practices for Memory Allocation in C and C++
- Secure Coding Best Practices for Memory Allocation in C and C++
- jQuery and JavaScript Coding: Examples and Best Practices
- Flex SDK coding conventions and best practices(官方)
- C Coding Standards
- 《Recommended C Style and Coding Standards》学习总结
- Ajax Patterns and Best Practices
- JSP Examples and Best Practices
- Achievements and Gamerscore: Best Practices
- WCF Coding Standards - Operations and Calls
- 将XSD文件中分开的元素,合并在一起
- svn
- Vxworks 的查看修改内存
- 介绍锁定在SQL服务器
- rabbitmq源码分析(一)
- C Sharp Coding Standards and Best Programing Practices
- 从此以后,写代码不能再是搭鸡窝
- atmel9260上linux socketcan MCP2515调试笔记
- ISAPI精彩编程
- 关于Java读取xml文件的学习
- PB调用动态库参数为char*时的问题
- 关于wired memory
- 设计模式-结构型模式-解释器
- volatile原理