首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

Java 编程规范的有关问题, 高质量代码的困扰~ 请高手或有相关方面经验的大牛看看

2013-10-24 
Java 编程规范的问题, 高质量代码的困扰~ 请高手或有相关方面经验的大牛看看本帖最后由 DuanKong86 于 201

Java 编程规范的问题, 高质量代码的困扰~ 请高手或有相关方面经验的大牛看看
本帖最后由 DuanKong86 于 2013-10-23 10:41:10 编辑 Summary:
我们有一个系统可以检查代码的规范性等,有两个rule我一直过不去, 而且也不懂是什么意思,
哪位高手帮我看看, 指点一二也好.
第一个:
Name
Avoid Artifacts with High Fan-Out
Rationale
The higher the number of referenced Artifacts, the more difficult the maintenance and evolution as all updates in referenced Artifacts will have to be tested and taken into account.
Description
Avoid Artifacts with High Fan-Out (Fan-Out > X). The Fan-out of an Artifact is the number of other Artifacts that are referenced in it. When computing the Fan-Out of an Artifact, multiple accesses to the same component of an Artifact are counted as one access. The threshold is a parameter and can be changed at will.
The Fan-Out computed by CAST takes into account dependency resulting from polymorphism and thus depends on the number of sub-classes of the objects referenced in the artifact.
Remediation
Reduce the number of referenced Artifacts
Output
This report lists all Artifacts with a Fan-Out greater than X. It provides the following information: Artifact full name, Fan-Out value
Total
Number of Artifacts
Violation 示例:

/**
 * @param void
 * @return void
 * */
private void initComponent() {
ta.setAutoscrolls(true);
ta.setEditable(false);

scrollPane = new JScrollPane(ta);
scrollPane.setBounds(2, 2, 690, 400);
this.add(scrollPane);

btn.setText("Start");
btn.setBounds(280, 420, 150, 30);
this.add(btn);
TextAreaAppander.setTa(ta);
}

这个方法只在Frame的构造函数里调用了,用于初始化frame上的components
----------------------------------------------------------------
第二个:
Name 
Avoid Packages with High Efferent Coupling (CE)
Rationale
Excessive coupling is detrimental to modular design since classes are not independent. A large efferent coupling indicates that a class is unfocussed and may also indicate that it is unstable, since it depends on the stability of all the types to which it is coupled. This prevents reuse since a high coupling possibly indicates a package is poorly designed and difficult to understand/maintain. Extracting classes from the original class so the class is decomposed into smaller classes can reduce efferent coupling, this improves modularity and promotes encapsulation.
Description
CE (also known as Outgoing Dependencies or the Number of Types outside a Package that Types of the Package Depend on) indicates the number of other packages that classes and interfaces in the analyzed package depend upon. This is an indicator of the package's independence.
The threshold contains the maximum efferent coupling.
Output
This report all classes ( and interfaces ) of a package with a high efferent Coupling.
It provides the following information:
- Class (Or Interface)
- CE 
Violation 示例:
出错的, 不是指向某方法, 而是指向某个类.
/**@author Ken*/
public class JobManager {

/**job configuration file*/
private static final String PROP_JOB_XML = PropertiesManager.getJobConfigFile();

/**
 * @param void
 * @return void
 * */
private JobManager() {

}
/**
 * get file list of a directory 
 * @param dirName the directory name that contains excels you want to handle
 * @return List<String>
 * */
public static List<String> getFileList(String dirName) {
List<String> list = new ArrayList<String>();

if (!dirName.endsWith(File.separator)) {
dirName = dirName + File.separator;


}

File dirFile = new File(dirName);
if (!dirFile.exists() || (!dirFile.isDirectory())) {
BHCLogger.LOG.error("can't find directory, dirName="+dirName);
return list;
}

File[] files = dirFile.listFiles();

for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
list.add(files[i].getAbsolutePath());
}
}
return list;
}
/**
 * filter file by suffix
 * @param fileNameList a list of filepath
 * @param suffix for now, it should be '.xls'
 * @return List
 * */
public static List<String> filterFile(List<String> fileNameList, String suffix) {
List<String> list = new ArrayList<String>();
for(String fileName : fileNameList) {
if(fileName.endsWith(suffix)) {
list.add(fileName);
}
}
return list;
}
/**
 * load jobs from job.xml
 * @param void
 * @return List
 * */
@SuppressWarnings("unchecked")
public static List<AbstractJob> loadJobs() {
List<AbstractJob> list = new ArrayList<AbstractJob>(0);
SAXReader saxReader = new SAXReader();
Document document = null;
try {
document = saxReader.read(PROP_JOB_XML);
} catch (DocumentException e) {
BHCLogger.LOG.error("DocumentException occurs, filename="+PROP_JOB_XML);
}
if(document==null) {
BHCLogger.LOG.error("can't find resource, filename="+PROP_JOB_XML);
return list;
}
Element root = document.getRootElement();
List<Element> elements = root.elements();
for (Element element : elements) {
List<Element> jobElements = element.elements();
AbstractJob job = null;
for (Element jobElement : jobElements) {
if ("class".equalsIgnoreCase(jobElement.getName())) {
try {
Class<?> jobClass = Class.forName(jobElement.getStringValue());
//update if else, because of [Avoid using Dynamic instantiation]
if("ibm.bhc.business.JobIsFeatureCodeActive".equals(jobClass.getName())) {
job = new JobIsFeatureCodeActive();
}
else if("ibm.bhc.business.JobIsHaveOSLEVELRelation".equals(jobClass.getName())) {
job = new JobIsHaveOSLEVELRelation();
}
else if("ibm.bhc.business.JobIsFCHaveOtherTypeRelation".equals(jobClass.getName())) {
job = new JobIsFCHaveOtherTypeRelation();
}
else {
job = null;
BHCLogger.LOG.error("Class has not be handled, className="+jobClass.getName());
}
} catch (ClassNotFoundException e) {
BHCLogger.LOG.error("Class not found, clasname="+jobElement.getStringValue());
}
}
else if ("sql".equalsIgnoreCase(jobElement.getName())) {
if(job!=null) {
job.setSql(jobElement.getStringValue());
}
else {
BHCLogger.LOG.error("xml format error : <sql> must after <class> in "+PROP_JOB_XML);
}
}
}
list.add(job);
}
return list;
}
}


求哪位好心人指点~  只有40分,  不好意思

拜谢!!!!

java 规范 高质量
[解决办法]
第一个感觉是魔法数字,下面的改成静态的试试看,不要直接写数字。
       scrollPane.setBounds(2, 2, 690, 400);
        btn.setBounds(280, 420, 150, 30);
第二个:应该申明接口,让调用方用接口调用,而不是直接指向类,面向接口编程。
[解决办法]
查了一下Fan-Out.
按照http://blog.csdn.net/runningya/article/details/5869749这哥们的说法,
你在init方法里面调用了过多的底层代码。

我怀疑,你在这个方法里大量重复性地调用了Component的setBounds()和add()方法。
建议添加一个方法把这段封装一下。比如:

private void addCompontnt(Component c, int x, int y, int w,int h){
c.setBounds(x,y,w,h);
this.add(c);
}

上面代码类型和命名可能有错。

然后再init方法里面调用这个。

如果这样要是能解决,大概就是说代码检查工具认为你封装的不够,呵呵。

热点排行