更新时间:2023-02-22 20:46:37
(不是答案,但有一些代码注释.)
(Not an answer, but some code notes.)
1)名称函数使用小写字母,例如:
1) Name functions with a lower case letter, e.g.:
public String login() { ... }
2)使用规范Java来检查布尔值,例如:
2) Use canonical Java for checking booleans, e.g.:
if (!isValidUser) { ... }
3)使用正确命名规范代码的变量名称,例如:
3) Use variable names that make canonical code "read" properly, e.g.:
if (!validUser) { ... }
4)进行字符串比较时,无需进行空检查,例如:
4) Remove the need for null checks when doing string compares, e.g.:
if ("de".equals(getLang()) { ... }
另外,我不清楚[code> setLang()是否只是用 getLang()
检索的内容的设置器;这有点令人困惑,并且在没有进一步知识的情况下使得那段代码有点奇怪.
Also, it's not clear to me if setLang()
is just a setter for what's retrieved with getLang()
; that's a little confusing and makes that chunk of code kind of weird without further knowledge.
5)不要将有效的用户代码放在相同的条件中;如果它是无效用户,则将其视为特殊情况并返回.然后将有效的用户代码与保护子句区分开,例如:
5) Don't put the valid user code in the same conditional; if it's an invalid user, treat it as a special case and return. The valid user code is then differentiated from the guard clause, e.g.:
if (!validUser) {
// actionMessage stuff.
return ERROR;
}
session = ActionContext.getContext().getSession();
// The rest of valid user code.
6)将布尔值用于布尔值,例如:
6) Use booleans for booleans, e.g.:
session.put("loggedIn", true);
7)对范围变量使用常量,例如:
7) Use constants for scoped variables, e.g.:
session = ActionContext.getContext().getSession();
session.put(SessionKeys.LOGGED_IN, true);
8)使用 SessionAware
,它使测试更加容易;那么 session
只是一个动作属性:
8) Use SessionAware
, it makes testing easier; session
is then just an action property:
session.put(SessionKeys.LOGGED_IN, true);
9)当它是完全不同的东西时,不要命名 session1
...在这种情况下,它甚至也不是什么不同,因此您要在其中输入相同的信息两次.
9) Don't name something session1
when it's a completely different thing... and in this case, it isn't even a different thing, so you're putting the same information in twice.
10)访问动作属性时要保持一致;不要使用 setXxx()
和 this.xxx
.在大多数情况下,您仍然可以只使用 xxx
.
10) Be consistent when accessing action properties; don't use setXxx()
and this.xxx
. Most of the time you can just use xxx
anyway.
setCountryList(applicationManager.getCountries()); // Or, my preference in most cases:
countryList = appManager.getCountries();
11)请勿按变量类型命名,除非在需要特别区分的情况下,例如:
11) Don't name variables by their type, except in situations where you need to specifically differentiate, e.g.:
countries = appManager.getCountries();
这给我们带来了更易于管理的地方:
This leaves us with a more-manageable:
public String Login() {
if (!appManager.isValidUser(username, password)) {
handleInvalidUser();
return ERROR;
}
session.put(SessionKeys.LANG, lang);
session.put(SessionKeys.LOGGED_IN, true);
session.put(SessionKeys.USER_NAME, username);
session.put(SessionKeys.USER_PASSWORD, password);
countries = appManager.getCountries();
return SUCCESS;
}