哪个更好?将整个内容作为字符串传递,还是字符串连接?

发布于 2024-09-15 17:48:41 字数 1693 浏览 3 评论 0原文

你好,我有 20 个字符串,除了类名之外,每个字符串都有相同的包结构。这些字符串需要根据需要传递给方法。请参阅下面的代码:

public static final String RECENT_MSG_        = "com.foo.xxs.RecentMessage";
public static final String PROJ_              = "com.foo.xxs.Proj";
public static final String FORECAST           = "com.foo.xxs.Forecase";
public static final String REQUEST            = "com.foo.xxs.Request";
public static final String UNAPPROVED         = "com.foo.xxs.UnApproved";
public static final String UNPOSTED           = "com.foo.xxs.Unposeted";
public static final String VACANT             = "com.foo.xxs.Vacant";
public static final String ORG_VIOL           = "com.foo.xxs.OrgViolation";
public static final String ORG_WARN           = "com.foo.xxs.OrgWarning";
public static final String EMP_VIOL           = "com.foo.xxs.EmpViolation";
public static final String EMP_WARN           = "com.foo.xxs.EmpWarning";    
public static final String TS_WARN            = "com.foo.xxs.TSWarn";
public static final String TS_VIOL            = "com.foo.xxs.TSViolation";
public static final String AGE_GROUP          = "com.foo.xxs.AgeGroup";


private void rescheduleTasks(long _taskType,String value)
{
    if(_taskType == 1000 &&(_sSchedTaskMgr.getInstance().getCurrentScheduledTaskInfo(RECENT_MSG_)==null))
    {

    // do something     
    }

}

也可以按如下方式完成:

public static final String RECENT_MSG_        = "RecentMessage";
public static final String PACK                       ="com.foo.xxs."

并像这样连接字符串:

if(_taskType == 1000 &&(_sSchedTaskMgr.getInstance().getCurrentScheduledTaskInfo(PACK+RECENT_MSG_)==null))

哪个会更好?

Hi, I have 20 strings, each of which will have same package structure except for the class name. These strings need to be passed to method as required. Refer to the code below:

public static final String RECENT_MSG_        = "com.foo.xxs.RecentMessage";
public static final String PROJ_              = "com.foo.xxs.Proj";
public static final String FORECAST           = "com.foo.xxs.Forecase";
public static final String REQUEST            = "com.foo.xxs.Request";
public static final String UNAPPROVED         = "com.foo.xxs.UnApproved";
public static final String UNPOSTED           = "com.foo.xxs.Unposeted";
public static final String VACANT             = "com.foo.xxs.Vacant";
public static final String ORG_VIOL           = "com.foo.xxs.OrgViolation";
public static final String ORG_WARN           = "com.foo.xxs.OrgWarning";
public static final String EMP_VIOL           = "com.foo.xxs.EmpViolation";
public static final String EMP_WARN           = "com.foo.xxs.EmpWarning";    
public static final String TS_WARN            = "com.foo.xxs.TSWarn";
public static final String TS_VIOL            = "com.foo.xxs.TSViolation";
public static final String AGE_GROUP          = "com.foo.xxs.AgeGroup";


private void rescheduleTasks(long _taskType,String value)
{
    if(_taskType == 1000 &&(_sSchedTaskMgr.getInstance().getCurrentScheduledTaskInfo(RECENT_MSG_)==null))
    {

    // do something     
    }

}

This can also be done as follows:

public static final String RECENT_MSG_        = "RecentMessage";
public static final String PACK                       ="com.foo.xxs."

And concatenating the strings like so:

if(_taskType == 1000 &&(_sSchedTaskMgr.getInstance().getCurrentScheduledTaskInfo(PACK+RECENT_MSG_)==null))

Which one would be better?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿看小鸭子会跑 2024-09-22 17:48:41

它们将具有相同的性能 - 连接将在编译时而不是执行时执行,因为两个部分都是常量。诚然,原始版本中常量池中的字符串会更少,但这不太可能产生影响。

您觉得哪个更具可读性?我不能说它对我有什么好处 - 我不喜欢第一种形式的重复,但同样我不确定我是否想要在任何地方连接。

另一种选择是:

public static final String PACK               = "com.foo.xxs."
public static final String RECENT_MSG_        = PACK + "RecentMessage";

etc - 因此您可以在常量声明处执行串联。然后,您可以按照第一个片段在代码中使用 RECENT_MSG_ ,但避免按照第二个片段重复“com.foo.xxs”。

编辑:您可能要考虑的另一个选项是使用枚举。

They will have the same performance - the concatenation will be performed at compile time rather than execution time as both parts are constants. There will be fewer strings in the constant pool in the original version, admittedly - but that's unlikely to make a difference.

Which do you find more readable? I can't say there's much in it for me - I dislike the repetition of the first form, but equally I'm not sure I'd want to concatenate everywhere.

Another alternative is:

public static final String PACK               = "com.foo.xxs."
public static final String RECENT_MSG_        = PACK + "RecentMessage";

etc - so you perform the concatenation at the point of the constant declaration. Then you can just use RECENT_MSG_ in the code, as per the first snippet, but avoid the "com.foo.xxs" duplication as per the second.

EDIT: Another option you may want to consider is using an enum.

寂寞陪衬 2024-09-22 17:48:41

我会选择第一个版本,你只是让读者更容易立即看到字符串的含义以及你所引用的类。此外,如果您想要从不同的命名空间引入一个类,您将能够这样做。

相比之下,第二个版本引入了一些需要读者首先解释的逻辑。

如果您选择第二个版本,请使用 Jon 的替代方案,这样至少您仍然可以选择从其他名称空间引入类。

I would go for the first version, you just make it easier for a reader to immediately see what the strings mean and what classes you're referring too. In addition, should you ever want to introduce a class from a different namespace, you'll be able to do so.

The second version, in contrast, introduces some logic which needs to be interpreted by a reader first.

If you go for the second version, use Jon's alternative instead, so that at least you still have the option to introduce classes from other namespaces.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文