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);
}
/**@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;
}
}
private void addCompontnt(Component c, int x, int y, int w,int h){
c.setBounds(x,y,w,h);
this.add(c);
}