简体   繁体   English

这种方法有什么问题?

[英]what's wrong with this approach?

A new Code Review process has been put in place and now my team must not ever declare a string as a local variable, or the commit won't pass the code review. 新的代码审查流程已经到位,现在我的团队不能将字符串声明为局部变量,否则提交将不会通过代码审查。 We are now to use constants instead. 我们现在要使用常量。

So this is absolutely not allowed, even if we're dead sure the string will never be used in any other place 所以这绝对是不允许的,即使我们确定字符串永远不会在任何其他地方使用

String operationId = "create"; 

This is what should be used instead: 这应该是应该使用的:

private static final String OPERATION_ID = "create";

While I totally agree to use constants for strings that appears +2 times in the code ... I just find it overkill to completely not have the ability to declare a string in place if it's used only once. 虽然我完全同意在代码中出现+2次的字符串使用常量...但我发现如果它只使用一次就完全没有能力声明字符串。

Just to make sure it's clear, all the following are NOT ALLOWED under any circumstances: 为了确保清楚, 在任何情况下不允许以下所有内容:

  • String div = "div1";
  • Catch(Exception ex){ LOGGER.log("csv file is corrupt") }
  • String concatenation String str = "something ...." + someVar + "something" ... we are to replace someVar with %s , declare whole thing as a global string, and then later use String.format(....) 字符串连接String str = "something ...." + someVar + "something" ...我们用%s替换someVar ,将整个东西声明为全局字符串,然后使用String.format(....)

  • if( name.equals("Audi" ){....}

  • String value = map.get("key")

Any ideas guys ? 有什么想法吗? I want some strong arguments. 我想要一些有力的论据。 I'm ready to embrace any stand that's backed by a good argument. 我已经准备好接受任何以良好争论为后盾的立场。

Thanks. 谢谢。

First, let's throw out your assumption: There's nothing inherently wrong with the approach described. 首先,让我们抛弃你的假设:所描述的方法没有任何本质上的错误

It's not about strings being used in more than one place, it's about constants being easy to find and documented, and your code being consistent . 它不是关于字符串在多个地方使用,而是关于常量易于查找和记录,并且您的代码是一致的

private static final String OPERATION_ID = "create";

Really, this isn't used anywhere else? 真的,这其他任何地方都没用过吗? Nothing would break if I changed this to the string "beetlejuice"? 如果我把它改成字符串“beetlejuice”,什么都不会破坏? If something would break, then something else is using this constant... If the "something else" happens to be a codebase in a different language, and that's why they don't share string constants-- that's the exception, not the rule. 如果有什么东西会破坏,那么其他东西正在使用这个常量...如果“其他东西”碰巧是不同语言的代码库,这就是为什么它们不共享字符串常量 - 这是例外,而不是规则。 Consistency! 一致性!


That said, there are a few things I would standardize in a slightly different manner, but I would still standardize them nonetheless: 也就是说,我会以略微不同的方式标准化一些事情,但我仍然会将它们标准化:

I would suggest allowing string literals in the constructors of enums: 我建议在枚举的构造函数中允许字符串文字:

public enum Operation {
    CREATE("create"),
    ...
}

because here, the enum is the constant that is being referenced in the code, not the string literal. 因为在这里,枚举是代码中引用的常量,而不是字符串文字。 Declaring the constant as an enum or as a private static final String are equivalent to me, and there's no need to do both. 将常量声明为枚举或private static final String等同于我,并且不需要同时执行这两个操作。

Additionally, I would not use this pattern anywhere that it breaks your IDE's ability to warn you about missing strings-- For example, looking up strings from .properties files. 此外,我不会在任何地方使用此模式,因为它会破坏IDE警告您丢失字符串的能力 - 例如,查找.properties文件中的字符串。 Many IDEs will give you proper warnings when you look up a key in a .properties file that doesn't exist, but the extra level of indirection might break that depending upon how smart your IDE is. 当您在.properties文件中查找不存在的密钥时,许多IDE会给您正确的警告,但是额外的间接级别可能会破坏这一点,具体取决于IDE的智能程度。

Catch(Exception ex){ LOGGER.log("csv file is corrupt") }

This to me is a bit of a gray area - Is this an internal-only message? 这对我来说有点灰色 - 这是一个内部消息吗? Are the logs only ever seen by you, the developer, or are they for a user's benefit too? 您,开发人员是否只看到过这些日志,还是为了用户的利益?

If it's only for developers of the application These probably don't need to be localized. 如果它仅适用于应用程序的开发人员这些可能不需要本地化。

If you do expect the user to view the logs, then they should be externalized into a .properties file. 如果您确实希望用户查看日志,则应将它们外部化为.properties文件。

It is good coding style to define a constant for a value/literal when the value/literal is used multiple times. 当值/ literal多次使用时,为值/文字定义常量是一种很好的编码风格。

The imposed coding style forces you to use a constant for every string literal. 强加的编码风格强制您为每个字符串文字使用常量。

The good effect of that coding style is: All string literals which really should be declared as constants are now declared as constants. 这种编码风格的好处是:所有真正应该声明为常量的字符串文字现在都被声明为常量。

The bad implication of that coding style is: You - the developers - are not able to decide if a string literal should be defined as constant or not. 编码风格的不良含义是:您 - 开发人员 - 无法决定是否应将字符串文字定义为常量。 This is a heavy punch. 这是一个沉重的打击。

Therefore you should raise your concerns that the good intention of the coding style does not compensate for the mistrust in your developer qualitites. 因此,您应该提出您的担忧,即编码风格的良好意图不能弥补您的开发人员资格中的不信任。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM