阅读了一些jvm代码,仔细斟酌了一些大神的命名习惯和注释规范,有人可能觉得我太蛋疼了,当我觉得有规范比自己乱来一同要好的多
我在这里记录一下,只是习惯问题,并不影响阅读,大家可以自己取舍的学习一下
这里还有一篇好文,偶然看到的,一语就道中了java与c++程序员风格上的差别
http://www.ibm.com/developerworks/java/library/j-noaccent/index.html
1.java文件的内容顺序一般为
/*
* Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.awt;
import java.awt.image.Raster;
class GradientPaintContext implements PaintContext {}
版权声明,包,依赖类,类
2.过长的话,要换行,比上一行多一个缩进,从=开始
static ColorModel xrgbmodel =
new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff);
还有一种是参数过长,要把参数对齐,最后用“,”结尾
public GradientPaintContext(ColorModel cm,
Point2D p1, Point2D p2, AffineTransform xform,
Color c1, Color c2, boolean cyclic)
3.关键字前后要有空格
if (ulenSq <= Double.MIN_VALUE) {
dx = 0;
dy = 0;
} else {
4.参数时,”,“后要加空格,运算符前后加空格
static synchronized void putCachedRaster(ColorModel cm, Raster ras) {
if (cached != null) {
5.方法内一般为行注释,其他供javadoc一般是块注释,注意行注释//后要追加一个空格
} else {
// We are acyclic
if (dx < 0) {
// If we are using the acyclic form below, we need
// dx to be non-negative for simplicity of scanning
// across the scan lines for the transition points.
// To ensure that constraint, we negate the dx/dy
// values and swap the points and colors.
Point2D p = p1; p1 = p2; p2 = p;
6.类型转换括号也要加空格
Raster ras = (Raster) cached.get();
7.当局部变量太多是,为了让类属性更加突出,不会混淆,加个this
Point2D p = p1; p1 = p2; p2 = p;
Color c = c1; c1 = c2; c2 = c;
dx = -dx;
dy = -dy;
}
}
}
Point2D dp1 = xform.transform(p1, null);
this.x1 = dp1.getX();
this.y1 = dp1.getY();
this.cyclic = cyclic;
8.逻辑链接时,为了让逻辑显得更清楚,可以换行
if (ras != null &&
ras.getWidth() >= w &&
ras.getHeight() >= h)
9.参数传递时,复杂对象可以用缩写,用最少的字表达最多的意思,简单对象(int,double,string,char)一般为全名
public GradientPaintContext(ColorModel cm,
Point2D p1, Point2D p2, AffineTransform xform,
Color c1, Color c2, boolean cyclic)
10.尽可能的在需要使用是才声明,把使用和定义放到一个语句里面 int a = 123;
double colrel = rowrel;
int j = w;
if (colrel <= 0.0) {
int rgb = interp[0];
do {
pixels[off++] = rgb;
colrel += dx;
} while (--j > 0 && colrel <= 0.0);
}
while (colrel < 1.0 && --j >= 0) {
pixels[off++] = interp[(int) (colrel * 256)];
colrel += dx;
}