代码书写有很多需要注意的问题,其中最重要的莫过于代码的规范性。一般按照规范书写的代码往往使人赏心悦目,更能提高代码的可阅读性。
一: 命名方法的选择
代码的命名是一个代码规范中非常重要的一个方面,其中也流行着很多种方法,想匈牙利命名法,驼峰法等等。关于驼峰法,我个人认为驼峰法不是一个十分简明的命名方法,其变量复杂的构造往往让人不知所云。今天这篇博客里采用驼峰法,驼峰法错落有致,非常有层次感,看起来也不会像匈牙利命名法那么累,也是目前最流行的命名法之一。
二: 代码中的命名规范
命名方法可以采用大小驼峰法共同使用的方式。
小驼峰法:骆驼式命名法就是当变量名或函式名是由一个或多个单字连结在一起,而构成的唯一识别字时,第一个单词以小写字母开始;第二个单词的首字母大写或每一个单词的首字母都采用大写字母,例如:myFirstName、myLastName,这样的变量名看上去就像骆驼峰一样此起彼伏,故得名。(来自百度百科)
大驼峰法:相比小驼峰法,大驼峰法把第一个单词的首字母也大写了 如 MyFristName
1: 变量一般用小驼峰法
2: 类名 函数名 命名空间
下面看一下
<span style="font-size:18px;"><span style="font-size:18px;">#include <iostream>
#include <string>
using std::string;
class StudentInformation
{
private:
string stuName;
string stuAddress;
int stuID;
float stuEnglishScore;
float stuChineseScore;
float stuMathScore;
const int STUAGE; /* 学生年龄设置为const类型 */
public:
StudentInformation():STUAGE(20)
{
}
string GetStudentName() { }
string GetStudentAddress() { }
int GetStudentId() {}
};</span></span>
三: 大括号的放置
大括号的放置一般有两种方法,一种是悬挂法,一种是尾部法。
悬挂法:每个大括号各占一行,两个相关的大括号与大括号的父语句对齐。请看示例
<span style="white-space:pre"> </span>System.out.println("Welcome to huixingzhen ");
boolean con = true;
while(con)
{
System.out.print("Please input the size of the array: ");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.close();
int[][] array = InitArray(n);
System.out.println("Print: ");
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
System.out.printf("%-5d",array[i][j]);
}
System.out.println();
System.out.println();
}
con = IsContinue();
}
请注意大括号的位置关系,这种方法的好处是使得逻辑关系看起来清晰易懂,很好看。
尾部法:第一个大括号在其父语句的末尾,第二个大括号单独占据一行,并与父语句的开始位置对齐。请看示例
<span style="font-size:18px;"><span style="font-size:18px;">private static int[][] InitArray(int n){
<span style="white-space:pre"> </span>int count = 0;
int[][] ary = new int[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
ary[i][j] = 0;
}
}
for(int num = 1; num < n / 2 + 2; num ++){
for(int i = num - 1; i < n - num + 1; i++){
ary[num - 1][i] = ++ count;
}
for(int i = num; i < n - num + 1; i++){
ary[i][n - num ] = ++ count;
}
for(int i = n - num ; i >= num ; i--){
ary[n - num][i - 1] = ++count;
}
for(int i = n - num; i > num ; i--){
ary[i - 1][num - 1] = ++count;
}
}
return ary;
}</span></span>
尾部法是Eclipse默认提供的方法,我个人习惯上更喜欢悬挂法。现在的编译器都很智能,像Eclipse,只需要一个快捷键组合便能在悬挂法和尾部法之间组合。无论哪种方法,中心思想是统一,代码里面不能既有这种方法又有哪种方法 ,这样的话看起来也很乱,很烦。建议使用悬挂法。
四: 注释的书写
注视不可不写,最差的代码就是没有注释的代码,但是注释也不可多写。注释过少可能增加代码的阅读难度,注释过多也会让代码变得繁琐从而难以阅读。写代码的时候要明白,代码本身就是最好的注释,所以最优秀的代码几乎没有注释。
常见的注释方法有两种,一种是双斜杠 // ,一种是一对单斜杠加上星号 /* */。建议使用/* */
1:类的注释 一般写在类前 表明一些基本信息
<span style="font-size:18px;"><span style="font-size:18px;">/*
* 作者:xusworld
* 时间:2015/3/11
* 功能:输出一个回形矩阵
* */
public class HuiXingJuZhen
{</span></span>
<span style="font-size:18px;"><span style="font-size:18px;"><span style="white-space:pre"> </span>/*内容省去*/</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">}</span></span>
2:函数的注释 写在函数名之前 表明一些基本信息
<span style="font-size:18px;"><span style="font-size:18px;">/*</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">* 作者:
* 时间:</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">* 功能:判断是否继续
*
* */
private static boolean IsContinue()
{</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">}</span></span>
3:变量的注释 对于一些特别重要的变量需要注释的可以将注释写在变量后
int tmpNumber;/*声明临时变量 记录数目*/
五: 美观
在基本的规范上完成了之后,如何让代码变得更加美观,是一个很需要思考的问题。我在这里写一下自己平时经常使用的技巧。
1:数据和运算符之间要有一个空格;
<span style="font-size:18px;"><span style="font-size:18px;">int i=0;
for(int i=0;i<5;i++)
int Sum(int x=0, int y=0)
/* 看起来太紧凑 */
int i = 0;
for(int i = 0;i < 5; i++)
int Sum(int x = 0, int y = 0)
/* 看起来挺好 */</span></span>
2:每个功能段完成之后要有一个空行,这个就不举例了;
3:函数不要写的太长;
4:自己耐心寻找。
总结
写代码当然不仅仅是这些,很多都需要自己总结,这都是个人意见,欢迎批评指正,沟通交流。http://blog.csdn.net/u012706792