合并与空字符串连接

发布于 2024-07-10 20:31:34 字数 409 浏览 14 评论 0原文

我的同事是 C# 新手,不了解合并运算符。 所以,我看到他写了一行这样的代码:

string foo = "" + str;

这个想法是,如果 str 为 null,则该表达式将返回一个空字符串。 当然,可以重写为:

string foo = str ?? "";

我觉得这样会更具可读性。 但这真的有那么重要吗? 可读性的好处是否足以建议返回并使这些行看起来像第二行? 或者这是我应该学会放手的事情之一(前提是我的同事接受了未来做到这一点的最佳方法的教育)?

编辑:请注意,我很欣赏效率评论,但这并没有真正用于任何性能至关重要的情况。 因此,虽然这些信息很有趣,但它不一定是我认为重要的。

My coworker is new to C# and didn't know about the coalesce operator. So, I saw him write a line of code like this:

string foo = "" + str;

The idea being that if str is null, this expression would return an empty string. Of course, that could be rewritten as this:

string foo = str ?? "";

And I feel that would be more readable. But is it really that big a deal? Are the readability benefits enough to suggest going back and making those lines look like the second? Or is this one of those things that I should learn to let go (provided that my coworker is educated on the best way to do this in the future)?

EDIT: Just a note, I appreciate the efficiency comments, but this isn't really being used in any situations where that performance would be critical. So while that info is interesting, it's not necessarily what I feel is important.

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

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

发布评论

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

评论(6

歌入人心 2024-07-17 20:31:35

虽然从技术角度来看我更喜欢第二行,但第一行实际上对我来说更具可读性......

While I prefer the second line from the technical perspective, the first line is actually more readable to me...

魂牵梦绕锁你心扉 2024-07-17 20:31:35

第二行对我来说看起来更具可读性,它清楚地表明了右侧表达式的意图。

The second line looks more readable to me, it makes clear the intent of the right side expression.

后知后觉 2024-07-17 20:31:35

与第一个示例相比,我更喜欢您的第二个示例,因为它更主动地防止 str 为空并且更具表现力。 此外,我建议使用 String.Empty 而不是 "",因为它是一个常量,不需要创建新的 String (是的,这是一个极端的挑剔,但值得注意)。

I would prefer your second example to the first as it is more proactive against str being null and is more expressive. Also I would recommend the use of String.Empty over "" as it is a constant and does not require the creation of a new String (and yes this is an extreme nitpick but important to note).

可是我不能没有你 2024-07-17 20:31:35

我会使用合并运算符或 foo = str == null 吗? "" : str; 有两个原因:

  1. 第一个示例在其他花括号语言(特别是 Java 和 JavaScript)中没有相同的行为,其中将空引用附加到空字符串会导致包含文本 null 的字符串。 因此,第一个示例需要更多的脑力劳动才能确保其在所编写的语言中产生正确的行为。 即使给出其他线索,它是 C#(例如小写类型 string),任何需要开发人员放慢速度并思考“这段代码的意图是什么”的事情都会导致生产力下降。 从一种语言转移到另一种语言时,它也可能会引入错误。

  2. 第二个示例更清楚地表达了代码的意图,正如原始问题中所述:

    当 str 为 null 时,将 foo 设置为空字符串

    与第一个示例相反,可以理解为:

    将 foo 设置为附加 str 的空字符串(当 str 为 null 时,这可能会产生将 foo 设置为空字符串的副作用)

作为程序员,我们可能应该尝试表达意图 我们的代码,而不是依赖其他操作的副作用来为我们提供我们想要的结果。

I'd use the coalesce operator or foo = str == null ? "" : str; for two reasons:

  1. The first example does not have the same behaviour in other curly-brace languages (particularly Java and JavaScript), where appending a null reference to an empty string results in a string containing the text null. As a result, the first example requires much more mental effort to ensure it will result in the correct behaviour in the language being authored. Even given other clues it is C# (such as the lowercase type string), anything that requires the developer to slow down and think "what is the intent of this code" results in lost productivity. It could also introduce bugs when moving from language to language.

  2. The second example more clearly expresses the intent of the code, which as described in the original question is:

    when str is null set foo to an empty string

    As opposed to the first example, which can be read as:

    set foo to an empty string with str appended (which may have the side effect of setting foo to an empty string when str is null)

As programmers, we should probably be trying to express the intent of our code, rather than relying on side-effects of other operations to give us the results we want.

も让我眼熟你 2024-07-17 20:31:34

IMO,最好明确定义这样的逻辑,即不要使用字符串连接来避免空字符串并使用条件语句或 ?? 操作员。

关于其他评论:

还有一个性能优势
使用空合并运算符,
因为不需要进行串联
(因此,没有额外的字符串
正在创建实例
不必要)

不正确。 C# 编译器将 "" + string2 编译为与 string1 100% 相同的代码? “”。 此外,C# 编译器将 + 运算符转换为调用 string.Concat 方法,该方法又使用 string.IsNullOrEmpty 函数检查参数,并且在这种情况下不会分配新字符串。

我还建议使用
String.Empty over "" 因为它是
常数并且不需要
创建一个新的字符串

.NET 框架支持字符串驻留,因此 "" 和 string.Empty 指向同一内存区域

IMO, it is much better to clearly define such logic i.e. don't use string concatenation to avoid null string and use conditional statement or ?? operator.

Regarding other comments:

there is also a performance benefit to
using the null-coalescing operator,
since no concatenation need take place
(and, therefore, no extra string
instance is being created
unnecessarily)

Not true. C# compiler compiles "" + string2 to the 100% same code as string1 ?? "". Moreover C# compiler translates + operator to call to string.Concat method which in turn checks arguments with string.IsNullOrEmpty function and doesn't allocate a new string in this case.

Also I would recommend the use of
String.Empty over "" as it is a
constant and does not require the
creation of a new String

.NET framework supports string interning so "" and string.Empty point to the same memory region

柒七 2024-07-17 20:31:34

我个人认为其中一个并不比另一个更具可读性。 我更喜欢这个:

string foo = str ?? "";

只是因为我真的很喜欢?? 操作员。

如果你是一个新手,我认为这会更容易理解一点:

string foo = str == null ? "" : str;

或者

string foo = "";
if (str != null) foo = str;

但是,你有点需要问自己,“你到底想要多简单?”

I don't think one is any more readable than the other personally. I prefer this:

string foo = str ?? "";

simply because I really like the ?? operator.

If you are a brand newbie, I think that this would be a little easier to understand:

string foo = str == null ? "" : str;

or

string foo = "";
if (str != null) foo = str;

However, you kinda need to ask youself, "How simple do you really want to get?"

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