尤達條件式

本页使用了标题或全文手工转换
维基百科,自由的百科全书

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

尤達條件式是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). 

外部連結