【发布时间】:2020-05-30 05:58:32
【问题描述】:
想象一下,如果我有这段代码:
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)
(...)
那么可以进行以下操作吗?
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)
(...)
我不知道,我刚买了 Bob 叔叔的 Clean Code 书,我开始质疑自己。
【问题讨论】:
-
为什么会不好?在我看来,这取决于上下文。在你的例子中它是完美的。如果您有 37 像素的边距,我会感到困惑。
-
好吧,IDK,如果它消耗更多的内存或任何东西。
-
是的,没关系。确实,很多人都推荐这是一种很好的做法。
标签: java coding-style