For faster navigation, this Iframe is preloading the Wikiwand page for 尤達條件式.

尤達條件式

此條目需要补充更多来源。 (2019年5月27日)请协助補充多方面可靠来源改善这篇条目无法查证的内容可能會因為异议提出而被移除。致使用者:请搜索一下条目的标题(来源搜索:"尤達條件式"网页新闻书籍学术图像),以检查网络上是否存在该主题的更多可靠来源(判定指引)。
此條目需要編修,以確保文法、用詞、语气格式標點等使用恰当。 (2018年6月28日)請按照校對指引,幫助编辑這個條目。(幫助討論

尤達條件式(也稱為尤達標記法)是一種計算機編程中的編程風格,在此風格中表達式的兩個部份與條件語句中的順序將會對調, 並且表達式的常量部份將會放在條件語句的左側。這種風格的名稱來自於星際大戰的絕地大師尤達,他使用着缺乏標準語法的英語。

尤達條件式是PHP Symfony編碼標準的一部份。[1]

範例

通常計算機編程中的條件語句會寫成:

if ( $value == 42 ) { /* ... */ }
// Reads like: "If the value is equal to 42..."

在尤達條件式中對於相同的條件語句会反轉过来:

if ( 42 == $value ) { /* ... */ }
// Reads like: "If 42 equals the value..."

常數會放在比較運算子的左側,而右側會寫入測試常數的變量。這個次序和尤達的非標準口語風格非常相似,类似于賓主動語序[2](例如"When nine hundred years old you reach, look as good you will not.",“當九百歲你活到,看起來很好你將不”[3][4])。

優點

將常量放在表達式中不會改變程序的行為(除非此值被評估為false,請參見下文)。在使用單個等號(=)執行賦值操作 而非條件關係比較的編程語法中,可能会發生錯誤使程序產生意料之外的賦值操作,而並非如程序員原意要編寫關係判斷的條件語句。

if (myNumber = 42) { /* ... */ }
// This assigns 42 to myNumber instead of evaluating the desired condition

使用尤達條件式的優點:

if (42 = myNumber) { /* ... */ }
// This is a syntax error and will not compile

由於 42 是一個無法變動的固定常量,因此編譯器會捕捉到該錯誤。

Boolean myBoolean = true;
if (myBoolean = null) { /* ... */ }
// This causes a NullPointerException in Java Runtime, but legal in compilation.

它也可以解決一些不安全的null型別行為。

String myString = null;
if (myString.equals("foobar")) { /* ... */ }
// This causes a NullPointerException in Java

以尤達條件式:

String myString = null;
if ("foobar".equals(myString)) { /* ... */ }
// This is false, as expected

評論

有評論認為尤達條件式一個缺點是缺乏可讀性,其負面效果超過了上述優點。

一些編程語言如Python和Swift之中是不允許在條件式中進行對變量賦值操作的,藉由定義賦值表達式不會被評估就沒有任何值,在這種情況下是不可能發生這類錯誤的。[5]

許多編譯器會對如if (myNumber = 42)的源碼發出警示訊息(例如,GCC-Wall選項會警告括號語句中的賦值為真),讓程序員發現可能發生錯誤的地方。在JavaScript中如ESLint之類的語法建議程序,可以警告條件式中出現賦值操作。[6]

尤達條件式寫法避免null行為的優點也可被認為是一個缺點,因為空指針錯誤或被隱藏,並只出現在程序後期中。

當比較非基本類型時,這種寫法在C++中出現了另一個缺點,因為 == 是一個運算子,並可能沒有定義適當的重載運算子函數,例如CComBSTR與字串文本比較,寫成(L"Hello" == cbstrMessage),不會對應到重載的函數。[7]

參考


  1. ^ Coding Standards (Contributing to Symfony). Symfony.com. [2016-11-12]. (原始内容存档于2016-11-08). 
  2. ^ Pullum, Geoffrey K. Yoda's Syntax the Tribune Analyzes; Supply More Details I Will!. Itre.cis.upenn.edu. Language Log. 2005-05-18 [2014-12-22]. (原始内容存档于2013-03-09). One way to look at Yoda's syntax is that it shows signs of favoring OSV syntax (Object-Subject-Verb) as the basic order in the simple clause. 
  3. ^ The StarWars.com 10: Best Yoda Quotes. starwars.com. Lucasfilm, Ltd. 2013-11-26 [2014-12-22]. (原始内容存档于2014-12-22). When nine hundred years old you reach, look as good you will not. 
  4. ^ Quotes for Yoda (Character). imdb.com. Amazon. [2014-12-22]. (原始内容存档于2015-02-12). When nine hundred years old *you* reach, look as good *you* will not, hmm? 
  5. ^ The Swift Programming Language (Swift 3.0.1): Basic Operators. Developer.apple.com. 2016-10-27 [2016-11-12]. (原始内容存档于2016-04-23). 
  6. ^ disallow assignment operators in conditional statements. eslint.org. [2017-02-17]. (原始内容存档于2017-02-18). 
  7. ^ CComBSTR::operator. Msdn.microsoft.com. [2016-11-12]. (原始内容存档于2016-09-10). 

外部連結

{{bottomLinkPreText}} {{bottomLinkText}}
尤達條件式
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?