【发布时间】:2013-07-03 13:15:44
【问题描述】:
我有一个调用我的支持 bean 的复合组件。这里我传递一些图像的参数:
<h:outputText value="#{MyBBean.formRequest(
cc.attrs.type,
cc.attrs.orientation,
cc.attrs.title,
cc.attrs.width,
cc.attrs.height,
cc.attrs.xlabel,
cc.attrs.ylabel,
cc.attrs.value)}"
escape="false" />
方法如下
public boolean formRequest(
final String type,
final String orientation,
final String title,
final String width,
final String height,
final String xlabel,
final String ylabel) {
// some actions here
if (height != null) {
appendAttribute(HEIGHT_ATTRIBUTE_NAME, height, request);
}
if (width != null) {
appendAttribute(WIDTH_ATTRIBUTE_NAME, width, request);
}
// the like
if (ylabel != null ) {
appendAttribute(Y_LABEL_ATTRIBUTE_NAME, ylabel, request);
}
// other actions
return request.toString();
}
这里是 appendAttribute 方法
private void appendAttribute(final String attributeName,
final String attributeValue, final StringBuilder builder) {
builder.append(attributeName);
builder.append(EQUALS_CHAR);
builder.append(attributeValue);
builder.append(AMPERSAND_CHAR);
}
您对重构这些未嵌套的 if 语句有任何想法吗?因为,就我而言,它闻起来很臭
【问题讨论】:
标签: java refactoring conditional statements