简体   繁体   English

依赖编程语言的默认功能是明智的吗?

[英]Is it wise to rely on default features of a programming language?

Should I frequently rely on default values? 我应该经常依赖默认值吗?

For example, in PHP, if you have the following: 例如,在PHP中,如果您有以下内容:

<?php

$var .= "Value";

?>

This is perfectly fine - it works. 这非常好 - 它有效。 But what if assignment like this to a previously unused variable is later eliminated from the language? 但是,如果将这样的赋值转换为以前未使用的变量后来从语言中消除了怎么办? (I'm not referring to just general assignment to an unused variable.) (我不是指对未使用的变量的一般赋值。)

There are countless examples of where the default value of something has changed and so much existing code was then useless. 有无数的例子表明某些东西的默认值已经发生了变化,而现有的代码却没用了。

On the other hand, without defaults, there is a lot of code redundancy. 另一方面,没有默认值,会有很多代码冗余。

What is the proper way of dealing with this? 处理这个问题的正确方法是什么?

In my opinion, the odds of a language changing drastically once it reaches a certain level of acceptance are pretty low. 在我看来,一旦语言达到一定程度的接受程度,语言变化的可能性非常低。

To me, each language comes with a (sometimes more or less) unique set of features. 对我而言,每种语言都附带一个(有时或多或少)独特的功能集。 Not using those because they just might disappear some day seems shortsighted. 不使用那些,因为它们有一天可能会消失,似乎是短视的。 Naturally, don't use esoteric features just for the sake of doing so -- make sure you follow usual principles of readability and best practices for your language of choice, but otherwise I see no need to discriminate against particular features. 当然,不要仅仅为了这样做而使用深奥的功能 - 确保你遵循通常的可读性和最佳实践原则,但我认为没有必要区别于特定的功能。

Default-value features of a programming language, if actually a documented part of the standard rather than just an accident of the implementation (which many past "default initializations" have been), are no different from any other features of a programming language. 编程语言的默认值特征,如果实际上是标准的记录部分而不仅仅是实现的意外(许多过去的“默认初始化”),与编程语言的任何其他特征没有区别。 You might as well ask if it's wise to rely on anything else in the language, and the answer regardless of wisdom is that there's no choice -- you have to rely on something, and anything could hypothetically be changed in a future version. 你不妨问一下,依靠语言中的其他东西是否明智,无论智慧如何,答案是没有选择 - 你必须依赖某些东西,并且在未来的版本中可以假设任何东西都可以改变。

Of course, if the thing that you're relying on is a commonly-used feature of the language, rather than an odd corner case, then there's a lot more chance that it will be retained in future versions. 当然,如果您所依赖的东西是该语言的常用功能,而不是奇怪的角落情况,那么在未来版本中保留它的可能性要大得多。 In addition, if you're concerned about such things, it's wise to choose a well-established language that has a history of maintaining backwards compatibility. 此外,如果你担心这些事情,那么选择一个有着保持向后兼容性历史的成熟语言是明智的。 Some languages take great pains to make sure that older code runs in the new version of the language, and some less so. 有些语言需要付出很大的努力才能确保旧代码在新版本的语言中运行,而有些则不那么强。

So, that's the general answer. 所以,这是一般的答案。 The specific answer about default values is that it depends on the particular case, the language in question, and so forth. 有关默认值的具体答案是,它取决于具体情况,相关语言等。 You can have absolute ironclad reliance on the fact that global static variables in C will be zero at program start. 你可以完全依赖于C语言中的全局静态变量在程序启动时为零的事实。 Some other cases are rather notably less reliable. 其他一些案例显然不太可靠。

If something is a defined feature of a language, it's probably pretty safe to rely on it. 如果某些东西是某种语言的定义特征,那么依赖它就可能非常安全。

Where you should be wary of relying on functionality is when you get into areas where you're dealing with behavior that is either unspecified, or if you're "misusing" a feature by using it for something completely other than what it was intended for. 你应该警惕依赖功能的地方是当你进入你未处理行为的区域,或者如果你通过将某个功能用于完全不同于预期的功能而“滥用”某个功能时。

Since "default value of a string" is pretty much a mainline scenario, it's probably safe. 由于“字符串的默认值”几乎是主线方案,因此它可能是安全的。 What's more dangerous is that if you happen to declare that variable earlier, you can get hit by it changing the expected value as an unintended side effect. 更危险的是,如果你碰巧早先声明了这个变量,你可能会被它改变预期的值作为意外的副作用。

Basically, if you're not abusing a language feature, or relying on undefined behavior, you should probably worry more about unintended side effects in your code than you should worry about the language changing - especially if the language is mature. 基本上,如果您没有滥用语言功能或依赖未定义的行为,您可能应该更担心代码中的意外副作用,而不是担心语言的变化 - 特别是如果语言成熟的话。

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

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