英文:
Is it okay to create constant variables just for the sake of readability?
问题
以下是翻译好的内容:
假设我有这段代码:
System.out.println("产品类型是:\n"+
"1. 国内产品。\n"+
"2. 国际产品。");
int choice = input.nextInt(System.in);
if (choice == 1)
(...)
else if (choice == 2)
(...)
那么以下做法是否可以呢?
final int NATIONAL = 1;
final int INTERNATIONAL = 2;
System.out.println("产品类型是:\n"+
"1. 国内产品。\n"+
"2. 国际产品。");
int choice = input.nextInt(System.in);
if (choice == NATIONAL)
(...)
else if (choice == INTERNATIONAL)
(...)
我不太确定,我刚刚购买了《代码整洁之道》这本由Bob大叔编写的书,我开始对自己产生了疑问。
英文:
Imagine if I have this piece of code:
System.out.println("Is the product:\n"+
"1. National.\n"+
"2. International.");
int choice = input.nextInt(System.in);
if (choice == 1)
(...)
else if (choice == 2)
(...)
So is it okay to do the following?
final int NATIONAL = 1;
final int INTERNATIONAL = 2;
System.out.println("Is the product:\n"+
"1. National.\n"+
"2. International.");
int choice = input.nextInt(System.in);
if (choice == NATIONAL)
(...)
else if (choice == INTERNATIONAL)
(...)
I don't know, I just bought the Clean Code book by Uncle Bob and I started to question myself.
答案1
得分: 2
我相信使用常量比使用“魔法”数字要更好。
通过使用常量,你可以在一个地方控制定义并进行更好的命名。这将影响代码的进一步可维护性。
在某些情况下,尝试使用enum
而不是常量。Enum
相比常量有更多优势。
在这种情况下,enum
示例类似于以下代码:
enum UserInput {
NATIONAL(1), INTERNATIONAL(2), UNKNOWN(-1);
private int input;
public int getInput() {
return input;
}
UserInput(int i) {
this.input = i;
}
public static UserInput getUserInput(int input) {
for (UserInput userInput: UserInput.values()) {
if (userInput.getInput() == input) {
return userInput;
}
}
return UNKNOWN;
}
}
//main
public static void main(String[] args) {
System.out.println("产品属于哪种类型:\n"+
"1. 国内。\n"+
"2. 国际。");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
switch (UserInput.getUserInput(choice)) {
case NATIONAL: break;
case INTERNATIONAL: break;
default:
}
}
了解更多信息,请查看:https://stackoverflow.com/questions/11575376/why-use-enums-instead-of-constants-which-is-better-in-terms-of-software-design
英文:
I believe constant is better than the magic
number.
With constant, you control the definition in one place and better naming. It'll affect your further maintainability of the code.
And try using enum
instead of constant in some situations. Enum
has more pros than constant.
In this case enum example is similar to the below code:
enum UserInput {
NATIONAL(1), INTERNATIONAL(2), UNKNOWN(-1);
private int input;
public int getInput() {
return input;
}
UserInput(int i) {
this.input = i;
}
public static UserInput getUserInput(int input) {
for (UserInput userInput: UserInput.values()) {
if (userInput.getInput() == input) {
return userInput;
}
}
return UNKNOWN;
}
}
//main
public static void main(String[] args) {
System.out.println("Is the product:\n"+
"1. National.\n"+
"2. International.");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
switch (UserInput.getUserInput(choice)) {
case NATIONAL: break;
case INTERNATIONAL: break;
default:
}
}
check is for more: https://stackoverflow.com/questions/11575376/why-use-enums-instead-of-constants-which-is-better-in-terms-of-software-design
答案2
得分: 0
当您希望对某些变量(常量)或代码进行多次引用时,为了更好的可读性和理解,您可以创建常量。
例如:
如果(choice == NATIONAL)
(...)
否则,如果(choice == INTERNATIONAL)
(...)
当您需要多次使用INTERNATIONAL和NATIONAL时,这样做是正确的。
英文:
when you want to Some variable(constants) or Code multiple, then you make constants for better readablity and understanding.
For Example -:
if (choice == NATIONAL)
(...)
else if (choice == INTERNATIONAL)
(...)
is right when you have to use INTERNATIONAL and NATIONAL multiple times
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论