日期:2014-05-20  浏览次数:20687 次

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;