敏捷开发中如何将注释转换为代码

http://tech.ddvip.com   2008年01月22日    社区交流

内容摘要:本文讲述了敏捷开发中将注释转换为代码的必要性,需要注意的问题和具体的实例源代码,供大家参考!

  请看下面这两个注释:

public class ParticipantInfoOnBadge {
...
void getParticipantFullNames() {
ParticipantsInDB partsInDB = ParticipantsInDB.getInstance();
Participant part = partsInDB.locateParticipant(participantId);
if (part != null) {
//取得参会者的英文全名.
engFullName = part.getELastName() + ", " + part.getEFirstName();
//取得参会者的中文全名
chiFullName = part.getCLastName()+part.getCFirstName();
}
}
}

  因为程序员觉得这些代码片段还不够清楚,所以还是要用注释来解释它们。但这次移除注释时,我们会将抽取出来的方法放到Participant这个类里面,而不是ParticipantInfoOnBadge里了。

public class ParticipantInfoOnBadge {
...
void getParticipantFullNames() {
ParticipantsInDB partsInDB = ParticipantsInDB.getInstance();
Participant part = partsInDB.locateParticipant(participantId);
if (part != null) {
engFullName = part.getEFullName(); //将职责交给domain自己,也就是Participant
chiFullName = part.getCFullName();
}
}
}
public class Participant {
String getEFullName() {
return getELastName() + ", " + getEFirstName();
}
String getCFullName() {
return getCLastName() + getCFirstName();
}
}

  用注释去命名一个已经存在的方法

  请看下面的注释,这也是本例的最后一个注释了:

public class ParticipantInfoOnBadge {
...
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();
}
}
}

  之所以要用这个注释“取得参会者被雇佣部门的ID”,是因为这个方法名“getOrganization”还不够清楚,所以,我们将注释表达的信息放在方法名里面,如下:

作者:王伟杰    责编:豆豆技术应用

正在加载评论...