合并与空字符串连接
我的同事是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
虽然从技术角度来看我更喜欢第二行,但第一行实际上对我来说更具可读性......
While I prefer the second line from the technical perspective, the first line is actually more readable to me...
第二行对我来说看起来更具可读性,它清楚地表明了右侧表达式的意图。
The second line looks more readable to me, it makes clear the intent of the right side expression.
与第一个示例相比,我更喜欢您的第二个示例,因为它更主动地防止
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 ofString.Empty
over""
as it is a constant and does not require the creation of a newString
(and yes this is an extreme nitpick but important to note).我会使用合并运算符或 foo = str == null 吗? "" : str; 有两个原因:
第一个示例在其他花括号语言(特别是 Java 和 JavaScript)中没有相同的行为,其中将空引用附加到空字符串会导致包含文本
null
的字符串。 因此,第一个示例需要更多的脑力劳动才能确保其在所编写的语言中产生正确的行为。 即使给出其他线索,它是 C#(例如小写类型string
),任何需要开发人员放慢速度并思考“这段代码的意图是什么”的事情都会导致生产力下降。 从一种语言转移到另一种语言时,它也可能会引入错误。第二个示例更清楚地表达了代码的意图,正如原始问题中所述:
当 str 为 null 时,将 foo 设置为空字符串
与第一个示例相反,可以理解为:
将 foo 设置为附加 str 的空字符串(当 str 为 null 时,这可能会产生将 foo 设置为空字符串的副作用)
作为程序员,我们可能应该尝试表达意图 我们的代码,而不是依赖其他操作的副作用来为我们提供我们想要的结果。
I'd use the coalesce operator or
foo = str == null ? "" : str;
for two reasons: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 typestring
), 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.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.
IMO,最好明确定义这样的逻辑,即不要使用字符串连接来避免空字符串并使用条件语句或 ?? 操作员。
关于其他评论:
不正确。 C# 编译器将 "" + string2 编译为与 string1 100% 相同的代码? “”。 此外,C# 编译器将 + 运算符转换为调用 string.Concat 方法,该方法又使用 string.IsNullOrEmpty 函数检查参数,并且在这种情况下不会分配新字符串。
.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:
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.
.NET framework supports string interning so "" and string.Empty point to the same memory region
我个人认为其中一个并不比另一个更具可读性。 我更喜欢这个:
只是因为我真的很喜欢?? 操作员。
如果你是一个新手,我认为这会更容易理解一点:
或者
但是,你有点需要问自己,“你到底想要多简单?”
I don't think one is any more readable than the other personally. I prefer this:
simply because I really like the ?? operator.
If you are a brand newbie, I think that this would be a little easier to understand:
or
However, you kinda need to ask youself, "How simple do you really want to get?"