- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
private String getMessage(String prop, boolean suffixEnabled) {
String title = null;
if (prop.equals("headerTitle.suffix")) {
try {
title = messageSource.getMessage("headerTitle.suffix", null, locale);
} catch (NoSuchMessageException e) {
//e.printStackTrace();
}
if (title == null)
title = "";
} else {
try {
title = messageSource.getMessage(prop, null, locale);
if (suffixEnabled)
title += " " + messageSource.getMessage("headerTitle.suffix", null, locale);
} catch (NoSuchMessageException e) {
//e.printStackTrace();
}
if (title == null) {
try {
title = messageSource.getMessage("headerTitle.default", null, locale);
} catch (NoSuchMessageException ex) {
title = "";
}
}
}
return title;
}
2. Слишком много копипаста.
с флагом 50/50. метод приватный, значит используется для какого-то юзкейса внутри класса скорее всего.
[code=java]
// литералы в константы
private static final String HEADER_SUFFIX_PROP = "headerTitle.suffix";
private static final String DEFAULT_TITLE_PROP = "headerTitle.default";
//.....
private String getMessage(String prop, boolean suffixEnabled) {
StringBuilder title = new StringBuilder(getMessageOrEmptyString(pr op));
if (title.isEmpty()) {
title.append(getMessageOrEmptyString(DEF AULT_TITLE_PROP));
} else if (suffuxEnabled && !HEADER_SUFFIX_PROP.equals(prop)) {
title.append(" ");
title.append(getMessageOrEmptyString(HEA DER_SUFFIX_PROP));
}
return title.toString();
}
private String getMessageOrEmptyString(String prop) {
try {
return messageSource.getMessage(prop, null, locale);
} catch (NoSuchMessageException e) {
// добавить warning лог о несуществующем мессадже
return "";
}
}
[code]
Еще одна конструкция if.
Я имел ввиду ситуацию getMessage("headerTitle.suffix", false)
Ваш код вернет дефолтный заголовок, если значение окажется пустым, а исходный код вернет пустое значение.