敏捷开发中如何将注释转换为代码
http://tech.ddvip.com 2008年01月22日 社区交流
内容摘要:本文讲述了敏捷开发中将注释转换为代码的必要性,需要注意的问题和具体的实例源代码,供大家参考!
void loadInfoFromDB(String participantId) {
this.participantId = participantId;
getParticipantFullNames(); //取得参会者的全名,注意,我们已经将注释去掉了
//***********************
//取得参会者所在部门和国家
//***********************
//取得参会者被雇佣部门ID
OrganizationsInDB orgsInDB = OrganizationsInDB.getInstance();
String oid = orgsInDB.getOrganization(participantId);
if (oid != null) {
Organization org = orgsInDB.locateOrganization(oid);
engOrgName = org.getEName();
chiOrgName = org.getCName();
engOrgCountry = org.getEAddress().getCountry();
chiOrgCountry = org.getCAddress().getCountry();
}
}
void getParticipantFullNames() {
ParticipantsInDB partsInDB = ParticipantsInDB.getInstance();
Participant part = partsInDB.locateParticipant(participantId);
if (part != null) {
//取得参会者的英文全名
engFullName = part.getELastName() + ", " + part.getEFirstName();
//取得参会者的中文全名
chiFullName = part.getCLastName()+part.getCFirstName();
}
}
此外,还有一个注释“取得参会者所在部门和国家”也是可以重构在方法名里面的:
void loadInfoFromDB(String participantId) {
this.participantId = participantId;
getParticipantFullNames();
getOrgNameAndCountry(); //又抽取掉一个注释
}
void getParticipantFullNames() {
ParticipantsInDB partsInDB = ParticipantsInDB.getInstance();
Participant part = partsInDB.locateParticipant(participantId);
if (part != null) {
//取得参会者的英文全名
engFullName = part.getELastName() + ", " + part.getEFirstName();
//取得参会者的中文全名
chiFullName = part.getCLastName()+part.getCFirstName();
}
}
void getOrgNameAndCountry() {
OrganizationsInDB orgsInDB = OrganizationsInDB.getInstance();
//取得参会者被雇佣部门的ID
String oid = orgsInDB.getOrganization(participantId);
if (oid != null) {
Organization org = orgsInDB.locateOrganization(oid);
engOrgName = org.getEName();
chiOrgName = org.getCName();
engOrgCountry = org.getEAddress().getCountry();
chiOrgCountry = org.getCAddress().getCountry();
}
}
抽取出方法放于另一个类
作者:王伟杰 责编:豆豆技术应用